aspose file tools*
The moose likes Servlets and the fly likes dispatcher.forward(req, res): not able to invoke the jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "dispatcher.forward(req, res): not able to invoke the jsp page" Watch "dispatcher.forward(req, res): not able to invoke the jsp page" New topic
Author

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

Smitha Ram
Greenhorn

Joined: Feb 06, 2008
Posts: 6
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

Joined: Mar 06, 2001
Posts: 13459

String target = "error.jsp";
You set the target, but in this case you don't call forward so nothing happens.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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 ]

[My Blog]
All roads lead to JavaRanch
Smitha Ram
Greenhorn

Joined: Feb 06, 2008
Posts: 6
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Feb 06, 2008
Posts: 6
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Feb 06, 2008
Posts: 6
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jan 10, 2002
Posts: 61661
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Smitha Ram
Greenhorn

Joined: Feb 06, 2008
Posts: 6
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

Joined: Jan 10, 2002
Posts: 61661
    
  67

You can have as many as you want. What made you think you were limited to one?
Smitha Ram
Greenhorn

Joined: Feb 06, 2008
Posts: 6
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

Joined: Dec 11, 2004
Posts: 13410

You would create a different mapping.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dispatcher.forward(req, res): not able to invoke the jsp page