HttpServletRequest的獲取前端傳遞的參數並且請求轉發


寫在前面:

web服務器接收到客戶端的http請求,針對這個請求,分別建立了一個代表請求的HttpServletResponse對象;和一個代表響應的HttpServletRequest對象。

如果要獲取客戶端請求過來的參數:HttpServletRequest對象。

如果要給客戶端響應一些信息:找HttpServletResponse對象。

HttpservletRequest:代表客戶端的請求,用戶通過Http協議訪問服務器,Http請求中的所有信息會被封裝到HttpServletRequest,通過這個HttpServletRequest方法,獲得客戶端所有信息。

HttpServletRequest的獲取前端傳遞的參數並且請求轉發

1、 創建項目

     

 1 package com.wang.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.FileInputStream;
 8 import java.io.IOException;
 9 import java.net.URLEncoder;
10 import java.util.Arrays;
11 
12 public class LoginServlet extends HttpServlet {
13     @Override
14     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15         //解決后台接收中文亂碼問題
16         req.setCharacterEncoding("utf-8");
17         resp.setCharacterEncoding("utf-8");
18 
19         String username = req.getParameter("username");
20         String password = req.getParameter("password");
21         String[] hobbys = req.getParameterValues("hobbys");
22         System.out.println("===================================");
23         System.out.println(username);
24         System.out.println(password);
25         System.out.println(Arrays.toString(hobbys));
26         System.out.println("===================================");
27         System.out.println(req.getContextPath());
28         //通過請求轉發
29         req.getRequestDispatcher("/success.jsp").forward(req,resp);
30     }
31 
32     @Override
33     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
34         doGet(req, resp);
35     }
36 }
View Code

2、 寫index.jsp

刪掉webapp下的index.jsp,自己重建

  

 

寫index.jsp

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>登錄</title>
 5 </head>
 6 <body>
 7 <h1>登錄</h1>
 8 <div>
 9     <form action="${pageContext.request.contextPath}/login" method="post">
10         用戶名:<input type="text" name="username"> <br>
11         密碼:<input type="password" name="password"> <br>
12         愛好:
13         <input type="checkbox" name="hobbys" value="女孩">女孩
14         <input type="checkbox" name="hobbys" value="代碼">代碼
15         <input type="checkbox" name="hobbys" value="唱歌">唱歌
16         <input type="checkbox" name="hobbys" value="電影">電影
17         <br>
18         <input type="submit">
19     </form>
20 </div>
21 </body>
22 </html>
View Code

3、 再寫個success.jsp

 

4、 注冊+映射

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
 5                       http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 6          version="4.0"
 7          metadata-complete="true">
 8     <!--注冊和映射-->
 9     <servlet>
10         <servlet-name>LoginServlet</servlet-name>
11         <servlet-class>com.wang.servlet.LoginServlet</servlet-class>
12     </servlet>
13     <servlet-mapping>
14         <servlet-name>LoginServlet</servlet-name>
15         <url-pattern>/login</url-pattern>
16     </servlet-mapping>
17 </web-app>
View Code

5、 指定Tomcat並運行

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2、 解決請求和響應中文亂碼問題

1 req.setCharacterEncoding("utf-8");
2 resp.setCharacterEncoding("utf-8");
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM