• Servlet两种路径跳转方式练习


    1.客户端请求重定向跳转方式

    2.服务器内部跳转方式


    练习前提:

    1. 需要少量的servlet知识(out、response对象)
    2. 需要少量的html知识
    3. 需要少量的jsp知识

    使用工具:myeclipse

    打开myeclipse,新建web project ,new 一个servlet

    以下是测试代码:
    ServletPD.java

    package sevlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     *author:恋晴
     *http://blog.csdn.net/qq_32953185
     */
    /**
     * Servlet implementation class ServletPD
     */
    @WebServlet("/ServletPD")
    public class ServletPD extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * @see HttpServlet#HttpServlet()
         */
        public ServletPD() {
            super();
            // TODO Auto-generated constru-                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ctor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //response.getWriter().append("Served at: ").append(request.getContextPath());
    //      response.setContentType("text/html;charset=utf-8");
    //      PrintWriter out=response.getWriter();
    //      out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
    //      out.println("<html>");
    //      out.println("<head><title>servlet!!!</title></head>");
    //      out.println("<body>");
    //      out.println("<h1>你好,我是servlet!<h1><br>");
    //      out.println("</body>");
    //      out.println("</html");
    //      out.flush();
    //      out.close();
            doPost(request,response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //doGet(request, response);
            //1.请求重定向方式跳转
            response.sendRedirect(request.getContextPath()+"/test.jsp");
    
            //2.服务器内部跳转
            //request.getRequestDispatcher("test.jsp").forward(request,response);
        }
    
    }
    

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
    
        <title>servlet路径跳转test~</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      <body>
        <a href="/ServletPathDirection/ServletPD">访问ServletPD,跳转到test.jsp</a><br>
      </body>
    </html>
    

    test.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
    
        <title>跳转页面</title>
    
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
    
      <body>
        <h1>这里是test.jsp页面~</h1><br>
      </body>
    </html>
    

    总结:

    1.客户端请求重定向方式,重定向后浏览器URL变成定向后的页面地址。

    2.服务器内部跳转方式,重定向后浏览器地址栏URL不变。

  • 相关阅读:
    用算法合并数组
    Redis各个数据类型的使用场景
    seesion工作原理
    自删除道指令程序
    uva 1335
    《生活在Linux中》之:使用Bash就是使用Emacs
    手动配置S2SH三大框架报错(三)
    数据和C
    IOS之【地图MapKit】
    我工作这几年(五)-- Android学习4.5月总结(一)
  • 原文地址:https://www.cnblogs.com/famine/p/9124730.html
Copyright © 2020-2023  润新知