Advertisement
Guest User

Untitled

a guest
May 17th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. // Loading required libraries
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import java.sql.*;
  7.  
  8. public class SessionLoginServlet extends HttpServlet{
  9.  
  10. public void doGet(HttpServletRequest request,
  11. HttpServletResponse response)
  12. throws ServletException, IOException
  13. {
  14. sendLoginForm(response, false);
  15. }
  16.  
  17. public void doPost(HttpServletRequest request,
  18. HttpServletResponse response)
  19. throws ServletException, IOException{
  20. String userName = request.getParameter("userName");
  21. String password = request.getParameter("password");
  22. if (login(userName,password)){
  23. // envia cookie para o browser
  24. HttpSession session = request.getSession(true);
  25. session.setAttribute("loggedIn",new String("true"));
  26. response.sendRedirect("Content2Servlet");
  27. }
  28. else{
  29. sendLoginForm(response, true);
  30. }
  31. }
  32.  
  33. private void sendLoginForm(HttpServletResponse response, boolean withErrorMessage)
  34. throws ServletException, IOException{
  35. response.setContentType("text/html");
  36. PrintWriter out = response.getWriter();
  37. out.println("<HTML>");
  38. out.println("<HEAD>");
  39. out.println("<TITLE>Login</TITLE>");
  40. out.println("</HEAD>");
  41. out.println("<BODY>");
  42. out.println("<CENTER>");
  43. if(withErrorMessage){
  44. out.println("Login failed. Please try again.<BR>");
  45. out.println("Cookies might be off.");
  46. // link para ajuda para ablitar cookies
  47. }
  48. out.println("<BR>");
  49. out.println("<BR><H2>Login Page</H2>");
  50. out.println("<BR>");
  51. out.println("<BR>Please enter your user name and passaword.");
  52. out.println("<BR>");
  53. out.println("<BR><FORM METHOD=POST>");
  54. out.println("<TABLE>");
  55. out.println("<TR>");
  56. out.println("<TD>User Name:</TD>");
  57. out.println("<TD><INPUT TYPE=TEXT NAME=userName></TD>");
  58. out.println("</TR>");
  59. out.println("<TR>");
  60. out.println("<TD>Password:</TD>");
  61. out.println("<TD><INPUT TYPE=PASSWORD NAME=password></TD>");
  62. out.println("</TR>");
  63. out.println("<TR>");
  64. out.println("<TD ALIGN=RIGHT COLSPAN=2>");
  65. out.println("<INPUT TYPE=SUBMIT VALUE=Login></TD>");
  66. out.println("</TR>");
  67. out.println("</TABLE>");
  68. out.println("</FORM>");
  69. out.println("</CENTER>");
  70. out.println("</BODY>");
  71. out.println("</HTML>");
  72. }
  73.  
  74. public static boolean login(String userName, String password)throws ServletException{
  75. try {
  76. // JDBC driver name and database URL
  77. String JDBC_DRIVER="com.mysql.jdbc.Driver";
  78. String DB_URL="jdbc:mysql://localhost/pgmc";
  79. // Database credentials
  80. String USER = "root";
  81. String PASS = "123456";
  82. Class.forName(JDBC_DRIVER);
  83. Connection con =
  84. DriverManager.getConnection(DB_URL,USER,PASS);
  85. Statement s = con.createStatement();
  86. String sql = "SELECT username, password FROM usuario"+
  87. " WHERE username='"+userName+"'"+
  88. " AND password='"+password+"'";
  89. ResultSet rs = s.executeQuery(sql);
  90. if(rs.next()){
  91. rs.close();
  92. s.close();
  93. con.close();
  94. return true;
  95. }
  96. rs.close();
  97. s.close();
  98. con.close();
  99. }
  100. catch(ClassNotFoundException e){
  101. throw new ServletException(e);
  102. }
  103. catch(SQLException e){
  104. throw new ServletException(e);
  105. }
  106. catch(Exception e){
  107. throw new ServletException(e);
  108. }
  109. return false;
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement