This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes accessing servlet config object from the servlet class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "accessing servlet config object from the servlet class" Watch "accessing servlet config object from the servlet class" New topic
Author

accessing servlet config object from the servlet class

rai talari
Ranch Hand

Joined: Jan 31, 2006
Posts: 34
hi ,
i want to access the servlet config object in the servlet which i created by extending the HttpServlet class.the problem is we cant create instance to the servlet then how to call the getServletConfig() method defined in the GenericServlet class.we cant call that with the class as it is not static .it sounds simple but i am facing hell out of it.any input is appreciated.
Thank You
[ June 26, 2006: Message edited by: rai talari ]

nothing is impossible in the world even the word impossible sounds as I'M'Possible.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by rai talari:
hi ,
...the problem is we cant create instance to the servlet then how to call the


I'm not sure what you mean here.
The container will instanciate the servlets for you.
HttpServlet extends GenericServlet so you can call any it's methods from your servlet.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
rai talari
Ranch Hand

Joined: Jan 31, 2006
Posts: 34
-----------------------------------------------------------------------
I'm not sure what you mean here.
The container will instanciate the servlets for you.
HttpServlet extends GenericServlet so you can call any it's methods from your servlet.
------------------------------------------------------------------------
my view is that we are unable to get the reference for the instance then how to call that method - with class name?

[ June 26, 2006: Message edited by: rai talari ]
[ June 26, 2006: Message edited by: rai talari ]
rai talari
Ranch Hand

Joined: Jan 31, 2006
Posts: 34
I wrote a piece of code to display the version,name,etc of servlet


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Freak extends HttpServlet {
System.out.println(Freak.getServletInfo());//getting error

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Eclipse Remote Debugging");
}

}
i am getting an error like

Freak.java:13: <identifier> expected
System.out.println(Freak.getServletInfo());
what is wrong ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by rai talari:

public class Freak extends HttpServlet {
System.out.println(Freak.getServletInfo());//getting error


You've written that line of code outside of any of the method blocks.
Override the init method and put the same line of code in there.

You servlet doesn't have access to any of the container resources until it's been initialized by the container.

javax.servlet.ServletConfig&#41;" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/GenericServlet.html#init(javax.servlet.ServletConfig)
rai talari
Ranch Hand

Joined: Jan 31, 2006
Posts: 34
Originally posted by Ben Souther:
[QB]

You've written that line of code outside of any of the method blocks.
Override the init method and put the same line of code in there.

_________________________________________________________________
Ben, thanks for your intrest i kept the line of code in the init() method still it is giving error here is the code
public void init() throws ServletException
{
System.out.println(Freak.getServletInfo());
}
error
Freak.java:9: non-static method getServletInfo() cannot be referenced from a sta
tic context
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

System.out.println(Freak.getServletInfo());


If you're following normal coding conventions, which it appears you are, then you're trying to call the class's getServletInfo as a static method.

The getServletInfo metho is not static (and couldn't be).
Try:


The 'this' keyword provideds a reference to the instance of the servlet (or whatever Java class you're working with).

From the nature of your code samples, I'm guessing that you've jumped into servlet programming without first spending some time with core Java.
You might find that you'll progress faster if you step back from servlets for a while and follow some Java tutorials or pick up a book on code Java programming.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing servlet config object from the servlet class
 
Similar Threads
init(ServletConfig config) query
why super.init() is used??
Servlet context and config
non-static reference in singleton
Clarification abt init()