javaweb -jsp九大对象(2)

response对象:

对客户的请求做

出动态的响应,向客户端发送数据。

提供方法:

void addCookie(Cookie cookie);服务端向客户端怎加cookie对象
void sendRedirect(String location);throws IOException  页面跳转的一种方式(重定向)
void setContent Type(String type);(设置服务器响应的编码(设置服务器contentType类型)

示例:登陆
login.jsp -> check.jsp ->success.jsp

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="check.jsp" method = "post">
		用户名:<input type="text" name="uname"><br/>
		密码:<input type="text" name="upassword"><br/>
		提交:<input type="submit" value="登陆"><br/>
	</form>
</body>
</html>

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("uname");
		String password = request.getParameter("upassword");
		if(name.equals("yuanqi3366")&&password.equals("123456")){
			//response.sendRedirect("success.jsp");重定向。导致数据丢失
			//请求转发,可以获取到数据
			request.getRequestDispatcher("success.jsp").forward(request,response);
		}else{
			out.print("用户名或密码错误");
			
			response.sendRedirect("login.jsp");
		}
	%>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	登陆成功!
	用户名:
	<%
		String name = request.getParameter("uname");
		out.print(name);
	%>
</body>
</html>

tip++

请求转发与重定向的区别
                    
                                                        请求转发                            重定向
1.地址栏是否会改变                   			      不变(check。jsp)          		 改变(success。jsp)

2.是否保留第一次请求时的数据    						    保留      		   不保留(让客户端再次请求时不包含原来的数据)

3.请求次数                                           	   1                2(让客户端再次请求指定的页面)
                                                                                                          
                           			 (客户端请求一次,服务端跳转到success)   (login(1)->check->login(2)->success)

4.跳转位置:                      			            服务端                        客户端发出的第二次跳转

Cookie(非内置对象)

Cookie(客户端,不是内置对象):Cookie是由服务端产生,在发送给客户端保存,相当于本地缓存的作用
:客户端->服务器(hello.mp4)->Cookie(hello.mp4)->客户端
作用:提高访问服务器效率,但是安全性较差。

方法:

Cookie:key=value
javax.servlet.http.Cookie
public Cookie(String name, String value)
String getName():获取name
String getValue();获取value
void setMaxAge(int expiry);最大有效期(秒)

用法:

服务端准备Cookie
response.addCookie(Cookie cookie);
页面跳转(转发,重定向)
客户端获取Cookie:request.getCookies();

a.服务端增加Cookie(response对象);客户端获取对象(request对象)
b.不能直接获取某一个单独对象,只能一次将全部cookie拿到

通过f12可以发现 除了自己设置的Cookie外,还有一个name为JSESSIONID的cookie

建议cookie只保存英文数字,否则需要进行编码解码

示例:使用cookie实现记住用户名功能

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
		String name;
	%>
	<%
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for(Cookie cookie : cookies){
				if(cookie.getName().equals("uname")){
					name = cookie.getValue();
				}
			}
		}
	%>
	<form action="check.jsp" method = "post">
		用户名:<input type="text" name="uname" value = "<%=name==null?"":name%>"><br/>
		密码:<input type="text" name="upassword"><br/>
		提交:<input type="submit" value="登陆"><br/>
	</form>
</body>
</html>

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("uname");
		String password = request.getParameter("upassword");

		//将用户名加入到Cookie
		Cookie cookie = new Cookie("uname",name);
		cookie.setMaxAge(300);
		response.addCookie(cookie);
		response.sendRedirect("result.jsp");
	%>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值