jsp中cookie

请求重定向,可以跳转到工程外部资源,url发生改变

Cookie:
存储cookie

	Cookie c1 = new Cookie("uname", lname);
	Cookie c2 = new Cookie("upass", lpass);
	response.addCookie(c1);//向客户端存储cookie
	response.addCookie(c2);//向客户端存储cookie	
	c1.setMaxAge(9999);//存储cookie需要设置存活时间,不设置时间无法存储

读取cookie
读取本网站客户端中所有cookie信息:

	Cookie[] cs =  request.getCookies();
	for(Cookie item : cs )
	{
		if(item.getName().equals("uname")){
			uname = item.getValue();
		}
		if(item.getName().equals("upass")){
			upass = item.getValue();
		}
	}

.getName() 读取某个cookie的name值
.getValue() 读取某个cookie的value值
所有网页内容如下:
index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	请登录:<a href="login.jsp">登录</a>
</body>
</html>

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String uname = "";
	String upass = "";
	//读取cookie
	Cookie[] cs =  request.getCookies();
	for(Cookie item : cs )
	{
		if(item.getName().equals("uname")){
			uname = item.getValue();
		}
		if(item.getName().equals("upass")){
			upass = item.getValue();
		}
	}

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

		<form action="/20181130/LoginServlet" method="post">
			用户名:<input type="text" name="lname" value="<%=uname %>"><br>
			密码:<input type="password" name="lpass" value="<%=upass %>"><br>
			<input type="checkbox" name="rem">记住我
			<input type="submit" value="登录">
 		</form>
</body>
</html>

LoginServlet:

package com.neuedu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("tuf-8");
		String lname = request.getParameter("lname");
		String lpass = request.getParameter("lpass");
		request.setAttribute("lname", lname);
		
		if(lname.equals("admin")&&lpass.equals("123"))
		{
			
			//获得rem是否勾选
			String rem = request.getParameter("rem");
			System.out.println(rem);
			if(rem != null)
			{
				//如果rem不等于null,说面选中了记住,存储cookie
				Cookie c1 = new Cookie("uname", lname);
				Cookie c2 = new Cookie("upass", lpass);
				response.addCookie(c1);//向客户端存储cookie
				response.addCookie(c2);//向客户端存储cookie
				c1.setMaxAge(9999);
			}
			
			request.getRequestDispatcher("/welcome.jsp").forward(request, response);
		}
		else{
			//请求重定向
			response.sendRedirect("/login.jsp");
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <% 
    	String lname = (String)request.getParameter("lname");
    %>
<!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>
	<div>
		welcome,<%=lname %>
	</div>
</body>
</html>

cookie的销毁:
没有办法直接销毁cookie,可以通过设置同名的cookie来覆盖原有的cookie,起到销毁的作用,cookie设置存活的时间不能设置为0,有些情况可能0代表永久,1即可,存活1秒

猜你喜欢

转载自blog.csdn.net/ykallan/article/details/84671582