Solved

Accesing a method from a Servlet1 of a Servlet2

Posted on 2004-04-28
13
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 260
Problem to Alipay 10 94
wild fly 8 startup error 2 79
Find out all repos that a user is most active on Github 1 58
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

734 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