1 package com.lxr.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 import javax.mail.SendFailedException; 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import com.lxr.dao.Dao; 16 import com.mysql.jdbc.Connection; 17 import com.mysql.jdbc.Statement; 18 import com.mysql.jdbc.jdbc2.optional.ConnectionWrapper; 19 20 @SuppressWarnings("serial") 21 public class AddAccount extends HttpServlet { 22 23 public void doGet(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 doPost(request, response); 26 } 27 28 29 public void doPost(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 request.setCharacterEncoding("utf-8"); 32 String account = (String) request 33 .getParameter("account"); 34 String password = (String) request 35 .getParameter("password"); 36 Connection conn = null; 37 PreparedStatement stmt = null; 38 String sql=""; 39 ResultSet rs = null; 40 try { 41 42 // sql="select * from account where account='"+account+"'+ and password='"+password+"'"; 43 sql="select account, password from account where account=? and password=? "; 44 System.out.println("执行的sql语句"+sql); 45 conn = Dao.getConnection(); 46 // stmt=conn.createStatement(); 47 stmt=conn.prepareStatement(sql); 48 stmt.setString(1,account); 49 stmt.setString(2,password); 50 rs=stmt.executeQuery(); 51 if (rs.next()) { 52 //request.getContextPath()返回的是当前的webapp的名字 53 //而服务器内部跳转实在webapp里边,所以不用加request.getContextPath() 54 //直接加jsp页面的名字就行 55 //而请求重定向是默认不在webapp里边的 所以要加上webapp(request.getContextPath())的名字再加jsp页面名 56 System.out.println("登录成功"); 57 request.getRequestDispatcher("/loginsuccess.jsp").forward(request, response); 58 // response.sendRedirect("/plan/loginsuccess.jsp"); 59 }else { 60 System.out.println("登录失败"); 61 // request.getRequestDispatcher("/login.jsp").forward(request, response); 62 response.sendRedirect(request.getContextPath()+"/login.jsp"); 63 64 65 } 66 } catch (SQLException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } catch (ClassNotFoundException e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 }finally{ 73 try { 74 if(rs!=null){ 75 rs.close(); 76 } 77 if(stmt!=null){ 78 stmt.close(); 79 } 80 if(conn!=null){ 81 conn.close(); 82 } 83 } catch (SQLException e2) { 84 System.out.println("释放资源发生异常!"); 85 } 86 } 87 88 } 89 90 91 }