Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. <head>
  2. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  3. <title>Login page</title>
  4. <style type="text/css">
  5. body{
  6. background-image:url(mainLibrary.jpg)
  7. }
  8. </style>
  9. </head>
  10. <body style="body">
  11. <form action="UserActionServlet" method="doPost">
  12. <table width="50%" border="2">
  13. <tr bgcolor="#ffffcc">
  14. <td width="50%">Enter username</td>
  15. <td width="50"><input type="text" name="username"/></td>
  16. </tr>
  17. </table>
  18.  
  19. <input type="hidden" name="login" value="login"/>
  20. <input type="submit" value="Submit"/>
  21. </form>
  22. </body>
  23.  
  24. @WebServlet(urlPatterns={"/UserActionServlet"})
  25. public class UserActionServlet extends HttpServlet {
  26.  
  27. private static final long serialVersionUID = 1L;
  28.  
  29. public UserActionServlet(){
  30. super();
  31. }
  32.  
  33. @Override
  34. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
  35.  
  36. }
  37.  
  38. @Override
  39. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
  40.  
  41. String jspTo = null;
  42.  
  43. if(request.getParameter("action").equalsIgnoreCase("login")){
  44.  
  45. loginCommand cmd = new loginCommand();
  46. jspTo = cmd.execute(request,response);
  47.  
  48. }
  49.  
  50. RequestDispatcher rd = getServletContext().getRequestDispatcher(jspTo);
  51. rd.forward(request, response);
  52.  
  53. }
  54.  
  55. public class loginCommand implements Command {
  56.  
  57. @Override
  58. public String execute(HttpServletRequest request, HttpServletResponse response){
  59.  
  60. String jspTo = null;
  61. String username = request.getParameter("username");
  62. users u = null;
  63.  
  64. if(username != null){
  65. userService us = new userService();
  66. u = us.getUser(username);
  67.  
  68. if(u != null){
  69. HttpSession session = request.getSession();
  70. session.setAttribute("username", username);
  71. jspTo = "/loginSucess.jsp";
  72. }
  73. else
  74. jspTo = "/loginFailure.jsp";
  75. }
  76. else
  77. jspTo = "/loginFailure.jsp";
  78.  
  79. return jspTo;
  80. }
  81.  
  82. }
  83.  
  84. public users getUser(String uname){
  85.  
  86. users u = null;
  87.  
  88. try{
  89.  
  90. userDao dao = new userDao();
  91. u = dao.getUserByName(uname);
  92.  
  93. }
  94. catch(DaoException e){
  95. e.printStackTrace();
  96. }
  97.  
  98. return u;
  99. }
  100.  
  101. public users getUserByName(String uname) throws DaoException{
  102.  
  103. Connection conn = null;
  104. PreparedStatement ps = null;
  105. ResultSet rs = null;
  106. users u = null;
  107.  
  108. try{
  109.  
  110. conn = this.getConnection();
  111. String query = "SELECT * FROM users WHERE username = ?";
  112. ps = conn.prepareStatement(query);
  113. ps.setString(1,uname);
  114. rs = ps.executeQuery();
  115.  
  116. if(rs.next()){
  117.  
  118. String username = rs.getString("username");
  119. String fname = rs.getString("fname");
  120. String sname = rs.getString("sname");
  121. String password = rs.getString("password");
  122. u = new users(username,fname,sname,password);
  123.  
  124. }
  125.  
  126. }
  127. catch(SQLException e1){
  128. throw new DaoException("failed to locate user in DB" + e1.getMessage());
  129. }
  130.  
  131. return u;
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement