@WebServlet("/example")

2025-03-25 12:02:27
导读 public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse...

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!

");

}

}

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。