日期:2014-05-20 浏览次数:20808 次
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String name = request.getParameter("userName"); String password = request.getParameter("userPassword"); Connection conn=null; PreparedStatement pstmt= null; ResultSet rs=null; String getname=request.getParameter("USERNAME"); String getpsw=request.getParameter("USERPSW"); try { Class.forName("com.mysql.jdbc.driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/db?" + "user=root&password=root"); pstmt=conn.prepareStatement("SELECT * FROM USERINFO WHERE UNAME=? AND UPSW=?"); pstmt.setString(1, name); pstmt.setString(2, password); name=getname; password=getpsw; rs = pstmt.executeQuery(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(rs.next()){ response.sendRedirect("Welcome"); } else { response.sendRedirect("Login"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" 哈哈 "); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }