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>
posted @ 2024-11-12 21:26  张黎健  阅读(2)  评论(0编辑  收藏  举报