Thanks n Regards,
Nagaraj S K
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.
}
}
Thanks n Regards,
Nagaraj S K
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
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?
Thanks n Regards,
Nagaraj S K
Nagaraj Shivaklara wrote: Is this possible to get context parameters in classes apart from servlets?
SCJP 6 || SCWCD 5
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.
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.
Nilesh Miskin
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.
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![]()
Nilesh Miskin
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |