您的位置:首页技术文章
文章详情页

JSP的request对象实例详解

【字号: 日期:2022-06-07 11:57:33浏览:88作者:猪猪

JSP的request对象实例详解

一 request对象定义

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

二 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+"/";%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <base href="<%=basePath%>" rel="external nofollow" >    <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 rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >    --> </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>

四 运行效果

五 小知识点

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"/>

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签: JSP