• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Servlet to servlet

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I sincerely hope this is the correct forum, I was torn between this one and the Java Beginner... hopefully it's alright.

Anyway, I want to be able to forward control over to another servlet after the first one is done processing.

Ex.
RequestDispatcher view = request.getRequestDispatcher(****);
view.forward(request, response);

In place of the ****, would I be able to put the servlet mapping name? Or would I have to do something like "/servlet/myServlet"?

Secondly, would I be able to include parameters?

Ex.
RequestDispatcher view = request.getRequestDispatcher("myServlet?genreID="+ID);
view.forward(request, response);

Thanks to all who reply!!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the right forum.

If you use getRequestDispatcher than you would use the servlet mapping.
If you use getNamedDispatcher than you would use the servlet name.

Look at the "See Also" section near the top for links to the different methods for retrieving dispatchers.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/RequestDispatcher.html

Parameters are passed by binding objects to a chosen scope via setAttribute and getAttribute. The scope can be session, context, or request.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet mapping name can be passed in the parameter of getRequestDispatcher() method.


Cheer
Ravinder S Edhan
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate if somebody could share complete simple sources demonstrating usage of RequestDispatcher.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by D Rog:
I appreciate if somebody could share complete simple sources demonstrating usage of RequestDispatcher.



http://simple.souther.us
Look for SimpleMVC.
It is both simple and complete.
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it's exactly what I was looking for.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm not able to forward from one servlet to another still. Would I have to get the servlet context first even though I'm using the servlet's mapping?

Here is my servlet declaration in my deployment descriptor:


my mapping:


in the servlet that calls the Categories servlet I have:
RequestDispatcher view = request.getRequestDispatcher("Categories");
view.forward(request, response);

should I have this instead?
ServletContext ct = getServletContext();
RequestDispatcher view = ct.getRequestDispatcher("Categories");
view.forward(request, response);

Thanks to any and all who can help me here!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try:
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It still doesn't work!! My starting servlet works just fine, and then forwards over to the Categories servlet. I have a
System.out.println("Inside the categories servlet"); line inside my categories servlet. This never gets printed and the end result is an error similar to a 404 page not found type of error.

This is driving me nuts...

Thanks again for the help Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure the Catetories servlet is working?
Can you hit it directly with a browser?
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it works. The Categories servlet is used to preload the active music categories. It is called whenever the user goes to the homepage which calls /index.html which is one of Categories' mappings.

I'm working on the admin part now where an admin can activate, deactivate and add categories in which case, refreshing the list of categories which is pulled from the DB and saved to the application scope of the servlet context.

Here is my categories servlet's doGet method:
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally for forward, I'd suggest to use response.sendRedirect(); I do not see in your example that 1st servlet does some out. What's servlet container you are using?
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Tomcat. My 1st servlet hasn't been posted on this thread as of yet. That servlet, AdminController, just updates a row in my DB and then closes it's DB connection and forwards over to the CategoryController servet. When I run this, the update works just fine, but the redirect never makes it.
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since your 1st servlet just does database update, then sendRedirect should work perfectly for you. My entire project runs on this technique. Since sendRedirect is available from lower versions of JSDK specifications, then a project can run on older versions of a servlet container.

Anyway, your case looks interesting for me so, I'll try to reproduce it on my servlet container.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jason Kwok:



Just to clarify... the path variable read in from the request is always null when the servlet is called using the index.html mapping. This works just fine and contines to do so. Only from my admin servlet is the "path" attribute given a value, solely to allow the Category servlet to refresh the category list and as you can see above, forward the control back to admin edit screen.

So bottomline, in a browser, going for the index.html always works just fine. The problem is trying to have one servlet forward control over to the category servlet so it can refresh my category list.

Please help!! Thanks again to all!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you making a POST request to servlet A and then forwarding to a servlet that only implements the doGet method?

If so, in Servlet B (Catetories, I think) implement doPost.
You don't have to duplicate the code, just have doPost call doGet.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
Are you making a POST request to servlet A and then forwarding to a servlet that only implements the doGet method?

If so, in Servlet B (Catetories, I think) implement doPost.
You don't have to duplicate the code, just have doPost call doGet.



Yes that's exactly the case. Form data is posted to servlet A and that servlet updates the DB, then it tries to forward to the Categories servlet, all of which is done from inside servlet A's doPost method. I have made the doPost method in Categories to simply return the request and response to the client should they try to post to Categories.

So, in short, how do I have doPost call doGet? I wouldn't use the RequestDispatcher?

Ex.
RequestDispatcher view = request.getRequestDispatcher("Categories");
view.forward(request, response);
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Or vica versa
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok... now I'm a little lost... I would put that inside my doPost method in servlet A?? I already have a doGet method which does some other processing in servlet A!!

would sendRedirect work?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, in servlet B.

It's the easiest way to make sure that the servlet behaves exactly the same way regardless of whether it was called via doGet or doPost.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Ben... hopefully this will be my last question...

so in my Categories servlet, I currently only have a doGet method which as previously stated, retrieves a current list of active categories. Now, because servlet A is using doPost and forwarding to this servlet, I should include a doPost method in my Categories servlet to this effect:??

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, give that a try
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Success!! It's reaching the Categories servlet now, but the 'path' variable is coming across as null everytime, when servlet A sets gives it a value before it forwards.

Should the doPost method in Categories assign path a value instead?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is servlet A setting it?
I would bind it to request scope:






[Removed evidence of typing while tired]
[ May 04, 2005: Message edited by: Ben Souther ]
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
How is servlet A setting it?
I would bind it to request scope:






[Removed evidence of typing while tired]

[ May 04, 2005: Message edited by: Ben Souther ]



Yeah I have the path variable bound to the request scope just as you had specified.

And I have the very same getAttribute call in both the doPost and doGet methods in servlet B, but it always comes back null.

Thanks again for all your help Ben!!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that I think about it, you shouldn't even need to bind it as an attribute.
Both servlets will have access to the same request object and parameters are readonly to the servlets.

How are you sending the "path" parameter? Describe it, all the way from the HTML page to the second servlet.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Starting with a form on a JSP page, attributes specifically just to be added to the database are sent to my Admin servlet. The admin servlet checks the data to make sure it's up to par, then adds or updates the categories on the database.

Finally, the admin servlet creates the 'path' variable as such:
request.setAttribute("path", "admin");
which makes a string with the value admin and forwards over to the categories servlet.

The categories servlet just reads from the database to get all the active categories and then binds the result to the servletcontext application scope. The path variable is checked, and if it's null, then the user is forwarded to the index page, other if it's not null... go back to my admin JSP page. But the path variable always comes across as null.

I put String path = (String) request.getParameter("path"); in both the doGet and doPost methods of my Categories servlet, and still only get null.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh, I see now.

getParameter is for reading values parameters send from the webpage.
It will not read values bound to scope with setAttribute.

Your 'categories' servlet should be reading the path variable by using:
request.getAttribute("path") not getParameter("path")
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

does NOT set a parameter, it sets an attribute. When processing that request in the next servlet you must use getAttribute("path")
Parameters come from the original request and are immutable - attributes can be added during request processing.
Bill
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
This is the right forum for your topic
Regarding your qur\ery consider the below statement":
Servlet mapping name can be passed in the parameter of getRequestDispatcher() method.
You should also take care of the relative and the absolute path when u use RequestDispatcher.
 
A teeny tiny vulgar attempt to get you to buy our stuff
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic