aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes <url-pattern>/</url-pattern> and response.sendRedirect( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "<url-pattern>/</url-pattern> and response.sendRedirect("index.html")" Watch "<url-pattern>/</url-pattern> and response.sendRedirect("index.html")" New topic
Author

<url-pattern>/</url-pattern> and response.sendRedirect("index.html")

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Hello everyone!
Need some help why my below code gives this error:
"The page isn't redirecting properly"

I'm running the application through: http://localhost:8080/3/

Thanks in advance for any help.

index.html


web.xml


CoffeeSelector.java
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Just a guess,
Try closing writer stream before redirecting.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Why are you writing something to the response before doing a redirect?? Move lines 15,16 and 17 to the else block...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Ok, I followed your suggestions but it's still the same behavior:
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Faisal Pasha wrote:Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

This is an error which I think chrome browser reports. Are you redirecting a request too many times??
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Yes, it's from Chrome. However, I didn't do redirection multiple times. I'm still following the same code that I posted initially but with changes suggested by you. I still don't understand the reason behind such error.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Faisal.

Here program logic is correct. When we made a request like then container sees the url-pattern here it is '/' and mapps it to the servlet 'CoffeeSelector' calls doGet() which calls doPost(). The 'doPost' checks the request parameter 'coffee'. Here it is 'null'. Then it redirects to the static source 'index.html'.

Here container sends the response header as 'http://localhost:8080/3/index.html'. Here we have override the default servlet that is present in 'conf/web.xml'. The main purpose of the default servlet is to serve static resources like html files, image files etc.
Suppose when a request comes to the servlet like If we didn't override the default servlet, then the default servlet checks the type of the resource, wether it is static or dynamic resource. If it is static resource, it will handle and serve to the client.

Here we have overriden the default servlet's protected methods, doGet and doPost. Now the browser redirected as Now the default servlets method's that handles wether the resource is static or dynamic resource's will be called(since we have not overriden those methods) and decides wether it is static or dynamic resource. Here it is 'yes' calls the overriden doGet method and which calls doPost method. Now it checks the request parameter 'coffee' here it is 'null' and redirect the static resource as Again the browser sends the request to the container and the container decides the type of resource, the procedure is same as above. This recursion will be done many times. Browser will not be able to display the response. It will not be able to handle this. So it displays the message to the user as
The page isn't redirecting properly
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Thanks so much Chinmay! Your reply cleared my doubt. But, I have a new question based on your reply - in my program, for default servlet, is 'index.html' static/dynamic? You said about default servlet's methods that check whether a resource is static/dynamic; could you tell what are those methods? Is there any web link where I could understand more about default servlet's behaviour?

Thanks so much, again!
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Faisal.

See this link.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <url-pattern>/</url-pattern> and response.sendRedirect("index.html")