JavaWeb case (three): use response to achieve redirection

I. Introduction

In fact, the first two javaweb cases are just for us to practice hands, review the basics. It doesn’t matter if you don’t master it, but redirection is the most important technology. We need to focus on the redirection technology .

Second, implement redirection

After a web resource receives a client request, he will notify the client to access another web resource. This process is redirection .

Common scenarios:

  • User login
void sendRedirect(String var1) throws IOException;

Code test:

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        /*
        resp.setHeader("Location","/r/img");
        resp.setStatus(302);
         */
        
        resp.sendRedirect("/r/img"); //重定向
    }

As a result, the access effect in the browser is as follows: the path automatically jumps from /red to /img

Insert picture description here

3. Interview questions: the difference between redirection and request forwarding

Same point:

  1. Page will jump

difference:

  1. When the request is forwarded, the url will not change
  2. When redirecting, the URL address bar will change

Fourth, use redirection technology to make a small demo

At the very beginning we have to make preparations:
first create a Maven project, import jsp dependencies, the code is as follows:

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>

step:

  1. Create a class that inherits the HttpServlet class and override doGet() and doPost(). The url-pattern (access path) of this class is /login :
package com.xu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RequestTest extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        //处理请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username + ":" + password);
        //重定向的时候,一定要注意路径问题,否则就会404
        resp.sendRedirect("/r/success.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req,resp);
    }
}

  1. According to the code, we can see that our redirected resource path is /success.jsp . The code of the jsp page is as follows:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>

  1. We add a form form to the index.jsp (the page accessed for the first time after the server is turned on) as a login page. The effect is as follows:

Insert picture description herecode show as below:

<html>
<body>
<h2>Hello World!</h2>

<%--这里提交的路径,需要寻找到项目的路径--%>
<%--${pageContext.request.contextPath}代表当前项目--%>
<form action="${pageContext.request.contextPath}/login" method="get">
    用户名:<input type="text" name="username"> <br>
    密码:<input type="password" name="password"> <br>
    <input type="submit">
</form>
</body>
</html>

Finally, when we open the server, the login page will pop up. After submitting the form, the RequestTest resource will be accessed. Because the code in this class uses redirection technology to direct the page to the success.jsp page, the above is the display of the redirection technology.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46594796/article/details/109399932