Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. `import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. public class LoginDao {
  7.  
  8. public static boolean validate(String username, String password) {
  9. boolean status = false;
  10. Connection conn = null;
  11. PreparedStatement pst = null;
  12. ResultSet rs = null;
  13.  
  14. String url = "jdbc:mysql://localhost:3306/";
  15. String dbName = "info";
  16. String driver = "com.mysql.jdbc.Driver";
  17. String userName = "root";
  18. String passsword = "azerty";
  19. try {
  20. Class.forName(driver).newInstance();
  21. conn = DriverManager.getConnection(url+dbName, userName,passsword);
  22. pst = conn.prepareStatement("select * from users where username=? and password=?");
  23. pst.setString(1, username);
  24. pst.setString(2, password);
  25. rs = pst.executeQuery();
  26. status = rs.next();}
  27. catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
  28. System.out.println(e);
  29. }
  30. finally {
  31. if (conn != null) {
  32. try { conn.close(); }
  33.  
  34. catch (SQLException e) { } }
  35.  
  36.  
  37. if (pst != null) { try { pst.close(); } catch (SQLException e) { } }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. if (rs != null) { try {
  45. rs.close();
  46. } catch (SQLException e)
  47. {
  48. }
  49. }
  50. }
  51. return status;
  52.  
  53. }
  54.  
  55. }`
  56.  
  57. enter code here
  58. import java.io.IOException;
  59. import java.io.PrintWriter;
  60. import javax.servlet.RequestDispatcher;
  61. import javax.servlet.ServletException;
  62. import javax.servlet.annotation.WebServlet;
  63. import javax.servlet.http.HttpServlet;
  64. import javax.servlet.http.HttpServletRequest;
  65. import javax.servlet.http.HttpServletResponse;
  66. import javax.servlet.http.HttpSession;
  67.  
  68.  
  69. @WebServlet(urlPatterns = {"/LoginServlet"})
  70. public class LoginServlet extends HttpServlet {
  71. @Override
  72. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  73. throws ServletException , IOException {
  74. response.setContentType("text/html");
  75. PrintWriter out = response.getWriter();
  76.  
  77. String n=request.getParameter("username");
  78. String p=request.getParameter("password");
  79.  
  80. HttpSession session = request.getSession(false);
  81. if(session!=null)
  82. session.setAttribute("name", n);
  83.  
  84. if(LoginDao.validate(n,p)){
  85. RequestDispatcher rd = request.getRequestDispatcher("/welcome.jsp");
  86. rd.forward(request,response);
  87. }
  88. else{
  89. out.print("<p style="color:red">Sorry username or password error</p>");
  90. RequestDispatcher rd=request.getRequestDispatcher("/index.html");
  91. rd.include(request,response);
  92. }
  93.  
  94. out.close();
  95. }
  96. }
  97.  
  98. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  99. pageEncoding="ISO-8859-1"%>
  100. <html>
  101. <head>
  102. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  103. <title>Welcome <%=session.getAttribute("name")%></title>
  104. </head>
  105. <body>
  106. <h3>Welcome </h3>
  107. <h4>
  108. Hello,
  109. <%=session.getAttribute("name")%></h4>
  110. </body>
  111. </html>
  112.  
  113. enter code here
  114. <?xml version="1.0" encoding="UTF-8"?>
  115. <web-app
  116. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  117. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
  118. version="2.5">
  119. <welcome-file-list>
  120. <welcome-file>index.html</welcome-file>
  121. </welcome-file-list>
  122. <servlet>
  123. <servlet-name>loginDao</servlet-name>
  124. <servlet-class>/LoginDao</servlet-class>
  125. </servlet>
  126.  
  127. <servlet-mapping>
  128. <servlet-name>loginServlet</servlet-name>
  129. <url-pattern>/loginServlet</url-pattern>
  130. </servlet-mapping>
  131.  
  132. <welcome-file-list>
  133. <welcome-file>index.html</welcome-file>
  134. </welcome-file-list>
  135. </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement