有关session的登录注销的一个小例子
生活随笔
收集整理的這篇文章主要介紹了
有关session的登录注销的一个小例子
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下面是一個(gè)session的應(yīng)用的小例子,是用來注銷登錄的
登陸界面的代碼:
login.html:
[java] view plaincopy<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">?? <html>?? ??<head>?? ????<title>login.html</title>?? ?????? ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">?? ????<meta?http-equiv="description"?content="this?is?my?page">?? ????<meta?http-equiv="content-type"?content="text/html;?charset=UTF-8">?? ?????? ????<!--<link?rel="stylesheet"?type="text/css"?href="./styles.css">-->?? ?? ?? ??</head>?? ???? ??<body>?? ????<form?action="/day07/LoginServlet"?method="post">?? ??????????用戶名:<input?text="text"?name="username"><br/>?? ??????????密碼:??<input?text="password"?name="password"><br/>?? ????<input?type="submit"?value="登陸">?? ??</body>?? </html>??
用戶的javaBean
User.java:
[java] view plaincopypackage?cn.edu.login;?? ?? ?? public?class?User?{?? ????private?String?username;?? ????private?String?password;?? ????? ????public?User(String?username,?String?password)?{?? ????????super();?? ????????this.username?=?username;?? ????????this.password?=?password;?? ????}?? ?????? ????public?User(){?? ????????super();?? ????}?? ????public?String?getUsername()?{?? ????????return?username;?? ????}?? ????public?void?setUsername(String?username)?{?? ????????this.username?=?username;?? ????}?? ????public?String?getPassword()?{?? ????????return?password;?? ????}?? ????public?void?setPassword(String?password)?{?? ????????this.password?=?password;?? ????}?? ?????? ?????? }??
處理登錄信息的Servlet,如果用戶賬號(hào)密碼輸入正確,就讓用戶跳轉(zhuǎn)到歡迎界面,順
便將用戶信息加入到session中。
LoginServlet:
[java] view plaincopypackage?cn.edu.login;?? ?? ?? import?java.io.IOException;?? import?java.io.PrintWriter;?? import?java.util.ArrayList;?? import?java.util.List;?? ?? ?? import?javax.servlet.ServletException;?? import?javax.servlet.http.HttpServlet;?? import?javax.servlet.http.HttpServletRequest;?? import?javax.servlet.http.HttpServletResponse;?? ?? ?? public?class?LoginServlet?extends?HttpServlet?{?? ?? ?? ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse??? ?? ?? response)?? ????????????throws?ServletException,?IOException?{?? ????????response.setCharacterEncoding("UTf-8");?? ????????response.setContentType("text/html;charset=UTF-8");?? ????????PrintWriter?out=response.getWriter();?? ?????????? ???????String?username=request.getParameter("username");???? ???????String?password=request.getParameter("password");?? ????????? ???????List<User>?list=Db.getAll();//這里的Db是我自己寫的假數(shù)據(jù)庫,里面有一?? ?? ?? 些User的賬號(hào)密碼信息,是內(nèi)部類,在下面有?? ???????for(User?user:list){?? ???????????if(user.getUsername().equals(username)&&user.getPassword?? ?? ?? ().equals(password)){?? ???????????????request.getSession().setAttribute("user",?user);//登陸成?? ?? ?? 功,向session中存入一個(gè)登陸標(biāo)記?? ???????????????response.sendRedirect("/day07/index.jsp");???????? ???????????????return;?? ???????????}?? ???????}?? ????????? ???????out.write("用戶名或者密碼錯(cuò)誤!");?? ????}?? ?? ?? ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse??? ?? ?? response)?? ????????????throws?ServletException,?IOException?{?? ????????doGet(request,response);?? ????}?? ?? ?? }?? ?? ?? //模擬數(shù)據(jù)庫(上面提到的)?? class?Db{?? ????private?static?List<User>?list=new?ArrayList<User>();?? ????static{?? ????????list.add(new?User("aaa","123"));?? ????????list.add(new?User("bbb","123"));?? ????????list.add(new?User("ccc","123"));?? ????}?? ?????? ????public?static?List?getAll(){?? ????????return?list;?? ????}?? }??
歡迎界面,可以從session中拿出用戶姓名信息顯示在主頁上
index.jsp:
[html] view plaincopy<%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%>?? <%?? String?path?=?request.getContextPath();?? String?basePath?=?request.getScheme()+"://"+request.getServerName?? ?? ?? ()+":"+request.getServerPort()+path+"/";?? %>?? ?? ?? <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">?? <html>?? ??<head>?? ????<title>My?JSP?'index.jsp'?starting?page</title>?? ????<mate?http-equiv="content-type"?content="text/html;charset=UTF-8">?? ??</head>?? ???? ??<body>?? ?????歡迎您!${user.username}??? ?????<br/>?? ??<a?href="/day07/login.html">登錄</a>??<a?href="/day07/LogoutServlet">退出?? ?? ?? 登錄</a>?? ?? ?? ??</body>?? </html>??
//注銷時(shí)使用的Servlet,將session中加入的用戶信息清除
package?cn.edu.login;?? ?? ?? import?java.io.IOException;?? ?? ?? import?javax.servlet.ServletException;?? import?javax.servlet.http.HttpServlet;?? import?javax.servlet.http.HttpServletRequest;?? import?javax.servlet.http.HttpServletResponse;?? import?javax.servlet.http.HttpSession;?? //完成用戶注銷?? public?class?LogoutServlet?extends?HttpServlet?{?? ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse??? ?? ?? response)?? ????????????throws?ServletException,?IOException?{?? ????????HttpSession?session=request.getSession(false);?? ????????if(session==null){?? ????????????response.sendRedirect("/day07/index.jsp");?? ????????????return;?? ????????}?? ?????????? ????????session.removeAttribute("user");?? ????????response.sendRedirect("/day07/index.jsp");?? ????}?? ?? ?? ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse??? ?? ?? response)?? ????????????throws?ServletException,?IOException?{?? ???????doGet(request,response);?? ????}?? ?? ?? }??
這樣,當(dāng)用戶點(diǎn)擊退出登錄的時(shí)候,session中就沒有用戶的相應(yīng)信息,用戶再次進(jìn)入主頁或登錄頁面的時(shí)候,就會(huì)顯示用戶沒有登錄。
登陸界面的代碼:
login.html:
[java] view plaincopy
用戶的javaBean
User.java:
[java] view plaincopy
處理登錄信息的Servlet,如果用戶賬號(hào)密碼輸入正確,就讓用戶跳轉(zhuǎn)到歡迎界面,順
便將用戶信息加入到session中。
LoginServlet:
[java] view plaincopy
歡迎界面,可以從session中拿出用戶姓名信息顯示在主頁上
index.jsp:
[html] view plaincopy
//注銷時(shí)使用的Servlet,將session中加入的用戶信息清除
LogoutServlet:
[java] view plaincopy這樣,當(dāng)用戶點(diǎn)擊退出登錄的時(shí)候,session中就沒有用戶的相應(yīng)信息,用戶再次進(jìn)入主頁或登錄頁面的時(shí)候,就會(huì)顯示用戶沒有登錄。
轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/acmman
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的有关session的登录注销的一个小例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servlet跳转到jsp页面的几种方法
- 下一篇: java获取当前日期时间代码