Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet to servlet communication

 
ravi ckumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , i trying to do servlet to servlet communication using the weblogic server, i have written both the files & kept the class files in the myserver/servletclasses files, i also registered them in the server properties files , but im gettin the followin exception when i run my second servlet , in which im trying to get the first servlet instance & call a method in it,
java.lang.NullPointerException
at s9.service(s9.java:18)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:1at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletContextImpl. at weblogic.servlet.internal.ServletContextImpl.invokeServlet(ServletContextImpl. at weblogic.servlet.internal.ServletContextManager.invokeServlet(ServletContextMa at weblogic.socket.MuxableSocketHTTP.invokeServlet(MuxableSocketHTTP.java:361)
at weblogic.socket.MuxableSocketHTTP.execute(MuxableSocketHTTP.java:261)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java, Compiled Code)
the following is my first servlet file
// servlet to servlet Communication
import javax.servlet.*;
public class s8 extends GenericServlet
{
public void service(ServletRequest req, ServletResponse res)
{
try{
ServletOutputStream sos = res.getOutputStream();
sos.println("this is the first servlet");
}
catch(Exception e) {e.printStackTrace();}
}
public String display()
{
return "from the first servlet value sent = ";
}
}
and the second servlet file
// second servlet for servlet to servlet communication
import javax.servlet.*;
public class s9 extends GenericServlet
{
public void init(ServletConfig s )
{
try{super.init(s); }
catch(Exception e) { }
}
public void service(ServletRequest req,ServletResponse res)
{
try
{
ServletContext con = getServletContext();
s8 s = (s8)con.getServlet("s8");
String f = s.display();
sos.println(f);
ServletOutputStream sos = res.getOutputStream();
sos.println("second servlet");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
can anybody pl explain
thank u
ravi
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "getServlet" method represents a security and maintenance problem, so it has been deprecated in the the last few servlet API releases. Althouh callable it will always return null.
A much better solution for communication between servlets in the same context is to put objects into the servlet context (it works like a Hashtable or Map) which may then be accessed by all servlets in the context.
In your case I recommend that you define an interface:

and mark your servlet s8 as "implements Displayable".
In the "init" method of s8 and put "this" it into the context with a name (say "s8"). You can the retrieve the value of "s8" from the context in servlet s9, cast it to a Displayable and call the "display" method.
Also bear in mind that you can't print to the stream "sos" before you have created it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic