Home Ask Login Register

Developers Planet

Your answer is one click away!

Kapil February 2016

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 it is the servlet page Here it is the servlet and 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!");
}
}

And here it is a jsp page and i gave the action url as same as given in @webservlet But I am getting an error. [org.springframework.web.servlet.PageNotFound] (default task-4) No mapping found for HTTP request with URI [/MySpring/logincontrol] in DispatcherServlet with name 'MvcConfiguration'

<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 i dont want to put entry in web.xml. in web.xml there is only one entry for dispatcher servlet.

Answers


sadasidha February 2016

First of all do not use @RequestMapping annotation over doGet method. @RequestMapping is for Spring's controller method.

Secondly now you have 2 servlet in your application. One is Dispatcher Servlet and another is login servlet. You have to tell which request is processed by which servlet.

Currently spring's dispatcher servlet is processing /logincontrol/* requests and there is no controller method with proper request mapping.

So you have two option

1st instead of using @WebServlet use @Controller annotation on your 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 for your dispatcher servlet which will prevent it from 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 the request /spring/* will be processed by springs dispatcher servlet.

Post Status

Asked in February 2016
Viewed 2,944 times
Voted 10
Answered 1 times

Search




Leave an answer


Quote of the day: live life