需求:用Session模拟登录,退出登录场景
项目结构:
代码:
User
public class User {
private String id;
private String name;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
UserDao
public class UserDao {
public static User findByName(String name){
Document doc = XMLUtil.getDocument();
Element em = (Element)doc.selectSingleNode("//name[text()='"+name+"']");
User user = null;
if (em!=null){
user = new User();
user.setName(name);
Element el = em.getParent();
user.setId(el.attributeValue("id"));
user.setPassword(el.elementText("password"));
}
return user;
}
}
XMLUtil
public class XMLUtil {
/**
* 读取xml文件,返回document对象
*/
public static Document getDocument(){
try {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("user.xml");
Document doc = new SAXReader().read(in);
return doc;
} catch (DocumentException e) {
e.printStackTrace();
//把转换为运行时异常抛出即可!
throw new RuntimeException(e);
}
}
/**
* 传如docuemnt对象,写出到xml文件中
*/
public static void write2xml(Document doc){
try {
OutputStream out = new FileOutputStream("e:/user.xml");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(out,format);
writer.write(doc);
writer.close();
} catch (Exception e) {
e.printStackTrace();
//把转换为运行时异常抛出即可!
throw new RuntimeException(e);
}
}
}
UserIndexServlet
public class UserIndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
response.setContentType("text/html;charset=utf-8");
if (session!=null){ //如果session为空,说明从来没有登录过
User user = (User) session.getAttribute("name");
if (user!=null)
response.getWriter().write("欢迎您回来"+user.getName()+"<a href='/session_login/LogoutServlet'>[退出登录]</a>");
else
response.sendRedirect(request.getContextPath()+"/login.html");
}else{
response.sendRedirect(request.getContextPath()+"/login.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
LoginServlet
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
String password = request.getParameter("password");
User user = UserDao.findByName(name);
if (user!=null){
if (user.getPassword().equals(password)){
HttpSession settion = request.getSession();
settion.setAttribute("name", user);
response.sendRedirect(request.getContextPath()+"/UserIndexServlet");
}else
response.getWriter().write("用户名或密码不正确");
}else
response.getWriter().write("用户名或密码不正确");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
LogoutServlet
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LogoutServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
if (session !=null){
session.removeAttribute("name");
response.sendRedirect(request.getContextPath()+"/UserIndexServlet");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
效果展示: