Advertisement
Guest User

Untitled

a guest
Jan 14th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  6. <title>Login</title>
  7. </head>
  8. <body>
  9. <form action="loginServlet" method="post">
  10. <fieldset style="width: 300px">
  11. <legend> Login </legend>
  12. <table>
  13. <tr>
  14. <td>Usuario</td>
  15. <td><input type="text" name="usuario" required="required" /></td>
  16. </tr>
  17. <tr>
  18. <td>Senha</td>
  19. <td><input type="password" name="senha" required="required" /></td>
  20. </tr>
  21. <tr>
  22. <td><input type="submit" value="Login" /></td>
  23. </tr>
  24. </table>
  25. </fieldset>
  26. </form>
  27. </body>
  28. </html>
  29.  
  30. package com.amzi.dao;
  31.  
  32. import java.sql.Connection;
  33. import java.sql.DriverManager;
  34. import java.sql.PreparedStatement;
  35. import java.sql.ResultSet;
  36. import java.sql.SQLException;
  37.  
  38. public class LoginDao {
  39.  
  40. public static boolean validate(String nome, String senha) {
  41. boolean status = false;
  42. Connection conn = null;
  43. PreparedStatement pst = null;
  44. ResultSet rs = null;
  45.  
  46. String url = "jdbc:mysql://localhost:8095/";
  47. String dbName = "locadora";
  48. String driver = "com.mysql.jdbc.Driver";
  49. String userName = "root";
  50. String password = "123";
  51. try {
  52. Class.forName(driver).newInstance();
  53. conn = DriverManager
  54. .getConnection(url + dbName, userName, password);
  55.  
  56. pst = conn
  57. .prepareStatement("select * from tab_usuarios where user=? and pw_user=?");
  58. pst.setString(1, nome);
  59. pst.setString(2, senha);
  60.  
  61. rs = pst.executeQuery();
  62. status = rs.next();
  63.  
  64. } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException e) {
  65. System.out.println(e);
  66. } finally {
  67. if (conn != null) {
  68. try {
  69. conn.close();
  70. } catch (SQLException e) {
  71. }
  72. }
  73. if (pst != null) {
  74. try {
  75. pst.close();
  76. } catch (SQLException e) {
  77. }
  78. }
  79. if (rs != null) {
  80. try {
  81. rs.close();
  82. } catch (SQLException e) {
  83. }
  84. }
  85. }
  86. return status;
  87. }
  88. }
  89.  
  90. package com.amzi.servlets;
  91.  
  92. import java.io.IOException;
  93. import java.io.PrintWriter;
  94.  
  95. import javax.servlet.RequestDispatcher;
  96. import javax.servlet.ServletException;
  97. import javax.servlet.http.HttpServlet;
  98. import javax.servlet.http.HttpServletRequest;
  99. import javax.servlet.http.HttpServletResponse;
  100. import javax.servlet.http.HttpSession;
  101.  
  102. import com.amzi.dao.LoginDao;
  103.  
  104. public class LoginServlet extends HttpServlet {
  105.  
  106. private static final long serialVersionUID = 1L;
  107.  
  108. /**
  109. *
  110. * @param request
  111. * @param response
  112. * @throws ServletException
  113. * @throws IOException
  114. */
  115. @Override
  116. public void doPost(HttpServletRequest request, HttpServletResponse response)
  117. throws ServletException, IOException {
  118.  
  119. response.setContentType("text/html");
  120. try (PrintWriter out = response.getWriter()) {
  121. String n = request.getParameter("usuario");
  122. String p = request.getParameter("senha");
  123.  
  124. HttpSession session = request.getSession(false);
  125. if (session != null) {
  126. session.setAttribute("name", n);
  127. }
  128.  
  129. if (LoginDao.validate(n, p)) {
  130. System.out.println("Senha correta!");
  131. RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
  132. rd.forward(request, response);
  133. } else {
  134. out.print("<p style="color:red">Desculpe, usuario ou senha incorretos.</p>");
  135. RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
  136. rd.include(request, response);
  137. }
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement