13 Servlet - session Case 2: User Log Home Show usernames and logout

Remarks

We use the original  Section 11 code improvements, add a user login name to show their capabilities and add functionality to the home page after log off.

Design ideas

Home page for the user name design

  • In LoginServlet, we determined that the user account password is correct, prior to the jump page, create a user object and add a session to session, the session can get the object in the home.
  • Similarly, in the case of three days free landing, jump cookieServlet before the home page, create a session and user objects are added to the session, the session can get the object in the home.

Logout design

Add a form, add a button input when the user clicks the button, jump to a new Servlet - LogoutServlet, it needs to be done is simple:

  • Gets the session object
  • Failure forced the session object
  • Redirection Home

Look at the effect

 Demo Description: Visit the home page MainServlet, display null-> access index.jsp Jump Login -> Home MainServlet after landing the jump to see the user name -> Logout, see null-> index.jsp click access to log in again to jump directly to the home page (3 days free landing)

Code

LoginServlet

the LoginServlet class the extends HttpServlet {public 
	Private static Final Long serialVersionUID = 1L; 
    Private static String username; 
    Private static String password; 
    Private Flag static boolean = false; // account password is correct 
	@Override 
		protected void Service (REQ the HttpServletRequest, HttpServletResponse RESP) throws ServletException, IOException { 
			// set request encoding 
			req.setCharacterEncoding ( "UTF-. 8"); 
			// set the response code 
			resp.setContentType ( "text / HTML; charset = UTF-. 8"); 
			// data acquisition request 
				username = req .getParameter ( "the uname"); 
				password = req.getParameter ( "pwd"); 
			// process the request  
				LoginService ls = new LoginServiceImpl ();
				User user = ls.checkLoginService (username, password );
			//response
			IF (null! = the User) { 
				// Create a cookie, to achieve three days free landing 
					// We do not direct deposit account password, but keep the user's uid 
					Cookie c = new new Cookie ( "uid", user.getUid () + " "); 
					// set is valid for three days 
					c.setMaxAge (24 * 3600 *. 3); 
					// Sets the specified URL 
					c.setPath (" / 200 222-CookieLogin / CK "); 
					// add 
					resp.addCookie (c); 
					// create a session 
					the HttpSession hs = the req.getSession (); 
					// set the age 
					// save the user object in the session 
					hs.setAttribute ( "the user", the user); 
				resp.sendRedirect ( "main"); 
				return;  
			} the else { 
				// request forwarding 
				req.setAttribute ( "msg", "account number or password is incorrect");
				REQ. the getRequestDispatcher ( "Page") Forward (REQ, RESP);. 
				return; 
			} 
		} 
}

  

cookieServlet

class CookieServlet the extends the HttpServlet {public 
	@Override 
	protected void-Service (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException { 
		// set request encoding 
		req.setCharacterEncoding ( "UTF-. 8"); 
		// set the response type and encoding 
		resp.setContentType ( "text / HTML; charset = UTF-. 8"); 
		// acquisition request 
			// determines whether a cookie 
			cookies [] = req.getCookies CKS (); 
			// process the request 
			iF (CKS = null) {! 
				// from the cookie acquired UID 
				String UID = ""; 
				for (C cookies: CKS) { 
					IF ( "UID" .equals (c.getName ())) { 
						UID = c.getValue (); 
					}  
				} 
				// check whether the user is present (use uid)
				CookieService CookieServiceImpl new new LS = (); 
				User u = ls.checkUidService(uid);
				if(null!=u) {
					//设置session
					HttpSession hs = req.getSession();
					//将用户数据保存到session
					hs.setAttribute("user", u);
					//跳转主页
					resp.sendRedirect("main");
					return;
				}else {
					req.getRequestDispatcher("/page").forward(req, resp);
					return;
				}
			}else {
				req.getRequestDispatcher("/page").forward(req, resp);
				return;
			}
	}
}

  

Home MainServlet

the MainServlet the extends the HttpServlet {class public 
	@Override 
	protected void-Service (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException { 
		// set request encoding format 
		req.setCharacterEncoding ( "UTF-. 8"); 
		// set the response type and encoding 
		resp.setContentType ( "text / HTML; charset = UTF-. 8"); 
		// Get session object 
		the HttpSession the req.getSession HS = (); 
		// get the user target 
		the user user = (the user) hs.getAttribute ( "user"); 
		// obtain a user name 
		String username = null; 
		IF (user = null!) { 
			username = user.getUsername (); 
		} 
		. resp.getWriter () Write ( "<HTML>");  
		. resp.getWriter () Write ( "< head> "); 
		resp.getWriter ().write("</head>");
		. resp.getWriter () Write ( "< body> "); 
		resp.getWriter ().write ( "Welcome" + username + "management system <hr>");
		resp.getWriter().write("<form action='logout' method='get'>");
		resp.getWriter().write("<input type='submit' value='退出登录'><br>");
		resp.getWriter().write("</form>");
		resp.getWriter().write("</body>");
		resp.getWriter().write("</html>");
	}
}

  

LogoutServlet

public class LogoutServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//强制时效session
		HttpSession hs = req.getSession();
		hs.invalidate();
		//重定向到主页
		resp.sendRedirect("main");
	}
}

  

Guess you like

Origin www.cnblogs.com/Scorpicat/p/12356249.html