标签:oid patch 服务端 login 方法 登陆 epo inpu 请求转发
void addCookie(Cookie cookie);服务端向客户端增加一个cookie对象
void sendRedirect(String location) throws IOExcetion:页面跳转的一种方法
void setContentType(String type):设置服务端响应的编码
login.jsp—->check.jsp->success.jsp 判断登录是否合法
1.login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="check.jsp" method="post">
用户名:<input type="text" name="uname"><br /> 密码:<input
type="password" name="upwd"><br /> <input type="submit"
value="登录"></br>
</form>
</body>
</html>
2.check.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
if (name.equals("zs") && pwd.equals("abc")) {
response.sendRedirect("success.jsp");
} else {
out.print("用户名或者密码错误!");
}
%>
</body>
</html>
3.success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
登陆成功!
<br /> 欢迎您:
<%
String name = request.getParameter("uname");
out.print(name);
%>
</body>
</html>
经发现重定向方式会导致数据丢失:
checks.jsp修改:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
if (name.equals("zs") && pwd.equals("abc")) {
//response.sendRedirect("success.jsp");
request.getRequestDispatcher("success.jsp").forward(request,response);
} else {
out.print("用户名或者密码错误!");
}
%>
</body>
</html>
经运行发现,地址栏没有变。
a.地址栏是否转变,请求不变(内部转发看不见),重定改变
b.是否保留第一次请求时的数据,请求保留,重定不保留
c.请求次数 请求转发1次,重定2次
请求转发:
重定向:
就是找错人了,再次请求到success.jsp:
张三 --> 【服务窗口a --> 服务窗口b】
张三 --> 【 服务窗口a】 --> 去张三找【服务窗口b】
| ^
|-----------------------------------|
cookie由服务端生成,再给客户端保存,相当于本地缓存作用
客户端->服务端(hello.MP4,zs/abc)将(MP4,zc/abc)返回到客户端,提高效率,但安全性低
1.包含key:value键值对
2.javax.servlet.http.Cookie
3.方法:
response.addCookie(Cookie cookie)
页面跳转(转发或者重定向)
客户端2获取cookie:request.getCookie();
注意:
a.服务端增加cookie:response对象 客户端获取对象:request
b,必须一次将全部的cookie拿到
reponse_addCookie.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//服务端
Cookie cookie1 = new Cookie("name", "zs");
Cookie cookie2 = new Cookie("pwd", "abc");
response.addCookie(cookie1);
response.addCookie(cookie2);
//页面跳转到客户端
response.sendRedirect("result.jsp");
%>
</body>
</html>
result.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//客户端
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
out.print(cookie.getName() + "->" + cookie.getValue() + "<br/>");
}
%>
</body>
</html>
标签:oid patch 服务端 login 方法 登陆 epo inpu 请求转发
原文地址:https://www.cnblogs.com/julyzqy/p/13073223.html