-1

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.

Kapil
  • 71
  • 2
  • 5

1 Answers1

4

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.

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • 1
    Thanks, @mirmdasif for your explanation and I found other solution with the same issue here: http://www.tellmehow.co/use-servlet-annotation-with-spring-mvc/ – Tell Me How Jan 13 '19 at 07:13