开始界面HTML,跳转到判断是否有Cookie的Servlet.
SessionStartPage.html
Insert title here
判断是否有Cookie的Servlet.此处获取Cookie的数据。
package cc.session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
/**
* Servlet implementation class Index
*/
@WebServlet("/sessionindex.do")
public class Index extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Index() {
super();
// TODO Auto-generated constructor stub
}
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
Cookie[] cookies=request.getCookies();//查询浏览器存储的本地Cookie,返回结果是数组。
if(cookies!=null)
{
for(Cookie cookie:cookies)
{//Cookie由name和value两部分组成
String name=cookie.getName();
String value=cookie.getValue();
if("user".equals(name)&&"Daniel".equals(value)) {
request.setAttribute(name,value );
request.getRequestDispatcher("/sessionuser.view").forward(request, response);
return;
}
}
}
response.sendRedirect("SessionLogin.html");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
processRequest(request,response);
}
}
登录界面的Html
SessionLogin.html
Insert title here
登录的Servlet,此处创建Cookie,并将用户存到Cookie中。
package cc.session;
import java.io.IOException;
import java.net.URLDecoder;
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;
/**
* Servlet implementation class SessionLogin
*/
@WebServlet("/sessionlogin.do")
public class SessionLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SessionLogin() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
request.setCharacterEncoding("UTF-8");
String user=URLDecoder.decode(request.getParameter("user"),"UTF-8");
String password=request.getParameter("password");
String autologin=request.getParameter("autologin");
if(user.equals("Daniel")&&password.equals("2018Daniel"))
{
if("auto".equals(autologin))
{//自动登录时,创建Cookie,保存用户名。
Cookie cookieLogin=new Cookie("user", user);
cookieLogin.setMaxAge(3*60);//设置保存时间为3分钟
response.addCookie(cookieLogin);
}
request.setAttribute("user", user);
request.setAttribute("password",password);
request.getRequestDispatcher("sessionuser.view").forward(request, response);
}
else {
response.sendRedirect("SessionLogin.html");
}
}
}
获取Cookie和登录成功后的Servlet
package cc.session;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SessionUser
*/
@WebServlet("/sessionuser.view")
public class SessionUser extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SessionUser() {
super();
// TODO Auto-generated constructor stub
}
private void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
String user=(String) request.getAttribute("user");//forward过来的request,需要通过getAttribute方法获得,不是getParameter
PrintWriter out= response.getWriter();
if(user==null)
{
response.sendRedirect("SessionLogin.html");
}
else {
out.println("Servlet User "+user+" Welcome!");
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
//processRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
processRequest(request,response);
}
}