文本浏览器

Servlet Cookie登录状态检测

发布者 : @gmail | 发布时间 : 2019-01-05 11:50:12
文章号 : 9 | 阅读量 : 37+1 | AAW值(?) : 0.43 (仅供参考)

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: adler
  Date: 2018/11/9
  Time: 7:09 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8language="java" %>
<
html>
<
head>
    <
title>用户登录系统</title>
</
head>
<
script type="text/javascript">
    
functiondirectUrl() {
        
window.location.href="/GetCookie";
        
return;
    }
</
script>
<
body onload="directUrl()">
<
h1>正在跳转...</h1>
</
body>
</
html>

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: adler
  Date: 2018/11/9
  Time: 7:09 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8language="java" %>
<
html>
  <
head>
    <
title>用户登录系统</title>
  </
head>
  <
body>
  <
form method="post"target="_blank" action="/Servlet">
    
用户名:<input type="text" name="name"/>
    <
br>
    
密码:<input type="password" name="passwd"/>
    <
button type="submit">登录</button>
  </
form>
  <
br>
  <
form method="post"target="_blank" action="/GetCookie">
    <
button type="submit">登录状态检测</button>
  </
form>
  <
form method="post"action="/ClearCookie">
    <
button type="submit">清除Cookie</button>
  </
form>
  <
form method="post"action="/ShowCookie">
    <
button type="submit">查看Cookie</button>
  </
form>
  </
body>
</
html>

red.jsp

<%--
  Created by IntelliJ IDEA.
  User: adler
  Date: 2018/11/10
  Time: 12:12 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8language="java" %>
<
html>
<
head>
    <
title>Title</title>
</
head>
<
body>
<
h1>欢迎回来!</h1>
<
href="/ClearCookie">注销登录</a>
</
body>
</
html>

login.jsp重定向到:>>GetCookie.java<<判断是否已经登录过(Cookie信息)

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;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
@WebServlet("/GetCookie")
public class GetCookieextends HttpServlet {
    
@Override
    
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader(
"Content-type","text/html;charset=UTF-8");
       PrintWriter out = resp.getWriter();
        Cookie cookie = 
null;
        Cookie[] cookies = 
null;
        cookies = req.getCookies();
        String title = 
"Delete Cookie Example";
        String docType = 
"<!DOCTYPE html>\n";
        out.println(docType +
                
"<html>\n+
                
"<head><title>" + title + "</title></head>\n+
                
"<body bgcolor=\"#f0f0f0\">\n");
        
if(cookies != null) {
            out.println(
"<h2>Cookie 名称和值</h2>");
            
for(int i = 0; i < cookies.length; i++) {
                cookie = cookies[i];
                out.print(
"名称:+ cookie.getName() + "");
                out.print(
"值:+ URLDecoder.decode(cookie.getValue(), "utf-8") + " <br/>");
                String str = URLDecoder.decode(cookie.getValue(),
"utf-8");
                
if (str.equals("null")) {
                    resp.sendRedirect(
"/login.jsp");//如果没登录跳转到主界面
                    
break;
                } 
else {
                    resp.sendRedirect(
"/red.jsp");
                    
break;
                }
            }
        }
    }
    
@Override
    
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }
}

ClearCookie.java

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ClearCookie")
public class ClearCookieextends Servlet{

    
@Override
    
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = 
new Cookie("usernm""null");
        resp.setHeader(
"Content-type","text/html;charset=UTF-8");
        resp.addCookie(cookie);
        System.
out.println("successful clear cookie");
        PrintWriter out = resp.getWriter();
        out.print(
"注销成功!<a href=\"login.jsp\">重新登录</a>");
    }
}

ShowCookie.java

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;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
@WebServlet("/ShowCookie")
public class ShowCookieextends HttpServlet {
    
@Override
    
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader(
"Content-type","text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Cookie cookie = 
null;
        Cookie[] cookies = 
null;
        cookies = req.getCookies();
        String title = 
"Delete Cookie Example";
        String docType = 
"<!DOCTYPE html>\n";
        out.println(docType +
                
"<html>\n+
                
"<head><title>" + title + "</title></head>\n+
                
"<body bgcolor=\"#f0f0f0\">\n");
        
if(cookies != null) {
            out.println(
"<h2>Cookie 名称和值</h2>");
            
for(int i = 0; i < cookies.length; i++) {
                cookie = cookies[i];
                out.print(
"名称:+ cookie.getName() + "");
                out.print(
"值:+ URLDecoder.decode(cookie.getValue(), "utf-8") + " <br/>");
            }
        }
    }
    
@Override
    
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

Servlet.java

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;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/Servlet")
public class Servletextends HttpServlet {
    
@Override
    
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    
@Override
    
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader(
"Content-type","text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print(
"<!DOCTYPE html><head><title>Test page</title></head>");
        out.print(
"<body><h1>Connect successfully!</h1>");
        String name = req.getParameter(
"name");
        String passwd = req.getParameter(
"passwd");
        Cookie cookieNM = 
new Cookie("usernm", name);
        cookieNM.setMaxAge(
60*60*24);
        resp.addCookie(cookieNM);
        out.print(
"<h2>姓名:"+ name + "</h2>");
        out.print(
"</body></html>");
    }
}






评论加载中...

+ 参与讨论