jsp中 response和request区别
2015-06-09 10:06
417 查看
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型,在使用时要注意类型转换。
写一段示例代码:
属于重定向请求;
其地址栏的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型,在使用时要注意类型转换。
写一段示例代码:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <base href="<%=basePath%>"> 7 8 <title>登陆页面</title> 9 </head> 10 11 <body> 12 <h2>登陆页面</h2> 13 <% 14 String errorCode =(String)request.getAttribute("error");//request.getParameter("error"); 15 if(errorCode != null && ! "".equals("error") && "01".equals(errorCode)){ 16 %> 17 <h3 style="color:red">用户名或密码错误!</h3> 18 <% 19 } 20 %> 21 22 <form action="login.jsp" method="post"> 23 <p>用户名:<input type="text" name = "userName" /><br/></p> 24 <p>密 码:<input type="password" name ="userPwd" /><br/></p> 25 <p><input type = "submit" value = "登陆" /><br/></p> 26 </form> 27 <a href="reg.jsp">注册新用户</a> 28 29 </body> 30 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 8 <title>登陆页面</title> 9 10 </head> 11 12 <body> 13 <% 14 String name = request.getParameter("userName"); 15 String pwd = request.getParameter("userPwd"); 16 if("shamuu".equals(name) && "123".equals(pwd)){ 17 %> 18 <h3 style="color:red;">欢迎你!<%=name %></h3> 19 <% 20 }else{ 21 //response.sendRedirect("index.jsp?error=01"); 22 request.setAttribute("error","01"); 23 request.getRequestDispatcher("index.jsp").forward(request,response); 24 25 } 26 %> 27 </body> 28 </html>
相关文章推荐
- 【Java GUI】文本框和文本区
- Android Studio --> Gradle Build设置自动
- Adeneo Embedded: Building Qt 5.1 for Freescale i.MX6Introduction on LTIB
- Building QT5 for IMX6(all)
- hdu 4027 Can you answer these queries?(线段树)
- UILabel调整字间距
- UITextView的创建/背景图片/边框和颜色/圆角等
- 关于在openstack中创建实例时任务一直卡在Building的现象描述
- Java GUI 使用确认对话框控制循环
- Column count doesn't match value count at row 1解决方法
- Atitit.guice3 ioc 最佳实践 o9o
- longest-common-subsequence
- pytest框架系列教程:pytest框架在UI自动化测试中的应用
- Android之UI主线程更新问题
- 如何在Sublime Text中创建build system命令打开浏览器预览html页面
- UIButton 设置圆角 边框颜色 点击回调方法
- UIScrollView常见属性
- java基础—GUI概述
- 北京周边ui设计培训机构哪里好
- UIScrollView无法滚动的解决办法