Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Login page</title>
- <style type="text/css">
- body{
- background-image:url(mainLibrary.jpg)
- }
- </style>
- </head>
- <body style="body">
- <form action="UserActionServlet" method="doPost">
- <table width="50%" border="2">
- <tr bgcolor="#ffffcc">
- <td width="50%">Enter username</td>
- <td width="50"><input type="text" name="username"/></td>
- </tr>
- </table>
- <input type="hidden" name="login" value="login"/>
- <input type="submit" value="Submit"/>
- </form>
- </body>
- @WebServlet(urlPatterns={"/UserActionServlet"})
- public class UserActionServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public UserActionServlet(){
- super();
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
- String jspTo = null;
- if(request.getParameter("action").equalsIgnoreCase("login")){
- loginCommand cmd = new loginCommand();
- jspTo = cmd.execute(request,response);
- }
- RequestDispatcher rd = getServletContext().getRequestDispatcher(jspTo);
- rd.forward(request, response);
- }
- public class loginCommand implements Command {
- @Override
- public String execute(HttpServletRequest request, HttpServletResponse response){
- String jspTo = null;
- String username = request.getParameter("username");
- users u = null;
- if(username != null){
- userService us = new userService();
- u = us.getUser(username);
- if(u != null){
- HttpSession session = request.getSession();
- session.setAttribute("username", username);
- jspTo = "/loginSucess.jsp";
- }
- else
- jspTo = "/loginFailure.jsp";
- }
- else
- jspTo = "/loginFailure.jsp";
- return jspTo;
- }
- }
- public users getUser(String uname){
- users u = null;
- try{
- userDao dao = new userDao();
- u = dao.getUserByName(uname);
- }
- catch(DaoException e){
- e.printStackTrace();
- }
- return u;
- }
- public users getUserByName(String uname) throws DaoException{
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- users u = null;
- try{
- conn = this.getConnection();
- String query = "SELECT * FROM users WHERE username = ?";
- ps = conn.prepareStatement(query);
- ps.setString(1,uname);
- rs = ps.executeQuery();
- if(rs.next()){
- String username = rs.getString("username");
- String fname = rs.getString("fname");
- String sname = rs.getString("sname");
- String password = rs.getString("password");
- u = new users(username,fname,sname,password);
- }
- }
- catch(SQLException e1){
- throw new DaoException("failed to locate user in DB" + e1.getMessage());
- }
- return u;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement