• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Invocation

 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic