1.1 首先使用普通方法进行
1.1.1 顺便说一下案例环境
这里我直接使用的是上次Session登录注销的案例用jsp改写的所以我这就直接放代码了不进行说明了。。。对不起
- 数据准备
package com.itbear.domain;
public class User {
private String name;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
}
package com.itbear.domain;
import java.util.ArrayList;
import java.util.List;
public class UseDB {
private static List users = new ArrayList();
static {
users.add(new User("itcast","123456"));
users.add(new User("itheima","123456"));
users.add(new User("zhangsan","123456"));
}
public static List getUsers(){
return users;
}
}
- 首页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
这里是小熊先生的个人主页
先完成登录
登录
登录完成
登出
很重要的资源
- 登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
- 登录Servlet
package com.itbear.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.itbear.domain.UseDB;
import com.itbear.domain.User;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String pwd = request.getParameter("password");
PrintWriter out = response.getWriter();
User user = new User(name, pwd);
List users = UseDB.getUsers();
boolean flag = users.contains(user);
if(flag){
out.println("登录成功,3秒后跳转");
response.setHeader("Refresh", "3;URL=index.jsp");
HttpSession session = request.getSession();
session.setAttribute("user", user);
}else{
out.println("登录失败,3秒后跳转");
response.setHeader("Refresh", "3;URL=login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 注销Servlet
package com.itbear.domain;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginOut
*/
public class LoginOut extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginOut() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.invalidate();
out.println("注销完成");
response.setHeader("Refresh", "3;URL=/mySession/index.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
、