aspose file tools*
The moose likes Servlets and the fly likes Calling another servlet from the current servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling another servlet from the current servlet?" Watch "Calling another servlet from the current servlet?" New topic
Author

Calling another servlet from the current servlet?

Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
I have a servlet called DataServlet. When a call is made to DataServlet I need to make a check to see whethet the user has already logged in. If not I need to make a call in my DataServlet to LoginServlet with the aditional parameters...
like
LoginServlet?&cmd=LOGIN
How do I make a call to another servlet in my currect servlet?
Thanks.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I would strongly recommend against this. A much better solution is to refactor the login authentication functionality into a regular Java class, and use it from both of your servlets.
As a rule, servlet classes should not contain business logic such as this, but instead be largely concerned with selecting the correct classes and methods to call to perform whatever operations are needed.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
I have all the business login in a seperate class but all preior initialization is done in the second servlets init method. So I need to call this method by passing the request and response objects.
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Sorry I also need to pass ServletConfig isn't to the init method....oh I am confused.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Put your initialized login object into the session shared between your two servlets, and if the user is not logged in, redirect to the login servlet. Is this what you want to do?
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
I am not initialising the login object first. I need to call the call that initialises after I make a check that he's not yet logged in.
Manoj Prithiani
Greenhorn

Joined: Nov 03, 2000
Posts: 13
Dear Mr.Bala,
You can call another servlet by using a method of Httprequest and including the output of that servlet in your Dataservlet.
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Hello
I am trying to altr the request attributes on an HttpServlet request object by using the methods:
Hi I am able to that, I used the requestDispatcher's forward method. thanks.
But I have a new problem.
I want to add one more new value to the request. I am using,
request.setAttribute(String,Object)
and then using request dispatcher to forward the request from a servlet to another servlet,
but the added attributes dont seem to forwarded,
how do forward an request object along with the newly added value?
Manoj Prithiani
Greenhorn

Joined: Nov 03, 2000
Posts: 13
Try this code. It may work
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/jsp/temp.jsp?i="+ i);
dispatcher.include(request, response);
regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling another servlet from the current servlet?