Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error while getting servlet context in Java code

 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


}



}





 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11882
196
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps this would be best asked in the Servlets forum? Moving there now.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic