www.pudn.com > Jsp-Blog.rar > LoginSevlet.java, change:2010-05-24,size:1110b


package myblog.servlet; 
 
import java.io.IOException; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import myblog.dao.LoginDao; 
 
public class LoginSevlet extends HttpServlet { 
 
	public void doPost(HttpServletRequest request, HttpServletResponse response) { 
		String username = request.getParameter("username").trim();	 
		String password = request.getParameter("pwd").trim(); 
		LoginDao login = new LoginDao(); 
		int num = login.login(username, password); 
		if (num == 1) { 
			try { 
				request.getSession().setAttribute("username", username); 
				response.sendRedirect("index.jsp"); 
			} catch (IOException e) { 
 
				e.printStackTrace(); 
			} 
		} else { 
			try { 
				request.setAttribute("loginmsg","用户名或密码错误!" ); 
				request.getRequestDispatcher("login.jsp").forward(request, 
						response); 
			} catch (ServletException e) { 
 
				e.printStackTrace(); 
			} catch (IOException e) { 
 
				e.printStackTrace(); 
			} 
		} 
 
	} 
}