• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a guess,
Try closing writer stream before redirecting.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you writing something to the response before doing a redirect?? Move lines 15,16 and 17 to the else block...
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Faisal.

See this link.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic