How do you detect the URL in a Java Servlet when forwarding to JSP?

前端 未结 2 1077
难免孤独
难免孤独 2021-01-06 12:53

I have a servlet that looks something like this:

public class ExampleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServ         


        
相关标签:
2条回答
  • 2021-01-06 13:47

    The URL the user (browser) requested can be accessed from the request by:

    request.getRequestURL()
    

    Alternatively the request has a whole bunch of accessors to get the various pieces of the URL as well as those on ServletRequest.

    To redirect to a different URL change the response rather than the request:

    response.sendRedirect(theURLToRedirectTo)
    
    0 讨论(0)
  • 2021-01-06 13:48

    The question is vague and ambiguous (is the servlet calling itself on every forward again?), but it much sounds like that you need request.getAttribute("javax.servlet.forward.request_uri").

    0 讨论(0)
提交回复
热议问题