学习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{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//String getMethod():获取请求方式:GETString method = req.getMethod();System.out.println(method);//String getContextPath():获取虚拟目录(项目访问路径):/request-demoString contextPath = req.getContextPath();System.out.println(contextPath);//StringBuffer getRequestURL():获取URL(统一资源定位符):http://localhost:8080/request-demo/req1StringBuffer requestURL = req.getRequestURL();System.out.println(requestURL.toString());//String getRequestURI():获取URI(统一资源标识符):/request-demo/req1String requestURI = req.getRequestURI();System.out.println(requestURI);//String getQueryString():获取请求参数(GET):username=zhangsanString queryString = req.getQueryString();System.out.println(queryString);//----------------//获取请求头:user-agent:获取浏览器版本信息String header = req.getHeader("user-agent");System.out.println(header);}@Overrideprotected 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{@Overrideprotected 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:zhangsanSystem.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);}@Overrideprotected 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:zhangsanSystem.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>