Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dispatcher.forward(req, res): not able to invoke the jsp page

 
Smitha Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my app description: When I type the url http://localhost:8080/subscribe/subscription?userid=1&wikiid=01
it should invoke the subscription page which has a subscribe button. When I click the subscribe button, it should display the userid and wikiid that I am passing in the url on the display page.

I am using the session object to remember the session values between pages. I have the code and the output below(for debuging purpose). The problem is it's not able to display the subscription page. When traced the output dispatcher.forward(req, res) is not able to invoke the jsp page. Your valued input needed for my code.

Servlet Code:



output:
ut put
===========
15:05:55,562 INFO [STDOUT] PATH INFO: /subscription
15:05:55,562 INFO [STDOUT] INSIDE IF
15:05:55,578 INFO [STDOUT]
USERID: 1
WIKIID: 01
15:05:55,578 INFO [STDOUT] Instanciated bean
15:05:55,578 INFO [STDOUT] IN BEAN
15:05:55,578 INFO [STDOUT]
USERID:1
WIKIID:01
15:05:55,578 INFO [STDOUT] END OF IF
15:05:55,578 INFO [STDOUT] Entering dispatcher
15:05:55,656 INFO [STDOUT] INbetween dispatcher
15:05:55,656 INFO [STDOUT] PATH INFO: /subscription.jsp
15:05:55,656 INFO [STDOUT] NOTHING
15:05:55,656 INFO [STDOUT] Exiting dispatcher
[/CODE]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String target = "error.jsp";
You set the target, but in this case you don't call forward so nothing happens.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your servlet mapping ? If you mapped a forwarded request "/subscription*" to it, you may be in trouble. The fact that "15:05:55,656 INFO [STDOUT] NOTHING" is there shows that the servlet was called once again, but with a different path, maybe "subscription.jsp".
[ February 25, 2008: Message edited by: Christophe Verre ]
 
Smitha Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My servlet mapping in web.xml is


so it should be able to take any string. Then it's not a problem with the servlet mapping. In the code when the execution reaches dispatcher.forward(request, response) it is not invoking the subscription.jsp, but instead it's calling the doget() method of the servlet. This is the main issue. Since the control is passed to doget() method, it's coming to else block. But my question is how to invoke the subscription.jsp page. Why is this not happening?

This code should have taken care of it. then Why ??
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
String target = "error.jsp";
You set the target, but in this case you don't call forward so nothing happens.


15:05:55,656 INFO [STDOUT] PATH INFO: /subscription.jsp
15:05:55,656 INFO [STDOUT] NOTHING
15:05:55,656 INFO [STDOUT] Exiting dispatcher
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your mapping is forwarding every requests to the servlet. What happens is :
1. You enter the url
2. The servlet is called, the path being /subscription, so you enter the if block
3. The request is dispatched
4. The servlet is called, the path being /subscription.jsp, so you enter the else block (remember that you mapped everything to it in your web.xml)
5. Over

Don't map everything to this servlet !
 
Smitha Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate your inputs. Please correct my understanding of your explanation:

Your mapping is forwarding every requests to the servlet. What happens is :
1. You enter the url
2. The servlet is called, the path being /subscription, so you enter the if block
3. The request is dispatched [request is dispatched to the server ?]
4. The servlet is called, the path being /subscription.jsp, so you enter the else block (remember that you mapped everything to it in your web.xml)[So, if it's not mapped to everything then it would have forwared the request to subscription.jsp file ?]
5. Over

So when I tried to map it this way


Now I am invoking as
and for now harcoded the session objects.

it's getting the req.getPathInfo() as null. I expected it to get the result as /subscription.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request is dispatched to the server ?

Yes. It's still server side.

So, if it's not mapped to everything then it would have forwared the request to subscription.jsp file ?

Correct.

for now harcoded the session objects.

Why ?? Try http://localhost:8080/subscribe/subscription?userid=1&wikiid=01. The servlet will still be called, with both parameters being passed. You are misunderstanding the purpose of the "*" in the servlet mapping. You don't need to say "/subscription*" to use request parameters.

it's getting the req.getPathInfo() as null. I expected it to get the result as /subscription.

Yes, it will be null. But why do you need a if/else in the first place ? You want this servlet to be called only when "/subscription" is accessed, don't you ? That's why you map the servlet to "/subscription".
 
Smitha Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. First of all, the reason I had "*" mapped in my web.xml file was I wanted to map all the request to this servlet. In my project, subscription is just one of the feature among many other features. Hence the if else block in my servlet.I am still not sure if this is a bad design and how am I suppose to implement. I am following the MVC architecture, I just read that it's a good practice to seperate the "view"(jsp) from the "controller"(servlet) and this my first time trying the MVC design.

2. So my question was how do I get the display of subscritpion page?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am following the MVC architecture, I just read that it's a good practice to seperate the "view"(jsp) from the "controller"(servlet) and this my first time trying the MVC design.

That's right. Instead of mapping everything to this servlet, you could map "*.do" to it, like Struts, to make it look like a controller. Or "*.action" like Stripes. Anything will do. Even "*.smitha" I don't think it's a good idea to map "*" to the servlet.



You could access it via :
http://localhost:8080/subscribe/subscription.do?userid=1&wikiid=01
or
http://localhost:8080/subscribe/doSomething.do?userid=1&wikiid=01
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's a good idea to map "*" to the servlet.
It's a very bad idea. That cause all requests, even those for resources like images and style sheets, to be routed to the servlet. Not good.
 
Smitha Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the pointers. But another question, can we have more than one servlet for a single application ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have as many as you want. What made you think you were limited to one?
 
Smitha Ram
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every request will be routed through web.xml, either web.xml file will transfer every request to the servlet that is mapped in the xml file. So how do I say map this particular request to this particular servlet in the mapping file incase I am having more than 1 servlet.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would create a different mapping.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic