• 网页跳转


    这里不谈前端跳转的方法(如:a链接,或js控制跳转),我们讲后端跳转方法,后端页面跳转由servlet控制,大致可分为服务器端跳转和客户端跳转两种

    服务器端跳转

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      </head>
      
      <body>
           <form action="servlet/MyServlet" method="post">
            <input type="submit" value="提交"/>
        </form>
      </body>
    </html>

    Myservlet.java

    public class MyServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doPost(request, response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                 //在JSP内置对象session和request都有这个方法,用来保存数据,然后还可以用getAttribute方法来取出
                request.setAttribute("name", "dingshaohua");
                /*是采用请求转发方式,在跳转页面的时候是带着原来页面的request和response跳转的,request对象始终存在,不会重新创建。
                  即面跳转语句,执行这句时,跳到result.jsp  这个jsp页面,同时把request 请求和response 转发到result.jsp即所跳转到的页面 */
                request.getRequestDispatcher("../Result.jsp").forward(request,response);
        }
    }
    /*功能说明: 在servlet中实现服务器端跳转,并向跳转页面传递参数*/

    Result.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      </head>
      <body>
           接受到的数据是:<%=(String)request.getAttribute("name")%>
      </body>
    </html>

    客户端跳转

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      </head>
      <body>
           <form action="servlet/MyServlet" method="post">
            <input type="submit" value="提交"/>
        </form>
      </body>
    </html>

    Myservlet.java

    public class MyServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doPost(request, response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                String name="dingshaohua";
                response.sendRedirect("../Result.jsp?Myname="+name);
        }
    }
    /*功能说明: 在servlet中实现服务器端跳转,并向跳转页面传递参数*/

    Result.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      </head>
      <body>
           接受到的数据是:<%=request.getParameter("Myname")%>
      </body>
    </html>

    需要注意的是:

    ①response.sendRedirect(url)-----重定向到指定URL          request.getRequestDispatcher(url).forward(request,response) -----请求转发到指定URL
    ②response.sendRedirect(url)-----是客户端跳转              request.getRequestDispatcher(url).forward(request,response) -----是服务器端跳转

    ③response.sendRedirect(url)跳转到指定的URL地址后,上个页面(跳转之前的原来页面)中的请求全部结束,原request对象将会消亡,数据将会消失。紧接着,当前新页面会新建request对象,即产生新的request对象。
    【详细过程:redirect 会首先发一个response给浏览器,然后浏览器收到这个response后再发一个requeset给服务器,服务器接收后发新的response给浏览器。这时页面从浏览器获取来的是一个新的request。这时,在原来跳转之前的页面用request.setAttribute存的东西都没了,如果在当前的新页面中用request.getAttribute取,得到的将会是null。】
    request.getRequestDispatcher(url).forward(request,response)是采用请求转发方式,在跳转页面的时候是带着原来页面的request和response跳转的,request对象始终存在,不会重新创建。
    【详细过程:forward 发生在服务器内部, 是在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面收到的request不是从浏览器直接发来的,可能是在转页时己经用request.setAttribute在request里放了数据,在转到的页面就可以直接用request.getAttribute获得数据了。】


    ④使用response.sendRedirect()地址栏中的网址将改变 ,使用request.getRequestDispatcher().forward(request,response)地址栏中的网址保持不变。

    ⑤使用response.sendRedirect()时如果需要传递参数,那只能在url后加参数,如:url?id=1,而不能通过request或response方式。
    使用request.getRequestDispatcher().forward(request,response)如果需要传递参数,可以在程序内通过response.setAttribute("name",name)来传至下一个页面.而不能在后面带参数传递,比如servlet?name=frank这样不行。

    ⑥运用sendRedirect()方法可以让你重定向到任何URL,而forward()方法只能重定向到同一个Web应用程序中的某个资源。
    表单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。
    ⑦运用HttpServletResponse接口的sendRedirect()方法
    sendRedirect()是在用户的浏览器端工作,同时它可以重定向至不同的主机上,sendRedirect()可以重定向有frame的jsp文件。
    假设转发代码包含于注册的servlet-url为/ggg/tt;jsp为/ggg/tt.jsp。
    绝对路径:response.sendRedirect("http://www.brainysoftware.com")发送至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);

    ⑧运用RequestDispatcher接口的Forward()方法
    forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,
    只有在客户端没有输出时才可以调用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

    参考:

    http://blog.csdn.net/zumtz/article/details/6636639
    http://blog.sina.com.cn/s/blog_703074da01012oc5.html
    http://jorton468.blog.163.com/blog/static/72588135201101711810508/
    http://blog.csdn.net/ammmd/article/details/3990671
    http://zhidao.baidu.com/link?url=1b3qScDypjUuEh2PIrv8c1pB9QAHBDqDeFLnYrh8bIZojP5AV1uZPP8YaLo62trYCEFm4IfLSAbfJVSngw328a

  • 相关阅读:
    [转]maven插件的开发
    开发建议
    [转]利用maven的surefire插件实现单元测试与集成测试
    sonar-maven-plugin错误2
    20145218张晓涵_网络欺诈技术防范
    20145218张晓涵_信息搜集与漏洞扫描
    20145218张晓涵_Exp5 MSF基础应用
    20145218张晓涵 PC平台逆向破解_advanced
    20145218张晓涵 恶意代码分析
    20145218 免杀原理与实践
  • 原文地址:https://www.cnblogs.com/dshvv/p/5144567.html
Copyright © 2020-2023  润新知