This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet to servlet" Watch "Servlet to servlet" New topic
Author

Servlet to servlet

Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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!!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ravinder S Edhan
Ranch Hand

Joined: Dec 15, 2003
Posts: 57
Servlet mapping name can be passed in the parameter of getRequestDispatcher() method.


Cheer
Ravinder S Edhan
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

I appreciate if somebody could share complete simple sources demonstrating usage of RequestDispatcher.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Feb 07, 2004
Posts: 472

Thanks, it's exactly what I was looking for.
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

Try:
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

Are you sure the Catetories servlet is working?
Can you hit it directly with a browser?
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Feb 07, 2004
Posts: 472

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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Feb 07, 2004
Posts: 472

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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410




Or vica versa
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

Yes, give that a try
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Dec 11, 2004
Posts: 13410

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")
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5

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
ved gunjan
Ranch Hand

Joined: May 17, 2003
Posts: 86
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet to servlet
 
Similar Threads
Jsps and Servlets
problem in servlet
Request Dispatcher
forwarding to the DoPost part of a servlet
url rewriting