Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:22:52+00:00 2026-06-09T21:22:52+00:00

I’m trying to forward a servlet response to the same page from when it

  • 0

I’m trying to forward a servlet response to the same page from when it came from (a.k.a.: the previous page, or the “servlet” caller).

I’ve seen many answers (like this and this) but still can’t make it work.

I usually do the following to redirect the response of a servlet to another page:

request.getRequestDispatcher("MyNewPage").forward(request, response);

But I’ve tried to change “MyNewPage” for other options I’ve seen as solutions as:

request.getRequestDispatcher((String)request.getAttribute("javax.servlet.forward.request_uri")).forward(request, response);
request.getRequestDispatcher(request.getHeader("referer")).forward(request, response);

and other options and can’t make it work.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:22:54+00:00Added an answer on June 9, 2026 at 9:22 pm

    First of all the request.getHeader("referer") returns a full URL but you have to strip off the http://server[:port]/ because what you pass to request.getRequestDispatcher() is added to the application context like this:

    /NameOfApp/http:/localhost:8084/NameOfApp/test.jsp
    

    Which is not what you want because you just need to pass the following to the dispatcher method:

    test.jsp
    

    If we take things from the start the first request starts from this URL:

    http://localhost:8084/RequestDispatcher/test.jsp
    

    The forward works but the second time you are going make a request to your Servlet, the Servlet will forward to itself. So you will enter a loop with the Servlet calling itself. Why will this happen? Since you call the Servlet from the form, this means that the URL address in your browser address box will change to that of your Servlet after the first request.

    http://localhost:8084/RequestDispatcher/NewServlet
    

    The Servlet will forward the request back to the JSP page and the browser will display just that but the URL in the browser address box will still be the one containing the Servlet and not the JSP page:

    http://localhost:8084/RequestDispatcher/NewServlet
    

    So the next time you hit submit the Servlet will try to forward the request to itself. If I were you I would use a redirect. It seems more appropriate for your purpose:

    response.sendRedirect(request.getHeader("referer"));
    

    This will always change the URL in your browser address box and prevent your Servlet from looping. This will have an impact on the request parameters but you can always add them on the redirect URL(if it’s not sensitive information) or store them in session until you retrieve them on the next first request which will be made by the redirect.

    A framework like JSF would save you from these issues.

    The easiest solution that would allow you to use forward, is a hidden parameter in the form keeping the JSP(viewid) that called the Servlet instead of using the request.getHeader(“referer”). You will need to check for loops though, because someone could deliberately change the value to force your Servlet container to loop and eventually crash the VM. But you can just use a request attribute to keep record of the previous request in the chain and if it’s the same you respond with an error. So in the servlet you would use the hidden field value in order to decide where to forward to:

    request.getRequestDispatcher(request.getParameter("viewid")).forward(request, response);
    

    and in your JSP:

    <input type="hidden" name="viewid" value="test.jsp">
    

    I think this would cover your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.