Problem in servlet url-pattern JAVA

Closed
ahmedep Posts 9 Registration date Saturday November 27, 2021 Status Member Last seen November 19, 2022 - Updated on Nov 19, 2022 at 08:51 AM
eden60_3473 Posts 6 Registration date Tuesday July 11, 2023 Status Member Last seen July 27, 2023 - Jul 27, 2023 at 08:53 AM

This is the web.xml file :

<servlet>
    <servlet-name>ClientServlet</servlet-name>
    <servlet-class>ma.fstt.web.ClientServlet</servlet-class>
</servlet>
 
<servlet-mapping>
    <servlet-name>ClientServlet</servlet-name>
    <url-pattern>/client</url-pattern>
</servlet-mapping>

And this is a part of ClientServlet.java file :

package ma.fstt.web;

public class ClientServlet extends HttpServlet {
    ...
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getServletPath();
        try {
            switch (action) {
                case "/new":
                    showNewForm(request, response);
                    break;
                                ...
                default:
                    listClients(request, response);
                    break;
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }
        ...
}

I want to call the "showNewForm()" method with the url : "http://localhost:8081/Atelier1/listarticleclient/new"

When i did i got this error :

2 responses

gorgeousgorilla Posts 1 Registration date Friday November 25, 2022 Status Member Last seen November 25, 2022
Updated on Nov 25, 2022 at 04:16 AM

Hi,

There seem to be least three issues

Classes are searched in WEB-INF/classes rather than in WEB-INF

Use the HttpServlet instead of the generic Servlet

Overwrite the doXXX methods of the HttpServlet and not the generic service method. The service method dispatches to the corresponding doXXX method.

0
eden60_3473 Posts 6 Registration date Tuesday July 11, 2023 Status Member Last seen July 27, 2023 1
Updated on Jul 27, 2023 at 02:42 PM

It seems you are trying to access the "showNewForm()" method through the URL "http://localhost:8081/Atelier1/listarticleclient/new", but the servlet mapping is set to "/client" in the web.xml file.

To call the method correctly, change the URL to "http://localhost:8081/Atelier1/client/new".

This should resolve the error.

0