java - How to call a servlet from a HTML form -
this question has answer here:
- how call servlet class html form 2 answers
i have problems connecting html form servlet in web application. here's 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 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 web.xml file since "servlet 3.0, servlets can configured @webservlet annotation on class without need web.xml". correct me if misunderstood statement.
finally here's snippet project explorer, note home.jsp file form located:
note: sadly solutions provided in other similar questions on stackoverflow don't work in case.
you need replace input type button submit one.
<input type="submit" value="submit"/>
Comments
Post a Comment