JSP的request对象实例详解

JSP的request对象实例详解

一 request对象定义

客户端的请求信息被封装在request对象中,通过它才能了解客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求之前,该对象一直有效。 

二 request对象方法

JSP的request对象实例详解

 

三 实例

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link type="text/css" href="https://www.jb51.net/styles.css" > --> </head> <body> <h1>request内置对象</h1> <% request.setCharacterEncoding("utf-8"); //解决中文乱码问题,无法解决URL传递中文出现的乱码问题。 request.setAttribute("password", "123456"); %> 用户名:<%=request.getParameter("username") %><br> 爱好 :<% if(request.getParameterValues("favorite")!=null) { String[] favorites = request.getParameterValues("favorite"); for(int i=0;i<favorites.length;i++) { out.println(favorites[i]+"&nbsp;&nbsp;"); } } %> <br> 密码:<%=request.getAttribute("password") %><br> 请求体的MIME类型:<%=request.getContentType() %><br> 协议类型及版本号: <%=request.getProtocol() %><br> 服务器主机名 :<%=request.getServerName() %><br> 服务器端口号:<%=request.getServerPort() %><BR> 请求文件的长度 :<%=request.getContentLength() %><BR> 请求客户端的IP地址:<%=request.getRemoteAddr() %><BR> 请求的真实路径:<%=request.getRealPath("request.jsp") %><br> 请求的上下文路径:<%=request.getContextPath() %><BR> </body> </html>

 四 运行效果

JSP的request对象实例详解

五 小知识点

1、解决URL传递中文参数乱码问题

修改D:\apache-tomcat-7.0.81\conf中的server.xml <Connector port="8888" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjgyy.html