这篇文章主要介绍了jsp response.sendRedirect()用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

sendRedirect()
response和request一样都是jsp内置对象,request是获取用户的请求,response处理用户请求。sendRedirect()函数的作用是重定向网页,向浏览器发送一个特殊的Header,然后由浏览器来做重定向,转到指定的页面。下面我将创建四个页面,首先是sex.jsp,有一个下拉列表和提交按钮确定,选择“男”,就跳转到male.jsp,选择“女”就跳转到female.jsp,中间通过sex_action.jsp进行重定向

  1. <!-- sex.jsp -->
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
  11. <title>Sex Select's page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. </head>
  18. <body>
  19. <form action="<%=basePath%>c03/sex_action.jsp" method="post">
  20. <select name="sex">
  21. <option></option>
  22. <option></option>
  23. </select>
  24. <button type="submit">提交</button>
  25. </form>
  26. </body>
  27. </html>
  1. <!-- sex_action.jsp -->
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
  11. <title>My JSP 'sex_action.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. </head>
  18. <body>
  19. <%
  20. request.setCharacterEncoding("UTF-8");
  21. String sex = request.getParameter("sex");
  22. out.println(sex);
  23. if("男".equals(sex)) {
  24. response.sendRedirect("male.jsp");
  25. return;
  26. }
  27. else if("女".equals(sex)) {
  28. response.sendRedirect("female.jsp");
  29. return;
  30. }
  31. %>
  32. </body>
  33. </html>

到此这篇关于jsp response.sendRedirect()用法详解的文章就介绍到这了。

更多相关文章

  1. android 6.0权限问题处理的核心代码--shouldShowRequestPermissi
  2. Android-Intent的使用方法详解
  3. Android(安卓)IntentService用法和源码分析
  4. Android:Material Design详解
  5. Android透明字体画法
  6. Android(安卓)中数据库查询方法query()中的selectionArgs的用法
  7. Android(安卓)DownloadManager 用法
  8. android --相机使用详解概述
  9. Android开发之InstanceState详解

随机推荐

  1. 关于android的id
  2. Android:常用属性整理
  3. Android画图之Bitmap(二)
  4. Android的UI结构试图工具hierarchyviewer
  5. Android Studio开发指南
  6. Android中animation方面知识: Android:int
  7. Realtek 8192cu 支持 Android Hotspot
  8. Android Service总结01 目录
  9. [Android]滑动刷新ListView——android-p
  10. android - TextView单行显示...或者文字