1. 程式人生 > 其它 >HttpServletRequest應用之實現請求轉發

HttpServletRequest應用之實現請求轉發

宣告

本文部分內容參考自其他作者原創文章,僅供個人學習留檔,特此宣告

參考文章連結

(1條訊息) B站---【狂神說Java】JavaWeb入門到實戰---筆記_夜裡的雨的部落格-CSDN部落格_狂神說java筆記

實現請求轉發

1、程式碼

1.用於實現後臺獲取登入資訊及實現請求轉發的LoginServlet程式碼

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

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobbies");
        System.out.println("============");
        //後臺接收中文亂碼問題
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbies));
        System.out.println("============");
        System.out.println(req.getContextPath());
        //通過請求轉發
        //這裡的 / 代表當前專案的webapp
        req.getRequestDispatcher("/success.jsp").forward(req,resp);
    }

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

其中需要注意這行程式碼 req.getRequestDispatcher("/success.jsp").forward(req,resp);

這裡的 / 代表當前專案的webapp


2.用於註冊對映路徑的web.xml程式碼

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.xy.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

</web-app>

3.用於構造使用者登入頁面的index.jsp程式碼

<%@page contentType="text/html;" pageEncoding="UTF-8"%>
<html>
<head>
    <title>登入</title>
</head>
<body>

<h1>登入頁面</h1>

<div>
    <form action="${pageContext.request.contextPath}/login" method="post">
        使用者名稱 : <input type="text" name="username"> <br>
        密碼 : <input type="password" name="password"> <br>
        愛好 :
        <input type="checkbox" name="hobbies" value="打遊戲">打遊戲
        <input type="checkbox" name="hobbies" value="敲程式碼">敲程式碼
        <input type="checkbox" name="hobbies" value="唱歌">唱歌
        <input type="checkbox" name="hobbies" value="看電影">看電影

        <br><br>
        <input type="submit">
    </form>
</div>

</body>
</html>

其中需要注意的是這行程式碼 action="${pageContext.request.contextPath}/login

${pageContext.request.contextPath} 代表當前專案的網址 http://localhost:8080/request


4.請求轉發指向頁面的success.jsp程式碼

<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
    <head>
       <title>Title</title>
       </head>
    <body>
       <h1>登陸成功</h1>
    </body>
</html>

2、程式碼測試

  • 完善使用者資訊並提交

  • 提交後成功轉向success.jsp網址由於是請求轉發,url不會被改變(307)

  • 觀察IDEA控制檯,得到使用者資訊