This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to configure sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to configure sendRedirect()" Watch "How to configure sendRedirect()" New topic
Author

How to configure sendRedirect()

Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
I have 1.html and 2.html in "WebContents" folder and 3.html(now deleted*) and 4.html in "WebContents/wc" folder.
I can retrieve 1.html and 2.html but now I want to read the incoming request url for 3.html and redirect to 4.html but am facing 404 error at this step.
How can I correct this.Also, in what other ways can we redirect client browser to urls other than by sendredirect or setting filter flags and error page flags in server side programming.I saw something like setting properties file: is it related?
Thanks.
Arun Giridhar
Ranch Hand

Joined: Mar 10, 2012
Posts: 145

3.html is Deleted ? , what did you expect ?

On the other hand

You can get the ServletPath and then response.sendRedirect(URL);


hate Professionalism
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
Whether 3.html is deleted or not it is immaterial to my use case as I want to skip all requests to 3.html and redirect to 4.html but when request to 3.html is being called from 2.html, i am receiving 404 implying that response is being made which I am unable to catch within the servlet and redirect it . The path is gave is just hard coded instead of any variables using servletpath() etc but the path is correct.
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
It looks like its not a problem with sendredirect() as even 2.html(although i din't mention the code related to 2.html in the post) or 3.html or 4.html are not being caught at the server side i.e at the servlet:b.java.How do I implement this?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

I dont understand what the request dispatcher is trying to achieve. The request and the forward are identical for 4.html.
And if you are doing a sendRedirect() for 3.html, you should see the URL changing to 4.html in your browser. If that is happening right, then you have problem in the directory structure/spelling.

SCJP, SCWCD.
|Asking Good Questions|
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
1. Yes, the request and response are same. I was assuming that request could have been a query string(in my case) and response a response object like a html page which will be sent but it seems like my understanding is wrong.

2. Also, to what I have coded, I see that when a request query is fired (say 2.html request by clicking link in 1.html) from the client's browser, the html page is being serviced without the servlet having the control of this behaviour "of" fetching the page by reading the uri from request and providing it to client.Irrespective of my above misunderstanding of request_dispatch, this is what I wanted to have, which I am unable to figure out how to code.

3.This issue is partly irrelevant.Now, If I change web.xml's url-pattern to /* instead of /test/* and provide response.sendRedirect("/test/wc/3.html"); in the doPost(), I am receiving too many redirects error for the former pattern(/*) and receiving 3.html page in the browser in the latter(/test/*) so, how does this work?
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
To explain why I had to code thusly:
As I understood , when a redirect is sent, the client will be forced to request the newly set url which will be fired to the server where the servlet can read the request and decide whether to serve it or not.
So, I wanted to send a redirect signal to query for 4.html when the client requests for 3.html so that when the client's browser will request for it, the servlet can service it.

The issue I am facing is although I am reading the request URI's and match them to my use cases , that piece of code is not working.I want to know how to implement this.

So, here I am saying to redirect to 3.html for all request except when it is 3.html redirect to 4.html with the view that the else response will first reach clients browser from which a request will be made to fetch 3.html and this request will be received by the servlet where it will redirect to 4.html without serving the 3.html file as in the if block.

But the problem I am facing is : be it 1 or 2 or 3 or 4 .htmls, their request uri's aren't being caught by the servlet and the html's are being fetched by the client automatically when the 1.html loads and subsequent steps are run.

Now, If I change web.xml's url-pattern to /* instead of /test/* and provide response.sendRedirect("/test/wc/3.html"); in the doPost(), I am receiving too many redirects error for the former pattern(/*) and receiving 3.html page in the browser in the latter(/test/*) so, how does this work?

To summarize and precise: I want to know how to capture the uri request being made [ from the client to the server] at the servlet's end and decide according to my usecase whether to service it or otherwise.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60773
    
  65

In what way does the getRequestURI() method not fit your needs?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Arun Giridhar
Ranch Hand

Joined: Mar 10, 2012
Posts: 145

Jack Weasels wrote:
But the problem I am facing is : be it 1 or 2 or 3 or 4 .htmls, their request uri's aren't being caught by the servlet

Fact in your case
Jack Weasels wrote:
Now, If I change web.xml's url-pattern to /* instead of /test/* and provide response.sendRedirect("/test/wc/3.html"); in the doPost(), I am receiving too many redirects error for the former pattern(/*) and receiving 3.html page in the browser in the latter(/test/*) so, how does this work?

/* >> why you need to goto servlet over again and again ?? , i never use /*


Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22

Now I have come this far where I can now redirect to 4.html but I am unable get any output of the 4.html on the browser.(there is no source code as well which is obvious). But if I use getWriter(), I can see the text "good" on the page. So, how do I display the content in 4.html when the page is loaded.I tried both forward and include but the response seems to be null as I get a blank page.

In order to catch the uri's in the servlet for 3.html I changed the url-pattern in web.xml to /wc/* but I can't understand why /wc/* worked where the uri is /test/wc/3.html while /test/* din't work for the same uri (my project name is test and wc is a folder in test).
Jack Weasels
Greenhorn

Joined: Jan 04, 2012
Posts: 22
@Bear, I only saw your reply now.
Yes, getRequest uri will do the job, I was only doing for myself.Could you look at my last post and help me understand what I am missing out on.Like, is there a way at all to send html file as response object in the dispatcher or there is only the following ways through which any data can be sent to client which are: PrintWriter or ServletOutputStream.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Jack Weasels wrote:Like, is there a way at all to send html file as response object in the dispatcher or there is only the following ways through which any data can be sent to client which are: PrintWriter or ServletOutputStream.

From the design perspective, the job of a dispatcher is to delegate/transfer control for further processing.
I would not prefer it just to redirect to some page. You implementation is valid in a case where the actual URI on server is different from what the client should see.
For a plain static page redirect, you can write some Javascript code on the link itself to do the trick.
The ServletOutputStream is not really useful for writing out text data.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to configure sendRedirect()
 
Similar Threads
JSTL c:forEach ServletException
Problem with JSPs in web application
HFSJ book: Cookie example chapter 6 plz Help :(
EL question
EL & Maps