package com.itheima.servlet;
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.IOException;
import java.util.Enumeration;
import java.util.Map;
@WebServlet("/servletDemo03")
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.根据名称获取数据
String username = req.getParameter("username");
System.out.println(username);
String password = req.getParameter("password");
System.out.println(password);
System.out.println("-------------------------------------------");
//2.根据名称获取所有信息
String[] hobbies = req.getParameterValues("hobby");
for (String hobby :hobbies){
System.out.println(hobby);
}
System.out.println("-------------------------------------------");
//3.获取所有名称
Enumeration<String> names = req.getParameterNames();
while (names.hasMoreElements()){
String name = names.nextElement();
String value = req.getParameter(name);
System.out.println(name+"," +value);
}
System.out.println("-------------------------------------------");
//4.获取所有参数的键值对
Map<String, String[]> map = req.getParameterMap();
for (String key :map.keySet()){
System.out.print(key+":");
String[] values = map.get(key);
for (String value :values){
System.out.print(value+" ");
}
System.out.println();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
servlet请求对象获取请求参数信息的方法
最新推荐文章于 2023-09-20 19:08:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)