Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to instantiate HttpRequest Object in class level

Reply
Thread Tools

How to instantiate HttpRequest Object in class level

 
 
pradeepsarathy@gmail.com
Guest
Posts: n/a
 
      03-10-2006
Hi all,
In the below code i am instantiating the request and session object at
the class level.But i am getting Null Pointer Exception while
instantiating.
Is it possible to instantiate a request object at class level.
I am doing so because,I want the request and session object to be
accessible in different methods of the same class.
Also all these methods(eg.method1()),will be called from SAX event
handler method(EndDocument() and character(char[] , int, int)),both of
which does not have provisions for passing the request obejct(or do
they?).

Please help me out.

Thanks in advance
-pradeep

import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Session

public class xmlPocCmd extends AbstractCmd{
String onlyNumericElement = "";
String onlyAlphabetElement = "";
public static final String ONLYNUMERIC = "OnlyNumeric";
public static final String ONLYALPHABET = "OnlyAlphabet";

String[] errorArray = new String[2];

HttpServletRequest request; // Returns Null Pointer Exception
HttpSession session = request.getSession(false);

public String execute(HttpServletRequest req, HttpServletResponse res)
throws CmdException {
start(req);
return "xmlMarshallContainerPoc.jsp";
}
public method1(){
//Will accesss the request the session objects to set values;
}

}

 
Reply With Quote
 
 
 
 
Rastislav Komara
Guest
Posts: n/a
 
      03-10-2006
Well ... U are not instantienting HttpServletRequest!

This:
HttpServletRequest request; // Returns Null Pointer Exception
HttpSession session = request.getSession(false);

Is compiled to:
HttpServletRequest request = null;
HttpSession session = request.getSession(false);
in default constructor. Refer to Java documentation for fields
initialization.

U can't instantient HttpServletRequest. It is only interface and it's
implmentation is provided by Web/Application container. If u need
instance of session to store some information simply provide session
object in inicialisation or use some proper pattern to solve your
problem.

 
Reply With Quote
 
 
 
 
Alex Hunsley
Guest
Posts: n/a
 
      03-10-2006
wrote:
> Hi all,
> In the below code i am instantiating the request and session object at
> the class level.But i am getting Null Pointer Exception while
> instantiating.


That's because you're not instantiating:

HttpServletRequest request; // Returns Null Pointer Exception

This line doesn't instantiate a HttpServletRequest instance - it merely
declares the type of a reference called 'request' that can be used to
refer to an HttpServletRequest instance (or a subclass instance). In
Java you use 'new' to make a new instance of an object:

Integer int = new Integer(7);

From your code it seems you don't have a very good understanding of
servlets. That can be fixed easily enough, but you have to go and do
some studying - I think that you need to work on your basic Java skills
before even thinking about things like Servlets. So forget servlets,
polish up your plain old Java knowledge first.
There's a tonne of free resources on the web for learning Java (e.g.
Sun's site) - go and reap the fruits!

alex
 
Reply With Quote
 
Alex Hunsley
Guest
Posts: n/a
 
      03-10-2006
Rastislav Komara wrote:
> Well ... U are not instantienting HttpServletRequest!
>
> This:
> HttpServletRequest request; // Returns Null Pointer Exception
> HttpSession session = request.getSession(false);
>
> Is compiled to:
> HttpServletRequest request = null;
> HttpSession session = request.getSession(false);


Speaking accurately: it's not, it's compiled into bytecode.
What you mean is that the JVM defaults to 'null' when an object
reference is declared without being assigned.

> in default constructor. Refer to Java documentation for fields
> initialization.
>
> U can't instantient HttpServletRequest. It is only interface and it's


Please, use real english! It's "you".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Instantiate a python class object in C Dids Python 2 03-14-2012 02:28 PM
Add HttpRequest class to windows application =?Utf-8?B?SHVzYW0=?= ASP .Net 2 09-25-2007 03:53 AM
Using HttpRequest Class =?Utf-8?B?SHVzYW0=?= ASP .Net 1 09-20-2007 09:29 AM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
Instantiate class within a class Colin Mc Mahon ASP General 2 08-13-2004 11:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57