Teysz
Teysz

Reputation: 741

How to change HttpServletRequest.servletpath?

I'm using a few JSP-pages and one servlet.

Servlet doPost():

String userPath = request.getServletPath();
if (userPath.equals("/PageAdm")) {
     String username = request.getParameter("username");
     String password = request.getParameter("password");
     User userone = new User(username, password);
     request.setAttribute("user", user);
     RequestDispatcher view = request.getRequestDispatcher("view/Page.jsp");
     view.forward(request, response);
} else if (userPath.equals("/Page")) {
     String username = request.getParameter("username");
     UsersService us = new UsersService();
     User user = us.getUserByUsername(username);
     request.setAttribute("user", user);
     RequestDispatcher view = request.getRequestDispatcher("view/UserNameEdit.jsp");
     view.forward(request, response);
}

When I log in, I can change the username, but when I submit the form (click) to change it, the servletpath will still be "/PageAdm" but it has to be "/Page" so I can change my username. How can I change the servletpath so it will go to "/UserNameEdit"?

Upvotes: 0

Views: 1766

Answers (1)

stepanian
stepanian

Reputation: 11433

Add an action attribute to the form element to point to the correct path.

<form action="<c:url value="/MyServletPath/Page"/>" method="post">

Upvotes: 2

Related Questions