Guest User

Untitled

a guest
Aug 31st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. Login Servlet not responding
  2. <html>
  3. <body>
  4. <form action="loginservlet" method="post">
  5. <h1>Please Login</h1>
  6. Login: <input type="text" name="username"><br>
  7. Password: <input type="password" name="password"><br>
  8. <input type=submit value="Login">
  9. </form>
  10. </body>
  11. </html>
  12.  
  13. package connectm;
  14.  
  15. import java.io.IOException;
  16. import java.io.PrintWriter;
  17. import java.sql.*;
  18.  
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServlet;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import javax.servlet.http.HttpSession;
  24.  
  25.  
  26. public class loginservlet extends HttpServlet {
  27.  
  28.  
  29. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  30. // TODO Auto-generated method stub
  31.  
  32. response.setContentType("text/html");
  33. PrintWriter out = response.getWriter();
  34.  
  35. Connection conn = null;
  36. String url = "jdbc:mysql://localhost:3306/";
  37. String dbName = "test";
  38. String driver = "com.mysql.jdbc.Driver";
  39. String userName = "root";
  40. String password = "root";
  41. String username="";
  42. String userpass="";
  43. String strQuery="";
  44. Statement st=null;
  45. ResultSet rs=null;
  46. HttpSession session = request.getSession(true);
  47.  
  48. try {
  49. Class.forName(driver).newInstance();
  50. conn = DriverManager.getConnection(url + dbName, userName,
  51. password);
  52. if (request.getParameter("username") != null
  53. && request.getParameter("username") != ""
  54. && request.getParameter("password") != null
  55. && request.getParameter("password") != "") {
  56. username = request.getParameter("username").toString();
  57. userpass = request.getParameter("password").toString();
  58. /*strQuery = "select * from user where username='" + username
  59. + "' and password='" + userpass + "'";*/
  60. //System.out.println(strQuery);
  61. strQuery = "select username from user where username= ? and password= ?";
  62. st = conn.createStatement();
  63. PreparedStatement ps=conn.prepareStatement(strQuery);
  64. ps.setString(1, username);
  65. ps.setString(2, userpass);
  66. rs = ps.executeQuery(strQuery);
  67. int count = 0;
  68. while (rs.next()) {
  69. session.setAttribute("username", rs.getString(1));
  70. count++;
  71. }
  72. if (count > 0) {
  73. response.sendRedirect("welcome.jsp");
  74. } else {
  75. response.sendRedirect("login.jsp");
  76. }
  77. } else {
  78. response.sendRedirect("login.jsp");
  79. }
  80. out.println("Connected to the database");
  81. conn.close();
  82. out.println("Disconnected from database");
  83. } catch (Exception e) {
  84. // TODO: handle exception
  85. }
  86.  
  87.  
  88.  
  89.  
  90. }
  91.  
  92. }
  93.  
  94. } catch (Exception e) {
  95. // TODO: handle exception
  96. }
  97.  
  98. } catch (Exception e) {
  99. throw new ServletException("Login failed", e);
  100. }
Add Comment
Please, Sign In to add comment