Solved

Accesing a method from a Servlet1 of a Servlet2

Posted on 2004-04-28
13
354 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

713 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