The user name and password form will be submitted to page 2 (JSP is the servlet submitted to servlet). Page 2 will determine the correctness of the user name and password and then determine the direction of the jump

Use href hyperlinks to mark client jumps

Use javascript client to jump

Submit form client jump

Jump to the client using the Response object

Always use forward to mark a server jump

Jump to the server using the requestDispatcher class

Page 1

<%response.setCharacterEncoding("utf-8");
  response.setHeader("iso-8859-1"."utf-8");
  request.setCharacterEncoding("utf-8");
      String name = request.getParameter("name");
      String psd  = request.getParameter("psd");
      if(name.equals("admin")&&psd.equals("123")){%>
      <script type="text/javascript">
      window.location="login03.jsp";
      </script>
     <% }
     else{  %>
           <script type="text/javascript">
           window.location="http://www.baidu.com";
           alert(window.location.href);
           </script>
     <% }%>
Copy the code

Page 2

<%response.setCharacterEncoding("utf-8");
  response.setHeader("iso-8859-1"."utf-8");
  request.setCharacterEncoding("utf-8");
      String name = request.getParameter("name");
      String psd  = request.getParameter("psd");
      if(name.equals("admin")&&psd.equals("123")){%>
      <script type="text/javascript">
      window.location="login03.jsp";
      </script>
     <% }
     else{  %>
           <script type="text/javascript">
           window.location="http://www.baidu.com";
           alert(window.location.href);
           </script>
     <% }%>
Copy the code

After page 1 is submitted to page 2, the Java code is responsible for judging and then jumping through the JS code

 

Page 3

<%response.setCharacterEncoding("utf-8");
  response.setHeader("iso-8859-1"."utf-8");
  request.setCharacterEncoding("utf-8");
      String name = request.getParameter("name");
      String psd  = request.getParameter("psd");
        if(name.equals("admin")&&psd.equals("123")){
        response.sendRedirect("http://www.baidu.com");001 / / path
        return;
        }
        else{
        response.sendRedirect("login01.jsp");
        return; } % >Copy the code

\

Page 1 is submitted to page 3 and redirected via the SendreDirect () method of the Response object

The URL in sendreDirect () can take eg sendredirect(” URL? name=”+name);

Redirects with the sendredirect parameter. 2. Note that this sentence is usually followed by a return; (reason: Redirection via sendreDirect does not happen until after the page has been processed, For example, in the last example, after the user name and password of the page are input, click Submit and then the next page is judged and processed, that is, the page 3 does not need to be output and cannot be typed. There is no return and other output is meaningless, and sometimes Fang er reports errors because of meaningless output.)

There are two ways to jump to a page using a Response object, one of which is the setHeader() method

The code on page 3 should be modified:

<%response.setCharacterEncoding("utf-8");
  response.setHeader("iso-8859-1"."utf-8");
  request.setCharacterEncoding("utf-8");
      String name = request.getParameter("name");
      String psd  = request.getParameter("psd");
        if(name.equals("admin")&&psd.equals("123")) {// response.sendRedirect("http://www.baidu.com");
     // return;
     response.setHeader("Refresh"."1. url=http://www.baidu.com");
        }
        else{
       // response.sendRedirect("login01.jsp");
       // return;
       response.setHeader("Refresh"."1. url=login01.jsp"); } % >Copy the code

\

Responsetheader (“Refresh”,” seconds to wait; Url = absolute path or relative path “); The same is true for sendreDirect. In both cases, the absolute path and the relative path wait 1 second to jump

 

Response redirect and forward redirect

Response: 1 Go to the target page after executing all the codes. 2 The URL of the browser will change after going to the target page. 3 Redirection in the browser.

Forward jump: 1 directly jump to the target page after the code is no longer executed 2 jump to the target page after the URL remains the same although the URL is displayed index1.jsp but in fact you see index2. The contents of JSP 3 are redirected on the server side 4 cannot be redirected to a page on another server

 

RequestDispatcher class

Basic method

RequestDispatcher rd = request. GetRequestDispatcher (” target page “);

rd.forward(response,request);

The basic principle for page jumps is the same with RequestDispatcher jumps and forward action tags.

RequestDispatcher and Response redirect difference: 1 after executing all the code, jump to the target page. 2 After jumping to the target page, the URL remains the same. Although the URL displays index1.jsp, in fact, the content of index2.jsp you see is redirected on the server side

The difference between the RequestDispatcher and forward action tag is that the code after the forward 1 action tag does not execute immediately to the target page

1 Run all codes including all codes after the RequestDispatcher before redirecting to the destination page. 2 Specify an absolute path to the destination page

 

Servlets get the RequestDispatcher object in two ways: by calling the getRequestDispatcher(String Path) method of the ServletContext, the path parameter specifies the path of the target component. The difference between the two methods is that the path parameter of ServletRequest must be an absolute path, while the path parameter of ServletRequest can be an absolute or relative path. The absolute path is the path starting with the symbol “/”, which indicates the URL entry of the current Web application.

 






\