JSP九大内置对象总结(二)

上一篇文章列举了JSP内置对象的作用范围和当中的五个内置对象,本篇文章将进一步列举request和response内置对象的使用方法。

 

一、request对象

1、作用范围:请求有效。在一起完整的请求响应过程中,即request scope。

2、说明:代表由用户提交请求而出发的request对象。

3、request对象可以获取的信息:

(1)建立HTML表单: <form  action=“action” method=“method” name=“name”>。。。</form>

(2)用REQUEST对象处理:JSP页面将数据存放在request对象里,并将该请求传递到下一个页面,下一个页面访问request对象,处理上一个JSP页面传递过来的数据。

(3)通过超链接来传递:<a   href="aaa.jsp?aa=aa&bb=bb&cc=cc">aaa</a> 

(4)通过jsp动作标签param来进行传递

4、下面对四种可以通过request传递信息的方式进行一一说明。

(1)建立HTML表单: 

// login.jsp
<%@ page language="java" import="java.util.*" pageEncoding=“GB2312"%>
<html>
  <head><title>DengLu</title></head>
  <body>
    <form action="treat.jsp" method="post" >
     用户名<input type="text" name="name" />
     密  码<input type="password" name="password" />
     <input type="submit" value="登陆“ />
    </form>
  </table>
  </body>
</html>
  获取表单提交信息并处理: 
// treat.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
  <head><title>treat.jsp</title></head>
  <body>
   <%
   String name=request.getParameter("name");
   String passw=request.getParameter("password");
   %>
   您好!<%=name%><br />
   您的密码是<%=passw%>
  </body>
</html>
  (2)用REQUEST对象处理: 
// index.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
请求有效 - 使用请求request.setAttribute()
<p/>
<%
request.setAttribute("name", "yyw");  
request.setAttribute("password", "123");
%>
<jsp:forward page="requestScopeGet.jsp"/>
</body>
</html>
 由于index.jsp中采用的是forwadr跳转,所以会在同一次请求当中,所以requestScopeGet.jsp中可以获取index.jsp中保存的数据。 
// requestScopeGet.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head><title>请求有效</title></head>
<body>
请求有效 - 使用请求request.getAttribute()
<br />
<%
   String name = (String) request.getAttribute("name");  
   String password = (String) request.getAttribute("password");
   out.println("姓名 = " + name);
   out.println("密码 = " + password);
%>
</body>
</html>
 (3)通过超链接来传递: 
// chaolianjie.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
<h1>连接有效 - 使用超链接a</h1>
<a href="requestScopeGet.jsp?name=yyw&password=123">连接</a>
</body>
</html>
 处理提交请求的页面与(2)中的requestScopeGet.jsp一模一样。 

(4)通过jsp动作标签param来进行传递:

仔细观察,可以发现jsp:forward的结束放在了jsp:param的后面。

// jspparam.jsp
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
<title></title>
</head>
<body>
   	<jsp:forward page="header.jsp">
   	<jsp:param name="selected" value="welcome"/>
   	</jsp:forward>
    <h1>北京</h1>
</body>
</html>

 处理页面:

// Header.jsp
<html> …..
<h1><font color="#ff0000">beijin</font></h1>
<%
String name=request.getParameter("selected");
%>
<%=name%>

 (5)如何接受多个属性值的属性

比方说我们在表单中使用到了复选框checkbox,那么我们如何在表单提交页面获取用户选择的多个值呢,我们可以来模拟实现以下。 

// info.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
<h1>用户信息</h1>
<form action="requestScopeGet2.jsp" method = "post">
姓 名:<input type="text" name="name" /><br />
密 码:<input type="password" name="password" /><br />
爱 好:<input type="checkbox" name="habit" value="read" checked />看书
	   <input type="checkbox" name="habit" value="sport" />运动
	   <input type="checkbox" name="habit" value="code" />编程
	   <br />
<input type="submit" />
</form>
</body>
</html>

 requestScopeGet2.jsp中的处理内容如下: 

// requestScopeGet2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
请求有效 - 使用请求request.getAttribute()
<br />
<%
   String name = request.getParameter("name");  
   String password = request.getParameter("password");
   String[] userHabits=request.getParameterValues("habit");
   out.println("姓名 = " + name);
   out.println("密码 = " + password);
   for(int i=0;i<userHabits.length;i++){
%>
	第<%=i%>项<%=userHabits[i]%><br>
<%}%>
<br />
</body>
</html>

(6)通过request对象获取服务器和浏览器相关信息

 主要代码:

 <%
//服务器
String localName=request.getLocalName();
String serverName = request.getServerName();
String localAddr=request.getLocalAddr();
int localPort=request.getLocalPort();
int serverPort = request.getServerPort();%>
<p>
<b>服务器</b>:<%= localName %><br/>
<b>服务器端IP</b>:<%= localAddr %><br/>
<b>服务器端口</b>:<%= localPort %><br />
</p>
<%//客户端信息
String remoteHost=request.getRemoteHost();
String remoteAddr=request.getRemoteAddr();
int remotePort=request.getRemotePort();%>
<p>
<b>浏览器端</b>:<%= remoteHost %><br/>
<b>浏览器端IP是</b>:<%= remoteAddr %><br/>
<b>浏览器端口</b>:<%= remotePort %><br/>
</p>
<%//协议相关
String pro=request.getProtocol();
String pro1=request.getScheme();
int len=request.getContentLength();
String type=request.getContentType();
String charEncode=request.getCharacterEncoding();
%>
<b>协议版本</b>:<%= pro %><br/>
<b>协议</b>:<%= pro1 %><br/>
<b>数据内容长度</b>:<%= len %><br/>
<b>数据类型</b>:<%= type %><br/>
<b>字符编码方式</b>:<%= charEncode %><br/>

 效果:


 

二、response对象

1、作用范围:page scope

2、说明:负责将服务器端的数据发送回浏览器的客户端。主要用于向客户端发送数据,如Cookie、HTTP文件头等信息。

3、用法实例:

(1)设置返回给浏览器的页面格式,如下例将页面设置为word类型

 

<%@ page contentType="text/html;charset=GB2312" %>
<html>
	<body>
	<h2>response对象 - setContentType方法</h2>
	将当前页面转换为word文档
	<% response.setContentType("application/msword;charset=GB2312");%> 
	</body>
</html>
 运行效果: 


 

(2)设置header。如下实现了页面自动刷新 

<%@ page contentType="text/html;charset=GBK" %>
<html>
<head>
  <title>response对象 - 处理HTML Header</title>
</head>
<body>
<h2>response - no-cache</h2>
<% 	
	if (request.getProtocol().compareTo("HTTP/1.0") == 0) 		
		 response.setHeader("Pragma", "no-cache"); 	
	else if (request.getProtocol().compareTo("HTTP/1.1") == 0) 		
		response.setHeader("Cache-Control", "no-cache"); 	
	response.setDateHeader("Expires", -1); 
%>
<h2>response - 自动刷新</h2>
当前时间:
<% 
   response.setHeader("Refresh","3");
   out.println(""+new java.util.Date());
 %>
</body>
</html>
 设置setHeader的第一参数为“refresh”的时候,第二个参数可以在指定时间内跳转到其他页面,如下:response.setHeader("Refresh","3");设为 

response.setHeader("Refresh","3;url=http://localhost:8080/try/response1.jsp");

(3)设置重定向页面

<%@page import="java.util.*"%>
<html>
<head><title>response应用</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
Date nowdate=new Date();
int hour;
hour=nowdate.getHours();
if (hour>=8 & hour<=17) {
       response.sendRedirect(“business.jsp?time=”+hour);//跳转
}else{
       response.sendRedirect(“privacy.jsp?time=”+hour);//跳转
}
%>
</body>
</html>

 business.jsp代码:

// business.jsp
<%@page import="java.util.*"%>
<html>
<head>
<title>response应用</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FF11FF">
现在是办公时间
<%
    String nowdate = request.getParameter("time");
%>
<%= nowdate%>
</body>
</html>

 private.jsp代码:

// private.jsp
<%@page import="java.util.*"%>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>response应用</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FF11FF" text="#000000">
现在是私人时间
<%
   String nowdate = request.getParameter("time");
%>
<%= nowdate%>
</body>
</html>

 (4)添加Cookie

// Addcookies.jsp
<body>
<%
//以获取到的请求参数为值,创建一个Cookie对象
Cookie c = new Cookie("username" , "yyw");
//设置Cookie对象的生存期限,24小时
c.setMaxAge(24 * 3600);
//向客户端增加Cookie对象
response.addCookie(c);
%>
</body>

 readCookies.jsp代码:

// readCookies.jsp
<body>
<%
//获取本站在客户端上保留的所有Cookie
Cookie[] cookies = request.getCookies();
//遍历客户端上的每个Cookie
for (Cookie c : cookies){
	//如果Cookie的名为username,表明该Cookie是我们需要访问的Cookie
	if(c.getName().equals("username")){
		out.println(c.getValue());
		return;
	}
}
out.println("没有这个用户");
%>
</body>

 

关于request和response的用法先介绍到这了,下一篇将会详细介绍session和pageContext对象的使用和探究内置对象为何不用声明便可直接使用。

 

 谢谢您的关注和阅读,文章不当之处还请您不吝赐教~~~微笑微笑微笑

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值