1. 获取请求参数:
1.1 单个参数:
<%
String username = request.getParameter("username");
out.println("Username: " + username);
%>
1.2 多个参数:
<%
String[] hobbies = request.getParameterValues("hobby");
if (hobbies != null) {
for (String hobby : hobbies) {
out.println("Hobby: " + hobby);
}
}
%>
2. 获取请求头部信息:
<%
String userAgent = request.getHeader("User-Agent");
out.println("User-Agent: " + userAgent);
%>
3. 获取请求的HTTP方法:
<%
String method = request.getMethod();
out.println("HTTP Method: " + method);
%>
4. 获取会话信息:
<%
HttpSession session = request.getSession();
String username = (String)session.getAttribute("username");
out.println("Username from Session: " + username);
%>
5. 获取请求的URL和URI:
<%
StringBuffer requestURL = request.getRequestURL();
String requestURI = request.getRequestURI();
out.println("Request URL: " + requestURL);
out.println("Request URI: " + requestURI);
%>
这些代码片段展示了如何在JSP中使用request对象来获取客户端请求的一些信息。根据实际需求,你可以使用request对象的其他方法来访问更多的请求信息,比如获取请求参数、会话信息等。
转载请注明出处:http://www.zyzy.cn/article/detail/6906/JSP