This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes extending a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "extending a servlet" Watch "extending a servlet" New topic
Author

extending a servlet

PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
I want a class (a regular class not a servlet) extend another servlet to use the objects in the servlet. When I tried it, it looks like something is going wrong. Is this really recommended? What are the rules that I have to follow if this is possible? Do I have to implement init, service and destroy in this case?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If it extends a servlet, then it is a servlet.
I'm guessing that, if you tell us what you're trying to accomplish, we can recommend a better way.
Remember all of the methods in the Servlet and HttpServlet interface are really a form of callback method. The container controls the lifecycle of the servlet and calls these methods.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Oh, and to answer your question exactly, no, you do not need to override any of the methods in a servlet to get it to compile.
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
Thank you for answers. What I wanted to do is that I have few things on the ServletContext of the parent servlet. I just want to use the same objects I set by
getServletContext().setAttribute("attributeName");
in the parent servlet.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not sure I understand what you just said.
You do know that objects bound to context scope are available to all JSPs and servlets in your application (regardless of inheritance structure), right?
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
In the parent servlet I am doing a
getServletContext().setAttribute("attributeName", attributeValue);
In the class that extends the parent servlet I am doing a
super.getServletContext().getAttribute("attributeName");

But getting the same attribute in the child class fails. What is wrong in this. How can I get the same attributes I set in the parent servlet?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is only one context object.
Any servlet calling its getAttribute method will get the same thing.
There is no need to call super.getServletContext() because subclasses (and any other servlet or JSP in your app) will all use the same context object.
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
Thank you for the answer. But even if I do
getServletContext().getAttribute("AttributeName");
in the child class, it fails. I have the statement
getServletContext().getAttribute("AttributeName");
in a method in the child class and I am calling this method from a jsp.
Any clue you can give me will be a great help.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How do you know it's failing?

Post the actual code used to bind the object to scope and then the code used to retrieve it.
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
I think it is failing because the jsp is not getting compiled. If I comment out this statement to get this attribute from the servletContext, the page is getting rendered.
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
May be since this class extends another servlet, this class has to be in web.xml?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What JSP is not being compiled?

Are you doing this in a JSP scriptlet or in a servlet?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ben Souther:


Post the actual code used to bind the object to scope and then the code used to retrieve it.
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
I do have a servlet, say Servlet1. And I also have a class, say Class1 that extends Servlet1. In the init of Servlet1, I set some attributes to the ServletContext.
In my jsp, I am calling a method, say method1 which is in Class1. In method1, I am trying to get the attribute I set in Servlet1 using code

getServletContext().getAttribute(�attributeName�);

For some reasons, the jsp is not getting compiled. But if I commented out
getServletContext().getAttribute(�attributeName�);
the jsp is getting compiled.
I wrote this just to clarify the issue. Please ask me if this is not clear.
I will post the code.
I appreciate your effort to solve this issue.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ok, I think it's getting clearer.

#1 and this is important:
You should not be calling servlet methods from a JSP.
There is no need to do this.

#2 Extending servlet will make your class a servlet but that class will not have references to container managed objects (like servletContext) unless it is instanciated by the container. If it is instanciated by the container, you aren't really going to have a way to access it from your JSP anyway. Servlets weren't meant to be used this way (see #1).

If you want to move your logic out of your servlet and JSP, which is a good idea, put it in a plain old Java object. (POJO) Then instanciate the object from within your servlets doPost or doGet method. If that object needs something (like an object that is in context scope) get a reference to that object from your servlet and pass it to the POJO. This way your POJO doesn't need references to container managed resources (like servletContext).

Once you've done this, call your POJO's method from the servlet. Then, either bind the POJO to request scope or, if the POJO's method returns Java objects (beans) bind them to request scope. Doing this will allow your JSPs to access these objects.
Finally, use EL and JSTL from within your JSP to access the properties of the objects that you've bound to request scope.

We have a very simple MVC application in our
codebarn that does some of this.

If you want some more involved samples, I have some on my site:
http://simple.souther.us Look on the Not So Simple pages for more advanced projects with POJOs that return complex results in beans.
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
In the Servlet1 I have
public void init(ServletConfig config)
throws ServletException {
logger.info("Starting init process");
super.init(config);



getServletContext().setAttribute("communities", this.community);


}



public class Video extends Servlet1{

public void getVideo(){

// the jsp works if I comment out lines before System.out.println
Enumeration en = getServletContext().getAttributeNames();
while (en.hasMoreElements() ){
System.out.println(en.nextElement());
}
System.out.println("in getFlashVideo");



//code to get the video goes here.
}

}


In the jsp I have

<%}else if(request.getParameter("show_video").equalsIgnoreCase("true") ){
Video fv = new Video();
fv.getVideo();
System.out.println("VIDEO");
out.println("video");
%>


The jsp is not getting compiled. But if I comment out the statements to get from the servlet context, the jsp is getting compiled.

Any solution is highly appreciated.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think our posts just crossed.
Scroll up and look at my last response.
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
I thank you very much Sir for your solution. Here the main issue is I am not supposed to change anything in the Servlet1. But I need to have access to the attributes set to the servletContext. Is there any way to have access to the objects in the servletContext other than coding in the jsp or changing doGet or doPost?
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
Thank you very much. You took so much time to answer my question. I really appreciate that.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Without seeing what's there it's hard to say.
If you have to code from the JSP, you can certainly minimize the amount of work done in it by putting as much code as possible in a bean.
Instanciate the bean from the JSP and pass either a reference to the ServletContext to it or pull the objects from the servlet context and pass them to the bean.

Why don't you have access to the first servlet?
PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
Because the developer who coded the Servlet lost the source. Now we don't have the source code, we have only class file. Or the source file we have is much older than the class file on production.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

As long as you have the original developer's permission, (or if you or your company owns the rights to that code) you could always decompile the original with a tool like Jad.
[ September 13, 2007: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

And, not to be a lock salesman hanging around after the horses have been stolen, but........
You might also want to look at Subversion, for source control.

PradeepPillai Pradeep
Ranch Hand

Joined: Nov 15, 2005
Posts: 184
This servlet has more than 3000 lines in it. I tried dj java (decompiler), the java file created was not perfect. It requires lot of effort to fix it. I will try the one you said.
Thanks a lot.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: extending a servlet
 
Similar Threads
How to give a path name in web.xml
Why should be public?
jspServlet doesnt work
servlet not loading
issue with web.xml