File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Navigating to another page... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Navigating to another page..." Watch "Navigating to another page..." New topic

Navigating to another page...

curtis harrison

Joined: Sep 12, 2001
Posts: 8
Ok, I'm new to this jsp stuff. I have a page (Page1) that uses another page (Page2) to submit some data to a database. When Page2 is done processing, I want the browser to navigate back to Page1.
I tried using the following, but the text in the 'Address' box at the top of my browser is reflecting the URL for Page2...
<jsp:forward page="Page1.jsp" />

Is there just a quick and easy way to fix this?
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

If you're doing processing only (no display) you'd usually use a servlet rather than a JSP, but to answer the actual question...
Have a look at the sendRedirect method on the response object.
curtis harrison

Joined: Sep 12, 2001
Posts: 8
Again, I'm new to this stuff... What's a servlet? How do you implement it? Pretend I'm REALLY new. Spoonfeeding is appreciated.
Could you give me an example of EXACTLY how to use the sendRedirect() statement works? (ie: Arguements? Where I get the HTTP Path, if needed...)
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

My perspective on the servlet/JSP thing:

A servlet is a java class that provides 'extended behaviour' for a web server. It's like the cgi programs people used to write (and still do) in C or Perl. Among many other uses, servlets can be used to process form submissions on websites, generate 'plain HTML' pages (with dynamic content) or in the JSP world, they are sometimes used as a 'controller'.

Before JSP, if you wanted dynamic data in a HTML page, you'd need to write a servlet that contained a whole bunch of 'out.println()' statements that would print out the HTML tags required. But with JSP, you can instead, write the HTML plainly, and embed the Java code between <% %> tags for the dynamic data. JSP's are then compiled into a servlet for you, and in fact, you should write a simple Hello world JSP, and view the java source file for the generated servlet. It really helped me get a grip on how JSP works. I'd also highly recommend the book "Professional JSP 2nd Edition" by WROX.

In your example, (page 2 that has no output), it makes sense to write this as a servlet, as JSP is primarily meant to make the process of writing dynamic pages easier. But all you really need is a form processor, so a servlet makes more sense. The action target of the form would be the servlet's aliased name.

However, if you don't want to get into writing servlets and fooling around with aliases, writing a JSP that is nothing but Java code between <% %> tags, is an acceptable solution for now.

Finally, sendRedirect() is the solution you'd want, because it literally sends the browser a redirect command.. sort of like if you go to pages that say "you will be automatically redirected, click here if you aren't"

When the browser performs the redirect, it replaces the address bar's URL with the URL to which it is redirecting. So it 'looks' right as well as working right.

I haven't used redirect in a JSP page, but within the code of a servlet, it looks like:
I agree. Here's the link:
subject: Navigating to another page...
It's not a secret anymore!