前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JSP 内置对象request常见用法

JSP 内置对象request常见用法

作者头像
全栈程序员站长
发布2022-08-12 15:45:09
6870
发布2022-08-12 15:45:09
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

1、访问请求参数

代码语言:javascript
复制
<a href="login.jsp?name=张三&sex=man&id=">传递参数</a>

login.jsp关键 代码

代码语言:javascript
复制
<%= "name:"+new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") %><br>
<%= "sex:"+request.getParameter("sex") %><br>
<%= "id:"+request.getParameter("id") %><br>
<%= "pwd:"+request.getParameter("pwd") %><br>

说明:如果指定的参数不存在,将返回null;如果指定了参数名,但未指定参数值,将返回空的字符串”。

因为所有的request请求都是ISO-8859-1的,而在页面采用的是utf-8编码方式,所以在遇到中文时,将获取到的数据通过String的构造方法使用指定的编码类型重新构造一个String对象。

2、在作用域中管理属性

在进行请求转发时,需要把一些数据传递到转发后的页面进行处理,这时,需要用request对象的setAttribute方法将数据保存在request范围内的变量中。

代码语言:javascript
复制
	<%
	    try {
	        int money = 100;
	        int number = 0;
	        request.setAttribute("result", money / number);
	    } catch (Exception e) {
	        request.setAttribute("result", "很抱歉,页面产生错误!");
	    }
	%>
	
	<jsp:forward page="deal.jsp" />
代码语言:javascript
复制
<%= request.getAttribute("result").toString() %><br>

由于getAttribute方法返回值是Object,需要调用toString方法转换为字符串。

3、获取cookie

cookie是小段文本信息,在网络服务器上生成,并发送给浏览器。通过cookie可以标识用户身份,记录用户名和密码,跟踪重复用户等。以键值对形式保存在客户机的某个目录下。

代码语言:javascript
复制
	<%
	    Cookie[] cookies = request.getCookies();
	    String user = "";
	    String date = "";
	    if (cookies != null) {
	        for (int i = 0; i < cookies.length; i++) {
	            if (cookies[i].getName().equals("mrCookie")) {
	                user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);
	                date = cookies[i].getValue().split("#")[1];
	            }
	        }
	    }
	    if ("".equals(user) && "".equals(date)) {
	%>
	游客您好,欢迎您初次光临!
	<form action="deal.jsp" method="post">
		请输入姓名:<input name="user" type="text" value=""> <input
			type="submit" value="确定">
	</form>

	<%  } else { %>
	
	欢迎
	<b> <%=user%></b> 再次光临
	<br>

	<% }%>

deal.jsp:

代码语言:javascript
复制
	<%
	            request.setCharacterEncoding("GB18030");
				String user = URLEncoder.encode(request.getParameter("user"), "utf-8");
				Cookie cookie = new Cookie("mrCookie", user + "#" + new Date().toLocaleString());
				cookie.setMaxAge(60 * 60 * 24 * 30);
				response.addCookie(cookie);
	%>

	<script type="text/javascript">
		window.location.href = "index.jsp"
	</script>

4、获取客户端信息

JSP 内置对象request常见用法
JSP 内置对象request常见用法
代码语言:javascript
复制
<br> 客户提交信息的方式:<%=request.getMethod() %>
<br> 使用的协议:<%=request.getProtocol() %>
<br> 客户端地址:<%=request.getRequestURL() %>
<br> 客户端ip地址:<%=request.getRemoteAddr() %>
<br> 服务器端口号:<%=request.getServerPort() %>
<br> 服务器名称:<%=request.getServerName() %>
<br> 客户端主机名:<%=request.getRemoteHost() %>
<br> 客户端所请求的脚本文件的文件路径:<%=request.getServletPath() %>
<br> Http协议定义的文件头信息Host的值:<%=request.getHeader("host") %>
<br> Http协议定义的文件头信息User-Agent的值:<%=request.getHeader("user-agent") %>
<br> Http协议定义的文件头信息accept-language的值:<%=request.getHeader("accept-language") %>
<br> 请求文件的绝对路径:<%=request.getRealPath("index.jsp") %>
JSP 内置对象request常见用法
JSP 内置对象request常见用法

5、显示国际化信息

浏览器可以通过accept-language的HTTP报头向Web服务器指明它所使用的本地语言。java.util.Local类型对象封装了一个国家和国家所使用的一种语言。示例如下:

代码语言:javascript
复制
	<%
	    Locale locale = request.getLocale();
	    String str = "";
	    if (locale.equals(Locale.US)) {
	        str = "Hello,welcome to access our company's web!";
	    }
	    if (locale.equals(Locale.CHINA)) {
	        str = "您好,欢迎访问我们公司网站!";
	    }
	%>

	<%=str%>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131784.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档