1、response

属于重定向请求;

其地址栏的URL会改变;

会向服务器发送两次请求;

2、 request

属于请求转发;

其地址栏的URL不会改变;

向服务器发送一次请求;

举一个区分它们的简单实例:

A向B借钱:

第一种:用response。B没有钱,请求失败,但是B告诉A,C有钱。于是A再次向C借钱,C借给A,请求成功。

第二种:用request。B没有钱,但是B向C借钱然后给A,请求成功。这次A只发送了一次请求,他并不知道借的钱是C的。

用response方法是这样的:

response.sendRedirect( );

用resquest方法:

request.setAttribute("key","value");

request.getRequestDispatcher("index.jsp").forward(request,response);

这里的setAttribute传递的参数只能由request.getAttribute( )来接收。request.getAttribute( )方法返回值是object型,在使用时要注意类型转换。

写一段示例代码:

Jsp代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4. <head>  
  5. <base href="<%=basePath%>">  
  6. <title>登陆页面</title>  
  7. </head>  
  8. <body>  
  9. <h2>登陆页面</h2>  
  10. <%  
  11. String errorCode =(String)request.getAttribute("error");//request.getParameter("error");  
  12. if(errorCode != null && ! "".equals("error") && "01".equals(errorCode)){  
  13. %>  
  14. <h3 style="color:red">用户名或密码错误!</h3>  
  15. <%  
  16. }  
  17. %>  
  18. <form action="login.jsp" method="post">  
  19. <p>用户名:<input type="text" name = "userName" /><br/></p>  
  20. <p>密&nbsp;&nbsp;码:<input type="password" name ="userPwd"  /><br/></p>  
  21. <p><input type = "submit" value = "登陆" /><br/></p>  
  22. </form>  
  23. <a href="reg.jsp">注册新用户</a>  
  24. </body>  
  25. </html>

Jsp代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4. <head>  
  5. <title>登陆页面</title>  
  6. </head>  
  7. <body>  
  8. <%  
  9. String name = request.getParameter("userName");  
  10. String pwd = request.getParameter("userPwd");  
  11. if("shamuu".equals(name) && "123".equals(pwd)){  
  12. %>  
  13. <h3 style="color:red;">欢迎你!<%=name %></h3>  
  14. <%  
  15. }else{  
  16. //response.sendRedirect("index.jsp?error=01");  
  17. request.setAttribute("error","01");  
  18. request.getRequestDispatcher("index.jsp").forward(request,response);  
  19. }  
  20. %>  
  21. </body>  
  22. </html>

jsp中 response和request区别的更多相关文章

  1. JSP中pageEncoding和charset区别,中文乱码解决方案(转载)

    转载自:JSP中pageEncoding和charset区别,中文乱码解决方案 JSP指令标签中<%@ page contentType="text/html;charset=GB23 ...

  2. Servlet中response、request乱码问题解决

    Java Web(二) Servlet中response.request乱码问题解决   三月不减肥,五月徒伤悲,这就是我现在的状态,哈哈~ 健身.博客坚持. --WH 一.request请求参数出现 ...

  3. Java Web之Servlet中response、request乱码问题解决

    Java Web之Servlet中response.request乱码问题解决   一.request请求参数出现的乱码问题 get请求: get请求的参数是在url后面提交过来的,也就是在请求行中, ...

  4. JSP 中 pageEncoding 和 charset 区别以及中文乱码解决方案

    一.JSP 中 pageEndcodeing 和 charset 的作用 <%@ page contentType="text/html;charset=GB2312"%&g ...

  5. JSP中getParameter和getAttribute区别

    (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...

  6. JSP中page、request、session、application作用域的使用

    几乎所有的Web开发语言都支持Session功能,Servlet也不例外. Servlet/JSP中的Session功能是通过作用域(scope)这个概念来实现的. 作用域分为四种,分别为: page ...

  7. 关于jsp中response.sendRedirect显示错误

    今天在jsp中作判断时,当不同条件时利用response.sendRedirect(“url”)来转向不同的页面,首先是判断验证码,当错误时就转向错误页面:当正确时,才进行用户名和密码的判断,同样也r ...

  8. JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer

    String path = request.getContextPath();  String basePath = request.getScheme()+"://"+reque ...

  9. JSP中page,request,session,application四个域对象区别

    page page指当前页面.只在一个jsp页面里有效 . page里的变量没法从index.jsp传递到test.jsp,只要页面跳转了,它们就不见了. pageContext 如果把变量放到pag ...

随机推荐

  1. scroll滚动条插件初始化问题

    一种特殊场景下是滚动条容器先隐藏,点击某个东西后显示出来.然后实例化滚动条.实例 js: var flag = true; document.getElementById('btn1').onclic ...

  2. php基础28:文件读取

    <?php //1.追加文件内容 // $fp = fopen("test.txt", "a"); // $outString = "this ...

  3. oracle文字与格式字符串不匹配的解决

    oracle文字与格式字符串不匹配的解决 oracle的日期时间类型 在往oracle的date类型插入数据的时候,记得要用to_date()方法. 如insert into CUSLOGS(STAR ...

  4. MVC4验证用户登录特性实现方法

    在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...

  5. python字符串str和字节数组相互转化

    b = b"Hello, world!" # bytes object s = "Hello, world!" # str object print('str ...

  6. Gradle tip #2: understanding syntax

    In the Part 1 we talked about tasks and different stages of the build lifecycle. But after I publish ...

  7. 接收content-type:multipart/form-data类型的参数

    一.问题描述 最近在写接口程序,该接口需要与其他公司的程序对接.对方发送content-type:multipart/form-data类型的参数,结果通过request.getParameter(p ...

  8. MVC5 + EF6 + Bootstrap3 (13) 查看详情、编辑数据、删除数据

    Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-rud.html 系列教程:MVC5 + EF6 + Boo ...

  9. 简便的自动布局,对UIStackView的个人理解!

    序言: 更新了很久的Linux,我怕朋友们都视觉疲劳了,今天就更新在学ios开发时候,对一些知识点的理解.希望各位会喜欢! 正文: UIStackView 类提供了一个高效的接口用于平铺一行或一列的视 ...

  10. WIN8 浏览器排版不兼容问题

    经常访问网站失败 访问部分网站只显示白底蓝字没有排版也没有图片 尝试了网上的各种方法,也是用了360和百度安全管家的网页修复 重置了IE设置 都不行!!! 最后有效的方法是 更换DNS 208.67. ...