📄 cookieservlet.java
字号:
package com.learnweblogic.examples.ch3;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class CookieServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { handleRequest(req, res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { handleRequest(req, res); } public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException { // Get the session if one exists HttpSession session = req.getSession(false); // Try to retrieve the cookie from the request. boolean cookieFound = false; Cookie cookie = null; Cookie[] cookies = req.getCookies(); if (null != cookies) { // Look through all the cookies and see if the // cookie with the login info is there. for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; if (cookie.getName().equals("LoginCookie")) { cookieFound = true; break; } } } // Logout action removes session, but the cookie remains if (null != req.getParameter("Logout")) { if (null != session) { session.invalidate(); session = null; } } if (null == session) { // If the user is not logged in String loginValue = req.getParameter("login"); boolean isLoginAction = (null == loginValue) ? false : true; if (isLoginAction) { // User is logging in if (cookieFound) { // If the cookie exists update the value only if changed if (!loginValue.equals(cookie.getValue())) { cookie.setValue(loginValue); res.addCookie(cookie); } } else { // If the cookie does not exist, create it and set value cookie = new Cookie("LoginCookie", loginValue); cookie.setMaxAge(Integer.MAX_VALUE); System.out.println("Add cookie"); res.addCookie(cookie); } // create a session to show that we are logged in session = req.getSession(true); session.setAttribute("login",loginValue); req.setAttribute("login",loginValue); displayLogoutPage(req, res); } else { // Display the login page. If the cookie exists, set login if (cookieFound) { req.setAttribute("login", cookie.getValue()); } displayLoginPage(req, res); } } else { // If the user is logged in, display a logout page String loginValue = session.getAttribute("login").toString(); req.setAttribute("login",loginValue); displayLogoutPage(req, res); } } public void displayLoginPage( HttpServletRequest req, HttpServletResponse res) throws IOException { String login = (String) req.getAttribute("login"); if (null == login) { login = ""; } res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html><body>"); out.println( "<form method='POST' action='" + res.encodeURL(req.getRequestURI()) + "'>"); out.println( "login: <input type='text' name='login' value='" + login + "'>"); out.println( "password: <input type='password' name='password' value=''>"); out.println("<input type='submit' name='Submit' value='Submit'>"); out.println("</form></body></html>"); } public void displayLogoutPage( HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html><body>"); out.println("<p>Welcome " + req.getAttribute("login") + "</p>"); out.println( "<form method='GET' action='" + res.encodeURL(req.getRequestURI()) + "'>"); out.println( "Click <a href='" + res.encodeURL(req.getRequestURI()) + "'>here</a> to reload this page.<br>"); out.println("<input type='submit' name='Logout' value='Logout'>"); out.println("</form></body></html>"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -