2024/11/12日工作總結

张黎健發表於2024-11-12

學習Request和Response:

點選檢視程式碼
package com.vivy.web;

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

@WebServlet("/req1")
public class RequestDemo1 extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //String getMethod():獲取請求方式:GET
            String method = req.getMethod();
            System.out.println(method);

            //String getContextPath():獲取虛擬目錄(專案訪問路徑):/request-demo
            String contextPath = req.getContextPath();
            System.out.println(contextPath);

            //StringBuffer getRequestURL():獲取URL(統一資源定位符):http://localhost:8080/request-demo/req1
            StringBuffer requestURL = req.getRequestURL();
            System.out.println(requestURL.toString());

            //String getRequestURI():獲取URI(統一資源識別符號):/request-demo/req1
            String requestURI = req.getRequestURI();
            System.out.println(requestURI);

            //String getQueryString():獲取請求引數(GET):username=zhangsan
            String queryString = req.getQueryString();
            System.out.println(queryString);

            //----------------
            //獲取請求頭:user-agent:獲取瀏覽器版本資訊
            String header = req.getHeader("user-agent");
            System.out.println(header);


        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //獲取post請求體:請求引數
            //獲取字元輸入流
            BufferedReader reader = req.getReader();
            //讀取資料
            String line = reader.readLine();
            System.out.println(line);
        }

}

點選檢視程式碼
package com.vivy.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

@WebServlet("/req2")
public class RequestDemo2 extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //System.out.println("get...");
            //獲取所有引數的Map集合
            Map<String, String[]> map = req.getParameterMap();
            for (String key : map.keySet()) {
                //username:zhangsan
                System.out.print(key+":");

                //獲取值
                String[] values = map.get(key);
                for (String value : values) {
                    System.out.print(value + " ");
                }

                System.out.println();
            }

            System.out.println("----------------");

            //根據key獲取引數值,陣列
            String[] hobbies = req.getParameterValues("hobby");
            for (String hobby : hobbies) {
                System.out.println(hobby);
            }

            //根據key 獲取單個引數值
            String username = req.getParameter("username");
            String password = req.getParameter("password");

            System.out.println(username);
            System.out.println(password);


        }

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

            this.doGet(req, resp);

            /*
            System.out.println("post...");
            //獲取所有引數的Map集合
            Map<String, String[]> map = req.getParameterMap();
            for (String key : map.keySet()) {
                //username:zhangsan
                System.out.print(key+":");

                //獲取值
                String[] values = map.get(key);
                for (String value : values) {
                    System.out.print(value + " ");
                }

                System.out.println();
            }

            System.out.println("----------------");

            //根據key獲取引數值,陣列
            String[] hobbies = req.getParameterValues("hobby");
            for (String hobby : hobbies) {
                System.out.println(hobby);
            }

            //根據key 獲取單個引數值
            String username = req.getParameter("username");
            String password = req.getParameter("password");

            System.out.println(username);
            System.out.println(password);
            */

        }

}

點選檢視程式碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action="/request-demo/req2" method="post">
  <input type="text" name="username"><br>
  <input type="password" name="password"><br>
  <input type="checkbox" name="hobby" value="1">游泳
  <input type="checkbox" name="hobby" value="2">爬山<br>
  <input type="submit">
</form>


</body>
</html>