servletspath-parameter

When I change the url-pattern of servlet from / to /client doesn't take the path on consedration


This is web.xml

<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 the ClientServlet.java

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'm trying to access the showNewForm by http://localhost:8081/Atelier1/client/new and this error show up enter image description here


Solution

  • I resolved this problem by adding a variable in the path

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