JSP的request对象实例详解
Java  /  管理员 发布于 7年前   222
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="https:/article/<%=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="https:/article/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]+" "); } } %> <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"/>
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号