public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 🌟 HttpServletRequest对象详解 🌟
// 在Servlet中,HttpServletRequest对象是处理HTTP请求的核心。
// 它包含了客户端请求的所有信息,如参数、头信息和会话状态。
// 📝 获取请求参数
String param = request.getParameter("key");
System.out.println("Parameter: " + param);
// 🔑 获取请求头信息
String userAgent = request.getHeader("User-Agent");
System.out.println("User-Agent: " + userAgent);
// 💻 获取请求URI
String uri = request.getRequestURI();
System.out.println("Request URI: " + uri);
// 🔐 获取会话管理
HttpSession session = request.getSession();
session.setAttribute("username", "JohnDoe");
// 📤 响应客户端
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("
HttpServletRequest Explained!
");}
}