wood burning stoves*
The moose likes Servlets and the fly likes Error while getting servlet context in Java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error while getting servlet context in Java code" Watch "Error while getting servlet context in Java code" New topic
Author

Error while getting servlet context in Java code

Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
Hi,

I am trying to get the context object in java model class as follows, but it says getServletContext is not resolved? Any idea.. please help me.


package model;

import model.Database;
import javax.servlet.ServletContext;
import javax.servlet.*;
import javax.servlet.http.*;


public class VerifyCredentials {

public boolean Verify(String username,String password)
{

boolean flag = false;

Database db = (Database)getServletContext. // getting error here itself, so didnt write the full code.


}



}






Thanks n Regards,
Nagaraj S K
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
Hi,

If i put the code as Database db = (Database)getServletContext().getAttribute("db"); also error is coming up..


Nagaraj Shivaklara wrote:Hi,

I am trying to get the context object in java model class as follows, but it says getServletContext is not resolved? Any idea.. please help me.


package model;

import model.Database;
import javax.servlet.ServletContext;
import javax.servlet.*;
import javax.servlet.http.*;


public class VerifyCredentials {

public boolean Verify(String username,String password)
{

boolean flag = false;

Database db = (Database)getServletContext. // getting error here itself, so didnt write the full code.


}



}





Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Perhaps this would be best asked in the Servlets forum? Moving there now.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60821
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60821
    
  65

Your class has not getServletContext() method, so of course it will cause a compilation error.

This is basic Java. Methods are called on instances of objects that define that method.

I'm really confused as to what you are even trying to accomplish. Why would you cast a ServletContext to a Database (whatever that is), and why would even be thinking about the servlet context in a model class?
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
Bear Bibeault wrote:Your class has not getServletContext() method, so of course it will cause a compilation error.

This is basic Java. Methods are called on instances of objects that define that method.

I'm really confused as to what you are even trying to accomplish. Why would you cast a ServletContext to a Database (whatever that is), and why would even be thinking about the servlet context in a model class?



Hi Bear,

Since all the database related stuff must be in model classes( i mean to follow MVC) .. i thought of getting the db connection reference from the context listener. For this i added db related stuff in context param vaues and created MyServletContextListener class from there i called Database class to achieve the connection. In MyServletContextListener class i wrote like this :

public void contextInitialized(ServletContextEvent event)
{
ServletContext sc = event.getServletContext();

String url = sc.getInitParameter("url");
String username = sc.getInitParameter("username");
String password = sc.getInitParameter("password");
String dbname = sc.getInitParameter("database");

Database db = new Database(url+dbname,username,password);
sc.setAttribute("db", db);
}

Now in the mentioned class (VerifyCredentials, posted earlier) need to get the database reference so trying to get through context..

I hope you understood, please let me know if this does not clear your doubt. Is this possible to get context parameters in classes apart from servlets?
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Nagaraj Shivaklara wrote: Is this possible to get context parameters in classes apart from servlets?

Not that I know of. You class has to be a servlet in order to get the ServletContext. Just randomly calling getServletContext() in a non-servlet class won't do anything...


SCJP 6 || SCWCD 5
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
ServletContext is a plain java object and can be accessed from any java class if
1. You either set it after constructing the object
2. Pass it as a parameter to the Constructor of the class you instantiate.

However it is not a good idea to inject into a plain java class with any Servlet API related classes.

For the below scenario i will sucggest the below.

1. Define a ServletContextListener where you instantiate the Database class and store it in a servlet context (you seem to have already done this part).
2. Inject this Database class from the controller into this VerifyCredentials class either using a setter (after instantiation) or constructor.
3. Invoke the verity method.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60821
    
  65

Once again: please use code tags as I advised above!

Nagaraj Shivaklara wrote:
Since all the database related stuff must be in model classes( i mean to follow MVC) .. i thought of getting the db connection reference from the context listener.

If your model has any dependency whatsoever on the servlet environment, it's most decidedly not MVC!

Your model should have NO knowledge of what's powering the UI.

At the UI level, grab the information from the context and pass it down to the model layer as parameters.
Nilesh Miskin
Ranch Hand

Joined: Jun 17, 2010
Posts: 44
Nagaraj Shivaklara wrote:Hi,

I am trying to get the context object in java model class as follows, but it says getServletContext is not resolved? Any idea.. please help me.




First of all, your class VerifyCredentials is NOT an HttpServlet (I see it nowhere extending javax.servlet.http.HttpServlet).
Hence, you can't call getServletContext() method. If your class were to extend javax.servlet.http.HttpServlet, then the copile-time error would be gone.
If your logic doesn't allow to make VerifyCredentials a servlet, then you might want to make it a ServletContextListener.

Also, as suggested by fellow ranchers, please work on improving your design.


Nilesh Miskin
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
First of all, your class VerifyCredentials is NOT an HttpServlet (I see it nowhere extending javax.servlet.http.HttpServlet interface).
Hence, you can't call getServletContext() method. If your class were to implement javax.servlet.http.HttpServlet, then the copile-time error would be gone.


javax.servlet.http.HttpServlet is a class which implements javax.servlet.Servlet interface. You extend and not implement javax.servlet.http.HttpServlet
Nilesh Miskin
Ranch Hand

Joined: Jun 17, 2010
Posts: 44
Amol Nayak wrote:
First of all, your class VerifyCredentials is NOT an HttpServlet (I see it nowhere extending javax.servlet.http.HttpServlet interface).
Hence, you can't call getServletContext() method. If your class were to implement javax.servlet.http.HttpServlet, then the copile-time error would be gone.


javax.servlet.http.HttpServlet is a class which implements javax.servlet.Servlet interface. You extend and not implement javax.servlet.http.HttpServlet


My bad! Corrected the mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error while getting servlet context in Java code
 
Similar Threads
jdbctemplate not getting created in the DAO class
JSP using getServletContext Error
Has anybody successfully run the Virtual Trainer application in "JSF Complete Reference"?
Struts 2 and authentication
jsp standard action