0

Here when the url pattern is "/Login/new" the servlet is not invoked which I verified by printing to console.It works just for the default case that is for "/Login"

@WebServlet("/Login")
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    private UserDAO userDAO;
    
    public void init()
    {
        userDAO = new UserDAO();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("Servlet invoked !!");
        String action = request.getServletPath();
        System.out.println(action);
        switch(action)
        {
        case "/new":
            showNewForm(request,response);
            break;  
        case "/insert":
            insertUser(request, response);
            break;
        case "/update":
            updateUser(request, response);
            break;
        case "/delete":
            deleteUser(request, response);
            break;
        case "/edit":
            showEditForm(request, response);
            break;
        case "/login":
            RequestDispatcher dispatcher= request.getRequestDispatcher("/login.jsp");
            dispatcher.forward(request, response);
            
            
            
        default:
            //listUser(request,response);
            showLogin(request,response);
            break;
        
        
        }

Output for "/Login":[Output for "/Login":1

Output for "/Login/new":enter image description here

The desired output is that the function in "/new" case should be called.What am I missing?

Harmless
  • 38
  • 5

1 Answers1

1

Update the path to the following

@WebServlet("/Login/*")

, And try to check that you are retrieving the last part of the path to the URL and lower-case following

String action = request.getRequestURI();
action = action.substring(action.lastIndexOf("/")).toLowerCase();
0xh3xa
  • 4,801
  • 2
  • 14
  • 28