- 浏览: 247264 次
- 性别:
- 来自: 湖南
文章分类
- 全部博客 (194)
- java (23)
- 数据结构和算法 (5)
- oracle (7)
- sql server (1)
- mysql (5)
- Ajax (5)
- JSTL (7)
- jsp (20)
- 组件 (11)
- C++可视化 (0)
- javascript (14)
- c/c++ (0)
- XML (6)
- 只是扩宽 (0)
- 设计模式 (3)
- 正则表达式 (3)
- 心情和感悟 (4)
- ACM (3)
- HTML/css (9)
- 软件破解 (2)
- spring (1)
- struts (15)
- hibernate (10)
- Servlet (2)
- sql (2)
- 面试题 (1)
- struts2 (22)
- Unix/Linux (0)
- javamail (1)
- svn (1)
- 异常 (3)
- EJB (3)
- jquery (2)
- android (1)
最新评论
-
lianlupengUestc:
From my testing (Struts2 versio ...
struts2 注解 -
yulongxiang:
非常感谢!!!!!!!
使用动态ActionForm(转)
getRequestDispatcher()与sendRedirect()的区别
1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ;
response.sendRedirect()是重新定向,前后页面不是一个request。
request.getRequestDispather();返回的是一个RequestDispatcher对象。
2.RequestDispatcher.forward()是在服务器端运行;
HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成.
所以RequestDispatcher.forward()对于浏览器来说是“透明的”;
而HttpServletResponse.sendRedirect()则不是。
3.ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径; 而
ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。因为
ServletRequest具有相对路径的概念;而ServletContext对象无次概念。
RequestDispatcher对象从客户端获取请求request,并把它们传递给服务器上的servlet,html或
jsp。它有两个方法:
1.void forward(ServletRequest request,ServletResponse response)
用来传递request的,可以一个Servlet接收request请求,另一个Servlet用这个request请求来产生response。request传递的请求,response是客户端返回的信息。forward要在response到达客户端之前调用,也就是 before response body output has been flushed。如果不是的话,它会报出异常。
2.void include(ServletRequest request,ServletResponse response)
用来记录保留request和response,以后不能再修改response里表示状态的信息。
如果需要把请求转移到另外一个Web App中的某个地址,可以按下面的做法:
1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).
2. 调用ServletContext.getRequestDispatcher(String url)方法。
eg:ServletContext.getRequestDispatcher("smserror.jsp").forward(request,response);
http://lanew.blog.ccidnet.com/blog-htm-do-showone-type-blog-itemid-3690834-uid-327434.html
二者区别:
response.sendRedirect(url)跳转到指定的URL地址,产生一个新的request,所以要传递参数只有在url后加参
数,如:
url?id=1.
request.getRequestDispatcher(url).forward(request,response)是直接将请求转发到指定URL,所以该请求
能够直接获得上一个请求的数据,也就是说采用请求转发,request对象始终存在,不会重新创建。而
sendRedirect()会新建request对象,所以上一个request中的数据会丢失。
更具体来说就是这样的:
redirect 会首先发一个response给浏览器, 然后浏览器收到这个response后再发一个requeset给服务器, 然后
服务器发新的response给浏览器. 这时页面收到的request是一个新从浏览器发来的.
forward 发生在服务器内部, 在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面
收到的request不是从浏览器直接发来了,可能己经用request.setAttribute在request里放了数据.在转到的页
面可直接用request.getAttribute获得数据。
最基本的用法就如上了,其他的一些应注意的地方如下:
跳转方式
http://localhost:8080/Test应用
运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何
URL。
表单form的action="/uu";sendRedirect("/uu");表示相对于服务器根路径。如http://localhost:8080/Test应
用(则提交至http://localhost:8080/uu);
Forward代码中的"/uu"则代表相对与WEB应用的路径。如http://localhost:8080/Test应用(则提交至
http://localhost:8080/Test/uu);
(运用RequestDispatcher接口的Forward)方法
forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,
同时forward()无法在后面带参数传递,比如servlet?name=frank,这样不行,可以程序内通过
response.setAttribute("name",name)来传至下一个页面.
重定向后浏览器地址栏URL不变.
只有在客户端没有输出时才可以调用forward方法。如果当前页面的缓冲区(buffer)不是空的,那么你在调用
forward方法前必须先清空缓冲区。
"/"代表相对与web应用路径
RequestDispatcher rd = request.getRequestDispatcher("/ooo");
rd.forward(request, response);提交至http://localhost:8080/Test/ooo
RequestDispatcher rd = getServletContext().getRequestDispatcher("/ooo");
rd.forward(request, response);提交至http://localhost:8080/Test/ooo
RequestDispatcher rd =getServletContext().getNamedDispatcher("TestServlet");(TestServlet为一个
<servlet-name>)
rd.forward(request, response);提交至名为TestServlet的servlet
如果在<jsp:forward>之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用,
这一点应该特别注意。
另外要注意:它不能改变浏览器地址,刷新的话会导致重复提交
从http://localhost:8080/Test/gw/page.jsp中转发
<jsp:forward page="OtherPage.jsp"/>在JSP页面被解析后转换成pageContext.forward("OtherPage.jsp");
"/OtherPage.jsp"提交到http://localhost:8080/Test/OtherPage.jsp
"OtherPage.jsp"提交到http://localhost:8080/Test/gw/OtherPage.jsp
(运用HttpServletResponse接口的sendRedirect)方法302
是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面,
同时它可以重定向至不同的主机上,sendRedirect()可以重定向有frame.的jsp文件.
假设转发代码包含于注册的servlet-url为/ggg/tt;jsp为/ggg/tt.jsp:
绝对路径:response.sendRedirect("http://www.brainysoftware.com
根路径:response.sendRedirect("/ooo")发送至http://localhost:8080/ooo
相对路径:response.sendRedirect("ooo")发送至http://localhost:8080/Test/ggg/ooo,
sendRedirect等同于此方式
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "/newpath/jsa.jsp";
response.setHeader("Location",newLocn);
(Meta Refresh)方法200
这种方法是由HTML提供的,Meta本身就是HTML标签。使用方法是:<meta http-equiv="refresh" content="5;
url=http://www.dreamdu.com/" />
相应的java代码
String content=stayTime+";URL="+URL;
response.setHeader("REFRESH",content);
------------------------------------------------------------------------------------------------
--------------------------
使用response.sendRedirect()地址栏将改变
使用request.getRequestDispatcher().forward(request,response)地址栏中的信息保持不变.
------------------------------------------------------------------------------------------------
--------------------------
request.setAttribute存的东西
只用通过方法2跳转 才能在新页取出来
1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ;
response.sendRedirect()是重新定向,前后页面不是一个request。
request.getRequestDispather();返回的是一个RequestDispatcher对象。
2.RequestDispatcher.forward()是在服务器端运行;
HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成.
所以RequestDispatcher.forward()对于浏览器来说是“透明的”;
而HttpServletResponse.sendRedirect()则不是。
3.ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径; 而
ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。因为
ServletRequest具有相对路径的概念;而ServletContext对象无次概念。
RequestDispatcher对象从客户端获取请求request,并把它们传递给服务器上的servlet,html或
jsp。它有两个方法:
1.void forward(ServletRequest request,ServletResponse response)
用来传递request的,可以一个Servlet接收request请求,另一个Servlet用这个request请求来产生response。request传递的请求,response是客户端返回的信息。forward要在response到达客户端之前调用,也就是 before response body output has been flushed。如果不是的话,它会报出异常。
2.void include(ServletRequest request,ServletResponse response)
用来记录保留request和response,以后不能再修改response里表示状态的信息。
如果需要把请求转移到另外一个Web App中的某个地址,可以按下面的做法:
1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).
2. 调用ServletContext.getRequestDispatcher(String url)方法。
eg:ServletContext.getRequestDispatcher("smserror.jsp").forward(request,response);
http://lanew.blog.ccidnet.com/blog-htm-do-showone-type-blog-itemid-3690834-uid-327434.html
二者区别:
response.sendRedirect(url)跳转到指定的URL地址,产生一个新的request,所以要传递参数只有在url后加参
数,如:
url?id=1.
request.getRequestDispatcher(url).forward(request,response)是直接将请求转发到指定URL,所以该请求
能够直接获得上一个请求的数据,也就是说采用请求转发,request对象始终存在,不会重新创建。而
sendRedirect()会新建request对象,所以上一个request中的数据会丢失。
更具体来说就是这样的:
redirect 会首先发一个response给浏览器, 然后浏览器收到这个response后再发一个requeset给服务器, 然后
服务器发新的response给浏览器. 这时页面收到的request是一个新从浏览器发来的.
forward 发生在服务器内部, 在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面
收到的request不是从浏览器直接发来了,可能己经用request.setAttribute在request里放了数据.在转到的页
面可直接用request.getAttribute获得数据。
最基本的用法就如上了,其他的一些应注意的地方如下:
跳转方式
http://localhost:8080/Test应用
运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何
URL。
表单form的action="/uu";sendRedirect("/uu");表示相对于服务器根路径。如http://localhost:8080/Test应
用(则提交至http://localhost:8080/uu);
Forward代码中的"/uu"则代表相对与WEB应用的路径。如http://localhost:8080/Test应用(则提交至
http://localhost:8080/Test/uu);
(运用RequestDispatcher接口的Forward)方法
forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,
同时forward()无法在后面带参数传递,比如servlet?name=frank,这样不行,可以程序内通过
response.setAttribute("name",name)来传至下一个页面.
重定向后浏览器地址栏URL不变.
只有在客户端没有输出时才可以调用forward方法。如果当前页面的缓冲区(buffer)不是空的,那么你在调用
forward方法前必须先清空缓冲区。
"/"代表相对与web应用路径
RequestDispatcher rd = request.getRequestDispatcher("/ooo");
rd.forward(request, response);提交至http://localhost:8080/Test/ooo
RequestDispatcher rd = getServletContext().getRequestDispatcher("/ooo");
rd.forward(request, response);提交至http://localhost:8080/Test/ooo
RequestDispatcher rd =getServletContext().getNamedDispatcher("TestServlet");(TestServlet为一个
<servlet-name>)
rd.forward(request, response);提交至名为TestServlet的servlet
如果在<jsp:forward>之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用,
这一点应该特别注意。
另外要注意:它不能改变浏览器地址,刷新的话会导致重复提交
从http://localhost:8080/Test/gw/page.jsp中转发
<jsp:forward page="OtherPage.jsp"/>在JSP页面被解析后转换成pageContext.forward("OtherPage.jsp");
"/OtherPage.jsp"提交到http://localhost:8080/Test/OtherPage.jsp
"OtherPage.jsp"提交到http://localhost:8080/Test/gw/OtherPage.jsp
(运用HttpServletResponse接口的sendRedirect)方法302
是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面,
同时它可以重定向至不同的主机上,sendRedirect()可以重定向有frame.的jsp文件.
假设转发代码包含于注册的servlet-url为/ggg/tt;jsp为/ggg/tt.jsp:
绝对路径:response.sendRedirect("http://www.brainysoftware.com
根路径:response.sendRedirect("/ooo")发送至http://localhost:8080/ooo
相对路径:response.sendRedirect("ooo")发送至http://localhost:8080/Test/ggg/ooo,
sendRedirect等同于此方式
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "/newpath/jsa.jsp";
response.setHeader("Location",newLocn);
(Meta Refresh)方法200
这种方法是由HTML提供的,Meta本身就是HTML标签。使用方法是:<meta http-equiv="refresh" content="5;
url=http://www.dreamdu.com/" />
相应的java代码
String content=stayTime+";URL="+URL;
response.setHeader("REFRESH",content);
------------------------------------------------------------------------------------------------
--------------------------
使用response.sendRedirect()地址栏将改变
使用request.getRequestDispatcher().forward(request,response)地址栏中的信息保持不变.
------------------------------------------------------------------------------------------------
--------------------------
request.setAttribute存的东西
只用通过方法2跳转 才能在新页取出来
发表评论
-
LTS 轻量级分布式任务调度框架(Light Task Schedule)
2015-03-07 09:53 8645LTS 轻量级分布式任务调度框架(Light Task Sch ... -
LTS 轻量级分布式任务调度框架(Light Task Schedule)
2015-03-07 09:52 0<div class="iteye-blog- ... -
Struts2.1 处理未知Action 和访问不存在的页面
2011-10-18 12:13 1326你可以配置个默认action,当请求的action不存在时,他 ... -
java单例模式的几种写法
2011-07-05 19:52 1439private static final UserSe ... -
图片中文路径 不能显示处理
2011-04-09 22:33 1558在html页面中,引用了带汉字的图片,形如<img sr ... -
带html标签的中英文字符串截取之完整性
2011-04-05 23:50 1730需求一: 数据库中存储有一段带html标签的文字,如: & ... -
java清除html格式
2011-04-05 23:42 881s = s.replaceAll("(\\<\ ... -
jbpm步骤
2011-03-13 15:41 643理解JBPM(java Business Proces ... -
内部类
2010-09-20 13:21 930package InnerClass; //内部类可以继 ... -
Java的特点
2010-09-19 22:08 786Java是一种简单、动态、 ... -
初识JUnit
2010-09-19 00:09 1040package com.hugui.juint4; ... -
PropertiesEditor插件
2010-09-16 15:20 1304PropertiesEditor插件是一个资源文件编码转换插件 ... -
java对国际化的支持
2010-09-01 00:17 10251.缺省的Locale是由操作系统决定的 2.Locale是由 ... -
java中hashcode()和equals()的详解【转】
2010-08-31 18:30 7721. 首先equals()和hashcode() ... -
关于SimpleDateFormat
2010-08-30 23:39 770首先说明一下关键的参数 G 年代标志符 y 年 M ... -
Java中精确计算的一个类用BigDecimal
2010-08-30 23:38 992/* * * 如果需要精确计算,非要用St ... -
如何去掉JFrame上的最大化最小化和关闭按钮 【转】
2010-08-30 23:36 1286关键字: 去掉jframe上的 ... -
InvocationTargetException
2010-08-30 23:32 2882InvocationTargetException 是一种包装 ... -
Class.forName()
2010-08-30 23:30 818在Java开发特别是数据库开发中,经常会用到Class.for ... -
字符码转换(一个小程序)
2010-08-30 23:29 959package test; public cla ...
相关推荐
jsp跳转getRequestDispatcher()和sendRedirect()的区别.docx
主要介绍了jsp跳转getRequestDispatcher()和sendRedirect()的区别,需要的朋友可以参考下
跳转方式 ...运用forward方法只能重定向到同一个Web应用程序中的一个资源。...Forward代码中的"/uu"则代表相对与WEB应用的路径。如http://localhost:8080/Test应用(则提交至http://localhost:8080/Test/uu);
页面之间的跳转可以通过两种方式实现:forward和sendRedirect。 forward:可以在JSP页面中使用,可以在Servlet中实现。 在JSP页面中使用”目标文件”/>,例如要跳转到userlist.jsp,可以写: 代码如下: ”userlist...
<一>——forward与sendRedirect的区别 ********************************************************************************* forward:可以在JSP页面中使用,可以在Servlet中实现。 在JSP页面中使用目标文件"/>...
request.getRequestDispatcher("/admin/register.jsp").forward(request,response); return; } //2.调用业务层完成注册操作并返回结果 User user = new User(userId,realName,pwd,age, Arrays.toString...
response.sendRedirect("userInfo"); //重定向的跳转方式为get(如果下一个servlet中没有get方法 将出现异常) 注意事项:a标签和重定向差不多 通过a标签进行跳转时也是调用doGet()方法 //从地址栏上方进入网页也是...
转发和重定向的区别 forward和redirect的区别 request.getRequestDispatcher()..forward(request,response);和 response.sendRedirect();的区别详解
response_sendRedirect和request_getRequestDispatcher()_forward的区别.
//sql语句,从user表中选择出username和userpassword与用户填入的一样的数据 String sql = "select * from user where username = '" + username + "' and userpassword = '" + userpassword + "'"; ResultSet ...
rd = sc.getRequestDispatcher("/index.jsp"); //定向的页面 rd.forward(request, response); } 通常在servlet中使用,不在jsp中使用。 2.response.sendRedirect() 是在用户的浏览器端工作,...
response sendRedirect "login jsp" ; }else{ response sendRedirect "error jsp" ; } }else if "login" equals action { t setPassword MD5 generateMD5 request...
request.getRequestDispatcher("providerAddxiugai.jsp").forward(request, response); }else if(canshu.equals("xiugaiok")){ int gysid=Integer.parseInt(request.getParameter("gysid")); String ...
// resp.sendRedirect(req.getContextPath()+"/main?action=welcome"); application.getRequestDispatcher("/WEB-INF/front/success.jsp").forward(req, resp); return; } req.setAttribute("err",...
if (session.getAttribute("person") == null) ... response.sendRedirect(request.getContextPath() + "/login.jsp"); }else{ request.getRequestDispatcher("fall.jsp").forward(request, response); }
转载在Servlet中进行服务端跳转的方式:request.getRequestDispatcher("success.html").forward(request response);在Servlet中进行客户端跳转的方式: response.sendRedirect("fail.html");
RequestDispatcher rd = getServletContext().getRequestDispatcher(/test/test2.jsp); rd.forward(request, response); %> 2. response.sendRedirect 重定向 <% response.sendRedire
request.getRequestDispatcher("index.jsp").forward(request, resposne); 外部跳转: response.sendRedirect("index.jsp"); 内部跳转是一次请求和一次响应 外部跳转是两次请求和两次响应 ServletContext:Servlet上...
// response.sendRedirect("/sshmvc/listUser.jsp"); request.getRequestDispatcher("/listUser.jsp") .forward(request, response); } } import="java.util.*,java.util.*,com.sshmvc.*,...