java - I want to use spring annotation @webservlet and call this servlet from a jsp page action form. But i got the error. Here it is: -
here servlet page here servlet , mapping pattern and
@webservlet("/logincontrol") public class sessioncontroller extends httpservlet { private static final long serialversionuid = 1l; public sessioncontroller() { super(); } @requestmapping(value="/logincontrol") protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.getsession().setattribute("name",request.getparameter("password")); response.getwriter().println("hello world!"); } }
<form name="loginform" action="logincontrol"> <table> <tr> <td>user:</td> <td><input type='text' name='name'></td> </tr> <tr> <td>password:</td> <td><input type='password' name='password' /></td> </tr> <tr> <td colspan='2'><input name="submit" type="submit" value="submit" /></td> </tr> </table> <%-- <input type="hidden" name="${_csrf.parametername}" value="${_csrf.token}" /> --%> </form>
but remember dont want put entry in web.xml. in web.xml there 1 entry dispatcher servlet.
first of not use @requestmapping annotation on doget method. @requestmapping spring's controller method.
secondly have 2 servlet in application. 1 dispatcher servlet , login servlet. have tell request processed servlet.
currently spring's dispatcher servlet processing /logincontrol/* requests , there no controller method proper request mapping.
so have 2 option
1st instead of using @webservlet use @controller annotation on sessioncontroller class
@controller public class sessioncontroller { private static final long serialversionuid = 1l; public sessioncontroller() { super(); } @requestmapping(value="/logincontrol") protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.getsession().setattribute("name",request.getparameter("password")); response.getwriter().println("hello world!"); } }
2nd use different mappings dispatcher servlet prevent processing /logincontrol/* requests.
example
<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping>
all request /spring/* processed springs dispatcher servlet.
Comments
Post a Comment