下面是一个session的应用的小例子,是用来注销登录的
登陆界面的代码:
User.java:
便将用户信息加入到session中。
LoginServlet:
欢迎界面,可以从session中拿出用户姓名信息显示在主页上
index.jsp:
//注销时使用的Servlet,将session中加入的用户信息清除
登陆界面的代码:
login.html:
<!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:
package 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,如果用户账号密码输入正确,就让用户跳转到欢迎界面,顺
便将用户信息加入到session中。
LoginServlet:
package 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是我自己写的假数据库,里面有一
些User的账号密码信息,是内部类,在下面有
for(User user:list){
if(user.getUsername().equals(username)&&user.getPassword
().equals(password)){
request.getSession().setAttribute("user", user);//登陆成
功,向session中存入一个登陆标记
response.sendRedirect("/day07/index.jsp");
return;
}
}
out.write("用户名或者密码错误!");
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
doGet(request,response);
}
}
//模拟数据库(上面提到的)
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:
<%@ 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>
//注销时使用的Servlet,将session中加入的用户信息清除
LogoutServlet:
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);
}
}
这样,当用户点击退出登录的时候,session中就没有用户的相应信息,用户再次进入主页或登录页面的时候,就会显示用户没有登录。