Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. My html code (login.html)
  2.  
  3. <!Doctype html>
  4. <head>
  5. <title>Login</title>
  6. <link rel="stylesheet" type="text/css" href="header.css"/>
  7. <style>
  8. .content{
  9. text-align:center;
  10. position:absolute;
  11. top:250px;
  12. left:450px;
  13. font-size:20px;
  14. color:white;
  15. }
  16. #inputbox
  17. {
  18. height:30px;
  19. width:300px;
  20. }
  21. #submit
  22. {
  23. color:white;
  24. height:40px;
  25. width:100px;
  26. position:absolute;
  27. top:150px;
  28. background: #E8F0F0;
  29. color:brown;
  30. border:none;
  31. }
  32. body
  33. {
  34. background:url(Images/loginbackground.jpg);
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <ol>
  40. <li><a href="mainpage.html">Home</a></li>
  41. <li><a href="courses.html">Courses</a></li>
  42. <li><a href="">Contact Us</a></li>
  43. <li id="signup"><a href="signup.html">Sign Up</a></li>
  44. </ol>
  45. <div class="content">
  46. <form method="post" action="LoginCheck">
  47. Username:<input type="text" name="username" placeholder="Enter your userID" id="inputbox" required/><br><br>
  48. Password:<input type="password" name="password" placeholder="Enter Your password" id="inputbox" required/> <br><br>
  49. <input type="submit" name="login" value="login" id="submit"/>
  50. </form>
  51.  
  52. </div>
  53. </body>
  54. </html>
  55.  
  56. Whether the servlet was correct
  57. Servlet code is (LoginCheck.java)
  58.  
  59. import java.io.IOException;
  60. import java.io.PrintWriter;
  61.  
  62. //import javax.servlet.RequestDispatcher;
  63. import javax.servlet.ServletException;
  64. import javax.servlet.annotation.WebServlet;
  65. import javax.servlet.http.HttpServlet;
  66. import javax.servlet.http.HttpServletRequest;
  67. import javax.servlet.http.HttpServletResponse;
  68. import java.sql.*;
  69.  
  70. /**
  71. * Servlet implementation class MySQLConnect
  72. */
  73. @WebServlet("/LoginCheck")
  74. public class LoginCheck extends HttpServlet {
  75.  
  76. private static final long serialVersionUID = 1L;
  77.  
  78.  
  79. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  80. response.setContentType("text/html");
  81. PrintWriter out = response.getWriter();
  82. String username = request.getParameter("username");
  83. String password = request.getParameter("password");
  84. /*if((username.equalsIgnoreCase("jothibasu"))&&(password.equalsIgnoreCase("admin"))){
  85. out.println("<div style='position: absolute; top:100px; left:400px;'>");
  86. out.println("<font size='15' color='blue'>Welcome "+username+" !</font>");
  87. out.println("</div>");
  88. RequestDispatcher rd=request.getRequestDispatcher("/courses.html");
  89. rd.include(request, response);
  90. }
  91. else{
  92. out.println("<div style='position: absolute; top:180px; left:400px;'>");
  93. out.println("<font size='15' color='darkgray'>Sorry UserName or Password Error!</font>");
  94. out.println("</div>");
  95. RequestDispatcher rd=request.getRequestDispatcher("/login.html");
  96. rd.include(request, response);
  97.  
  98. }*/
  99. try {
  100. Class.forName("com.mysql.jdbc.Driver");
  101. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/startupdb", "root", "developer");
  102. PreparedStatement pst = conn.prepareStatement("Select username,userpassword from logindetials where username=? and userpassword=?");
  103. pst.setString(1, username);
  104. pst.setString(2, password);
  105. ResultSet rs = pst.executeQuery();
  106. if (rs.next()) {
  107. out.println("Correct login credentials");
  108. }
  109. else {
  110. out.println("Incorrect login credentials");
  111. }
  112. }
  113. catch (ClassNotFoundException | SQLException e) {
  114. e.printStackTrace();
  115. }
  116. }
  117. }
  118.  
  119.  
  120. And my database & table goes here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement