GeeCON Prague 2014*
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


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Invocation " Watch "Servlet Invocation " New topic
Author

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).


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

<BODY>

<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">
</form>

</BODY>
</HTML>


LoginServlet.java

SessionServlet.java
[code]
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

public class SessionServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");

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("<html>");
out.println("<head>");
out.println("<title> Servlet 1");
out.println("</title>");
out.println("</head>");
out.println("<body>");

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

out.println("</body>");
out.println("</html>");
}
}

[code]
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
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
Hi,
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...

daniel
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet Invocation