0

Cant seem to get this working, I'm writing code as a hobby so my level of understanding is low and cant get my head around why this is not working.

When I hit Submit I get this in the url:http://localhost:8080/Library/UserActionServlet?username=dsadl10&login=login

<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>

Serlvet:

@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);

}

}

command:

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;
}

}

service:

public users getUser(String uname){

    users u = null;

    try{

        userDao dao = new userDao();
        u = dao.getUserByName(uname);

    }
    catch(DaoException e){
        e.printStackTrace();
    }

    return u;
}

Dao:

 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;

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dave
  • 1
  • 1
  • Your form method is wrong. If you were seeing this in some online tutorial, throw away it and start at http://stackoverflow.com/tags/servlets/info instead. – BalusC Feb 29 '16 at 22:12
  • You can get there yourself by hovering the [servlets] tag you placed below the question until a black popbox shows up and then click therein the *info* link at bottom. Other tags have simliar wiki pages (at least the Java EE web related ones of my own interest; I wrote them) – BalusC Feb 29 '16 at 22:20

0 Answers0