www.pudn.com > restrant.zip > LoginServlet.java, change:2014-01-03,size:2396b


package com.alex.restrantol.servlet; 
 
import java.io.IOException; 
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; 
 
import com.alex.restrantol.dao.UserDao; 
import com.alex.restrantol.entity.AdminEntity; 
import com.alex.restrantol.entity.MealEntity; 
import com.alex.restrantol.entity.UserEntity; 
 
public class LoginServlet extends HttpServlet { 
	 
	@Override 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException { 
		this.doPost(req, resp); 
	} 
 
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException { 
		String method = req.getParameter("method"); 
		String username = req.getParameter("username"); 
		String psw = req.getParameter("psw"); 
		UserEntity user =null; 
		AdminEntity admin = null; 
		if("user".equals(method)){ 
			if(username != null && username.trim().length() > 0) 
				if(psw != null && psw.trim().length() > 0){ 
					user = new UserEntity(); 
					user = new UserDao().findUserIsExist(username); 
				} 
			if(user == null){ 
				req.setAttribute("username_error", "该用户不存在!"); 
				req.getRequestDispatcher("/login.jsp").forward(req, resp); 
			}else if(!user.getPsw().equals(psw)){ 
				req.setAttribute("psw_error", "密码错误!"); 
				req.getRequestDispatcher("/login.jsp").forward(req, resp); 
			}else{ 
				req.getSession().setAttribute("user", user); 
				req.getRequestDispatcher("/home.jsp").forward(req, resp); 
			} 
		}else if("admin".equals(method)){ 
			if(username != null && username.trim().length() > 0) 
				if(psw != null && psw.trim().length() > 0){ 
					admin = new AdminEntity();	 
					admin = new UserDao().findAdminIsExist(username); 
				} 
			if(admin == null){ 
				req.setAttribute("username_error", "该管理员不存在!"); 
				req.getRequestDispatcher("/backLogin.jsp").forward(req, resp); 
			}else if(!admin.getAdminPwd().equals(psw)){ 
				req.setAttribute("psw_error", "密码错误!"); 
				req.getRequestDispatcher("/backLogin.jsp").forward(req, resp); 
			}else{ 
				req.getSession().setAttribute("admin",admin); 
				req.getRequestDispatcher("/index.jsp").forward(req, resp); 
			} 
		} 
	} 
}