这篇“java重定向和转发的区别是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“java重定向和转发的区别是什么”文章吧。
定义
首先来看两者的javadoc。
sendRedirect()
/*** Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI.If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.*/public void sendRedirect(String location) throws IOException;
重定向是向客户端发送一个指定URL的临时重定向的响应。
forward()
/*** Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.The request and response parameters must be either the same objects as were passed to the calling servlet's service method .*/public void forward(ServletRequest request, ServletResponse response);
转发,则是将一个请求转到服务器的另一个资源。在处理完初步请求另外的资源之后生成响应。
定义基本说明了转发操作为何可以保持request内的parameter,attribute这些值都可以保留,而重定向操作却会丢弃的原因:
转发是在服务端完成的,并没有经过客户端
转发整个操作完成后才生成响应
重定向是服务端向客户端发送指定的URL
重定向是在客户端完成的
我们再来看Tomcat内部,对于两者是怎样一种实现方式。
2. 容器实现
我们在servlet内部一般对于这两者的使用形式也相当直观,例如对于hello.jsp的请求:
sendRedirct方法
response.sendRedirect("/hello.jsp");
此时,内部的处理方式如下:
public void sendRedirect(String location, int status) throws IOException { // Generate a temporary redirect to the specified location try { String absolute = toAbsolute(location); setStatus(status); //这里,重定向是返回302状态码以及Location和对应的url setHeader("Location", absolute); } catch (IllegalArgumentException e) { setStatus(SC_NOT_FOUND); }}
即根据Location,浏览器最终再发起新的请求,最终展现在浏览器中的即为新请求的URL,也就是大家常说的重定向会显示最终的URL。有上这些并不能造成重定向操作将之前request中已经绑定的一系列parameter和attribute丢掉。最根本的原因是一个请求完整处理完成之后,整个请求会有一个release的过程,即CoyoteAdapter的service方法执行完的finally块中执行release这一过程,基本如下:finally {
if (!comet && !async || error.get()) { request.recycle(); //注意这两行代码 response.recycle(); } }
具体request的recycle部分代码如下:
/** * Release all object references, and initialize instance variables, in preparation for reuse of this object. */public void recycle() { attributes.clear(); requestedSessionId = null; requestedSessionURL = false; parameterMap.clear(); pathParameters.clear();}
我们看到用于存储setAttribute方法设置的和setParameter方法设置的数据在这里都clear掉了。这也是重定向不能够保留数据的真正原因。forward方法forward方法一般使用如下:request.getRequestDispatcher("/hello.jsp").forward(request, response);
forward方法内部最终会调用dispatcher的doForward方法
void doForward(ServletRequest request, ServletResponse response){ // Set up to handle the specified request and response State state = new State(request, response, false); wrapResponse(state); ApplicationHttpRequest wrequest = (ApplicationHttpRequest) wrapRequest(state);
String contextPath = context.getPath(); HttpServletRequest hrequest = state.hrequest; if (hrequest.getAttribute( RequestDispatcher.FORWARD_REQUEST_URI) == null) { wrequest.setAttribute(RequestDispatcher.FORWARD_PATH_INFO,hrequest.getPathInfo()); wrequest.setAttribute(RequestDispatcher.FORWARD_QUERY_STRING, hrequest.getQueryString());} wrequest.setContextPath(contextPath); wrequest.setRequestURI(requestURI); wrequest.setServletPath(servletPath); wrequest.setPathInfo(pathInfo); if (queryString != null) { wrequest.setQueryString(queryString); wrequest.setQueryParams(queryString); } processRequest(request,response,state); //进行第二个资源的请求 }}
第二个资源的请求处理与一般的请求处理类似,只是在第一个请求之上,并没有返回响应时继续发起第二个请求,此时第一个请求的各类参数会继续向后传递,最终数据全部处理完成之后,整个响应发送回客户端。此时上面的release流程也依然会走,但并没有什么影响,毕竟第二个资源已经请求处理完成。
而由于浏览器发请求的时候是一个固定的URL,整个重定向是服务端内部进行的,浏览器并没有感知到,因此也不会显示出来。
以上就是关于“java重定向和转发的区别是什么”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4585957/blog/4583305