Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Accesing a method from a Servlet1 of a Servlet2

Posted on 2004-04-28
13
Medium Priority
?
360 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 750 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
 

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

572 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