0

I have problems connecting an html form to a Servlet in my web application. Here's the html file:

<form method="post" class="/Login" action="/Login">
  <fieldset class="textbox">
      
                      <label class="textbox_field">
                        <span>Username or Email</span>
                        <input id="username" name="username" value="" type="text" autocomplete="on" placeholder="Username">
                      </label>

                      <label class="textbox_field">
                        <span>Password</span>
                        <input id="password" name="password" value="" type="password" placeholder="Password">
                      </label>

                      <button class="submit_button" type="submit">Sign-In</button>
                </fieldset>
</form>

And here is the servlet, called Login:

 @WebServlet("/Login")
    public class Login extends HttpServlet {
        private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("in doPost...");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String username = request.getParameter("username");
        String pass = request.getParameter("password");

        if(Validate.checkUser(username, pass))
        {
            RequestDispatcher rs = request.getRequestDispatcher("Welcome");
            rs.forward(request, response);
        }
        else
        {
           out.println("Username or Password incorrect");
           RequestDispatcher rs = request.getRequestDispatcher("index.html");
           rs.include(request, response);
        }
    }

}

I didn't edit the web.xml file since with "Servlet 3.0, the servlets can be configured by @WebServlet annotation on the class without the need for a web.xml". Obviously correct me if I misunderstood this statement.

Finally here's a snippet from the Project Explorer, note that home.jsp is the file where the form is located:

enter image description here

Note: Sadly the solutions provided in other similar questions on stackoverflow don't work in my case.

Andrew
  • 358
  • 10
  • 23
  • 1
    Have you tried replacing `
    ` with `
    `, the difference lies only at `action="Login"` part, rest is same.
    – nIcE cOw Jul 14 '15 at 15:47
  • 1
    Yes I did, but it's still not working. Also, I just tried to call the doGet method of the servlet and it works just fine. The form+doPost is completely dead. – Andrew Jul 14 '15 at 15:54
  • change ` ` to `` – Kainix Jul 14 '15 at 17:15

1 Answers1

2

You need to replace the input type button with a submit one.

<input type="submit" value="Submit"/>
Recoba20
  • 314
  • 1
  • 13
  • 1
    Just change the @WebServlet("/Login") to @WebServlet("Login") and it must be ok. – Recoba20 Jul 14 '15 at 20:17
  • When I do this I get a LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Project_Name]] – Andrew Jul 15 '15 at 11:07
  • 1
    I do not know, I use Eclipse Mars and it works just fine here. – Recoba20 Jul 15 '15 at 13:07