Solved

Accesing a method from a Servlet1 of a Servlet2

Posted on 2004-04-28
13
347 Views
Last Modified: 2013-11-24
Hi everybody:
I have a problem trying to acces a method from one to another servlet.
What I have done is this:
public class Servlet1 extends HttpServlet
{
    public void init() throws ServletException
    {}

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        ServletContext context = this.getServletConfig().getServletContext();
        cdInterface interface = (cdInterface)context.getServlet("Servlet2");
        if (interface.method(5) == true)
             System.out.println("adios");
    }
    public interface cdInterface
    {
       public boolean method(int inNumero);
    }
}
public class Servlet1 extends HttpServlet
{
    public void init() throws ServletException
    {}

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {}

    public boolean method(int inNumero)
    {
        System.out.println("hello");
        System.out.println(inNumero);
        return true;
    }
}

As you can see I'm using context.getServlet("Servlet2");, but in the JBuilder Help it says the following: "Deprecated. As of Java Servlet API 2.1, with no direct replacement... In this version, this method always returns null and remains only to preserve binary compatibility. "  so it always returns NULL.

Is there a way to access method of Servlet2 from Sevlet1.
Hope someone can help me.
0
Comment
Question by:epadilla
  • 6
  • 5
  • 2
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10942698
In the servlet you want to call you can do

ServletContext context = this.getServletConfig().getServletContext();
context.setAttribute("cdInterface", this);

in its init()

Then the first one can do

ServletContext context = this.getServletConfig().getServletContext();

cdInterface cdServlet = (cdInterface).context.getAttribute("cdInterface");

If you make sure the first one loads at startup, it'll be available


0
 

Author Comment

by:epadilla
ID: 10942947
Thanks CEHJ.. but I still have a doubt...
The cdInterface is on the Sevlet1, the servlet that is calling the method... and what I understand in your answerr is that cdInterface is an attribute of Servlet2 the called servlet.. so... where do I have to put that interface? or maybe I havent understand well...
Thanks in advance
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10942973
>>The cdInterface is on the Sevlet1...

It's declared there yes, but Servlet2 must implement that interface or the following won't work:

>>cdInterface interface = (cdInterface)context.getServlet("Servlet2");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10943021
If you want it to implement that interface, you should actually declare that separately and make sure you have such an interface on its own in the classpath
0
 

Author Comment

by:epadilla
ID: 10943088
But anyway... I can't do this:
>>cdInterface interface = (cdInterface)context.getServlet("Servlet2");
because this method is not available for this version as I put in my first question.
And how do Servlet2 has to implement it exactly the same lines I put on Servlet1?
Thanks
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 10943229
>>But anyway... I can't do this:

No, i know, but even if the first thing had not been deprecated, you couldn't have done it anyway. Not can you do this:

>>cdInterface cdServlet = (cdInterface).context.getAttribute("cdInterface");

unless Servlet2 implements cdInterface, i.e.:

public class Servlet2 extends HttpServlet implements cdInterface
{
            // etc.

      public boolean method(int inNumero)
      {
            return false; // the real implementation here
      }

}
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:epadilla
ID: 10943471
Ok, first of all I'm having trouble trying this:
public class Servlet2 extends HttpServlet implements cdInterface
I got: cannot resolve symbol: class cdInterface in class Servlet2.... then another question I have is:
based that this code is in Servlet2:
public class Servlet2 extends HttpServlet implements cdInterface
{
context.setAttribute("cdInterface", this);

     public boolean method(int inNumero)
     {
          return false; // the real implementation here
     }

}

context.setAttribute("cdInterface", this);
0
 

Author Comment

by:epadilla
ID: 10943495
sorry but this continue the question above ...
public class Servlet2 extends HttpServlet implements cdInterface
{
     context.setAttribute("cdInterface", this);

     public boolean method(int inNumero)
     {
          return false; // the real implementation here
     }

}

don't this line "context.setAttribute("cdInterface", this);" have to be like this:
context.setAttribute("method", this);" ???????????
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10943529
>>cannot resolve symbol: class cdInterface in class Servlet2....

As i mentioned earlier, that interface must be available in the classpath

Also as i mentioned earlier

>>...in its init()

Servlet2 must save its own reference as a context attribute during the init() method

0
 

Author Comment

by:epadilla
ID: 10943642
>>cannot resolve symbol: class cdInterface in class Servlet2....
As i mentioned earlier, that interface must be available in the classpath
---- how can I make that interface available in the claspath..???

And about..... Also as i mentioned earlier
>>...in its init()
I understand this part... but my question was that the thing that have to be set as attribute is the "cdInterface" or "the name of the method I want to use"????  
"context.setAttribute("cdInterface", this);"  or   "context.setAttribute("method", this);"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10943695
>>---- how can I make that interface available in the claspath..???

Depends on the context in which you're working. If you're talking about at compile time, just like any other class. If runtime, it should be in the classes directory of the web app

>>but my question was that the thing that have to be set as attribute is the "cdInterface"

Yes. This is the reference to the interface/Servlet2. It doesn't actually matter what name you use. It's just logical to use "cdInterface" is that's what you're calling
0
 
LVL 92

Expert Comment

by:objects
ID: 10944099
You should move the method you want to call into a seperate class or mmake it a static method, then you no longer need an instance of the servlet to make the call.
0
 
LVL 92

Expert Comment

by:objects
ID: 10954608
I think you'll run into problems with that approach, and I don't see any purpose for the added complication. Anyways if you do have problems, post a question and I'll help sort it out for you :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now