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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Invocation " Watch "Servlet Invocation " New topic

Servlet Invocation

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello friends
I have very small doubt. I am giving you the following code where it is possible for me to instantite one servlet from another one if this is possible why we require .
The code is working fine(I am using Weblogic 6.1).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">


<form method="post" action="http://localhost:7001/LoginServlet">
Enter your UserName:<INPUT TYPE="text" NAME="user"><br>
Enter your Password :<INPUT TYPE="password" NAME="pass"><br>
<INPUT TYPE="submit" value="Sign-Up">

import javax.servlet.*;
import javax.servlet.http.*;


public class SessionServlet extends HttpServlet
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException

PrintWriter out=response.getWriter();

HttpSession session=request.getSession(false);

String username=(String)session.getAttribute("User");
String password=(String)session.getAttribute("Password");

String code=request.getParameter("Name");

out.println("<title> Servlet 1");

out.println("Your Name is: "+username);
out.println("Your Password is" +"********");
out.println("Your Code is " + code);


Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 160
Creating an Instance of an Servlet and invoking its methods may seem to be correct in a java perspective.

But look at an web application perspective, in which you never know, whether the request to the servlet is comming in get method or post method. Since these two methods have their own pros and cons, it will not be right to invode only one method for whatever kind of request.

Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
I think we can initiate another servlet inside a servlet.
But we have to remember that some methods inside our servlet are callback methods. It is called by web container.
Web container have responsible to manage every request from client and send that request into proper servlet (and its methods)..

Correct me if I am wrong
Hope this help you...

Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Well, yeah... it will generate your expected output... but why would you want to set it up this way? It seems like bad practice for three reasons:

First, as far as I can tell, there is no reason for SessionServlet to be a servlet at all. It could just as easily be a plain old java object that you pass the request and response object to. You really aren't using any of the servletness functionality.

Second, you shouldn't be calling the doGet() method directly... It is the job of the container to look at the request header and determine which service method to call.

Lastly, just because you have access to the request and response object does not mean that the servlet was initialized correctly. Test the ServletContext and see if it holds a reference to the same context as the rest of the application. I would guess that it doesn't because the container never called the init() method.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
I agree. Here's the link:
subject: Servlet Invocation
It's not a secret anymore!