Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling another servlet from the current servlet?

 
Bala Krishniah
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bala Krishniah
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I also need to pass ServletConfig isn't to the init method....oh I am confused.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code. It may work
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/jsp/temp.jsp?i="+ i);
dispatcher.include(request, response);
regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic