reponse对象JSP学习

1、response代表服务器对客户端的相应。

 ①而大部分时候,程序无须使用response来响应客户端请求,因为有个更简单的响应对象----out,它代表页面输出流,直接使用out生成响应更简单。

②但是out是JspWriter的实例,JspWriter是Writer的子类,Writer是字符流,无法输出非字符内容。

③假如需要在JSP页面中动态生成一副位图或者输出一个PDF文档,使用out作为响应对象将无法完成,此时必须使用response作为响应输出。

④另外还可以使用response来重定向请求,以及用与向客户端增加Cookie

2.response响应生成非字符响应

①对于需要生成非字符响应的情况,就应该使用response来响应客户端请求。

②response是HttpServletResponse接口的实例,该接口提供了一个getOutputStream()方法,使用客户端生成一张图片

<%-- 通过contentType属性响应数据是图片 --%>
<%@page contentType="image/jpeg" language="java" errorPage="" %>
<%@page import="java.awt.image.*,javax.imageio.*,java.io.*,java.awt.*" %>
<%
BufferedImage image = new BufferedImage(340,160,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.fillRect(0, 0, 400, 400);
g.setColor(new Color(255,0,0));
g.fillArc(20,20,100,100,30,120);

g.setColor(new Color(0,255,0));
g.fillArc(20,20,100,100,150,120);

g.setColor(new Color(0,0,255));
g.fillArc(20,20,100,100,270,120);

g.setColor(new Color(0,0,0));
g.setFont(new Font("Arial Black",Font.PLAIN,16));

g.drawString("red:climb",200,60);
g.drawString("green:swim",200,100);
g.drawString("blue:jump",200,140);
g.dispose();
ImageIO.write(image, "jpg", response.getOutputStream());
%>

使用临时生成图片的方式就可以非常容易地实现网页上的图像验证码功能。不仅如此,使用respinse生成非字符响应还可以直接生成PDF文件、Excel文件,这些文件可直接作为报表使用。


3、重定向 

①重定向是response的另外一个用处,与forward不同的是,重定向会丢失所有请求参数和request范围的属性,因为重定向将生成第二次请求,与前一次请求不在同一个request范围内,所以发送一次请求参数和request范围的属性会全部丢失

②HttpServletResponse提供一个sendRedirect(String path)方法,该方法用于重定向到path资源,即重新向path资源发送请求。

执行重定向动作时,地址栏的URL会变成重定向的目标URL

重定向会丢失所有的请求参数,使用重定向的效果:他们都可将地址栏里重新输入新地址再按回车的效果完全一样,即发送了第二次请求


3.增加Cookie

①Cookie通常用于网站记录客户的某些信息,比如客户的用户名和客户的喜好等。一旦用户下次登录,网站可以获取到客户的相关信息,根据这些客户信息,网站可以对客户提供更友好的服务。

②Cookie与session的不同之处:session会随浏览器的关闭而失效,但Cookie会一直存放在客户端机器上,除非超出Cookie的生命期限

③由于安全性的原因,使用Cookie'客户端浏览器必须支持Cookie才行。客户端浏览器完全可以设置禁用Cookie才行

④增加Cookie也是使用response内置对象完成的,response对象提供的方法

void addCookie(Cookie cookie):增加Cookie

在增加Cookie之前,必须先创建Cookie对象。增加Cookie步骤:

>>创建Cookie实例,Cookie的构造器为Cookie(String name,String value);

>>设置Cookie的生命期限,即该Cookie在多长时间内有效(必须设置生存期限,否则Cookie将会随浏览器的关闭而自动消失)

>>向客户端写Cookie


<%@page contentType="text/html;charset=GBK" language="java" errorPage=""%>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>增加Cookie</title>
</head>
<body>
<%
String name = request.getParameter("name");
Cookie c = new Cookie("username",name);
c.setMaxAge(24*3600);
response.addCookie(c);
%>
</body>
</html>
读取Cookie

<%@page contentType="text/html;charset=GBK" language="java" errorPage=""%>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Trasitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-trasitional.dtd">
<html>
<head>
<title>readCookie  page jsp</title>
</head>

<body>
<%
Cookie[] cs = request.getCookies();
for(Cookie c:cs){
	if(c.getName().equals("username")){
		out.println(c.getValue());
	}
}

%>
</body>

</html>
⑤默认情况下,Cookie值不允许出现中文字符,如果需要值为中文的内容的Cookie可以借助java.net.URLEncoder先对中文字符进行编码,将编码后的结果设为Cookie值,当程序要读取Cookie时,则应该先读取,然后使用java.net.URLDecorder对其进行解码。

存入中文Cookie

<%@page contentType="text/html;charset=GBK" language="java" errorPage=""%>
<%@page import="java.net.*" %>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>增加Cookie</title>
</head>
<body>
<%
String name = request.getParameter("name");
Cookie c = new Cookie("username",name);
c.setMaxAge(24*3600);
response.addCookie(c);
String namecn = request.getParameter("namecn");
out.println(namecn + "<br/>");
byte[] namecnBytes =  namecn.getBytes("ISO-8859-1");
String namecnnormal = new String(namecnBytes,"UtF-8");
out.println(namecnnormal + "<hr/>");
Cookie c1 = new Cookie("cnname",URLEncoder.encode(namecnnormal,"GBK"));
c1.setMaxAge(24*3600);
response.addCookie(c1);
%>
</body>
</html>


<%@page contentType="text/html;charset=GBK" language="java" errorPage=""%>
<%@page import="java.net.*" %>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Trasitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-trasitional.dtd">
<html>
<head>
<title>readCookie  page jsp</title>
</head>

<body>
<%
Cookie[] cs = request.getCookies();
for(Cookie c:cs){
	if(c.getName().equals("username")){
		out.println(c.getValue());
	}else if(c.getName().equals("cnname")){
		out.println(URLDecoder.decode(c.getValue(),"GBK"));
	}
}

%>
</body>

</html>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值