Can pass Request Object :(

Discussion in 'Java' started by Tereska, Dec 10, 2006.

  1. Tereska

    Tereska Guest

    I have something like this:

    -----------file: index.jsp
    <% sp.start temp = new sp.start(request); %>

    ------------file: start.java (in package sp)
    package sp;

    import javax.servlet.http.HttpServletRequest;

    public class start {
    public HttpServletRequest r = null;

    public void start(HttpServletRequest r) {
    this.r=r;
    }
    }

    When I run this, there is an exeption :/ Why I can't pass request
    object to another java class?

    Please help!
     
    Tereska, Dec 10, 2006
    #1
    1. Advertising

  2. Tereska

    Tereska Guest

    When I make empty constructor all is OS,but when I add "response" I
    have something like this:

    org.apache.jasper.JasperException: Unable to compile class for JSP

    An error occurred at line: 1 in the jsp file: /index.jsp
    Generated servlet error:
    G:\Documents and
    Settings\Tereska\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FTestNew\org\apache\jsp\index_jsp.java:42:
    cannot find symbol
    symbol : constructor start(javax.servlet.http.HttpServletRequest)
    location: class sp.start
    sp.start me = new sp.start(request);
    ^
    1 error


    I wondering what is wrong. I have that kind of constructor....
     
    Tereska, Dec 10, 2006
    #2
    1. Advertising

  3. Tereska wrote:
    > I have something like this:
    >
    > -----------file: index.jsp
    > <% sp.start temp = new sp.start(request); %>
    >
    > ------------file: start.java (in package sp)
    > package sp;
    >
    > import javax.servlet.http.HttpServletRequest;
    >
    > public class start {
    > public HttpServletRequest r = null;
    >
    > public void start(HttpServletRequest r) {
    > this.r=r;
    > }
    > }
    >
    > When I run this, there is an exeption :/ Why I can't pass request
    > object to another java class?


    Do you want to give us a hint ?

    (like the exception text)

    Arne
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Dec 10, 2006
    #3
  4. Tereska

    Lew Guest

    Tereska wrote:
    > When I make empty constructor all is OS,but when I add "response" I
    > have something like this:
    >
    > org.apache.jasper.JasperException: Unable to compile class for JSP
    >
    > An error occurred at line: 1 in the jsp file: /index.jsp
    > Generated servlet error:
    > G:\Documents and
    > Settings\Tereska\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FTestNew\org\apache\jsp\index_jsp.java:42:
    > cannot find symbol
    > symbol : constructor start(javax.servlet.http.HttpServletRequest)
    > location: class sp.start
    > sp.start me = new sp.start(request);
    > ^
    > 1 error
    >
    >
    > I wondering what is wrong. I have that kind of constructor....


    No, actually, you don't. You defined a method "start()" with void return type.
    Constructors do not have return types.

    > package sp;
    >
    > import javax.servlet.http.HttpServletRequest;
    >
    > public class start {


    You chould name your classes with an initial upper-case letter:

    > public HttpServletRequest r = null; // no need to specify null here


    Better that the instance variable be /private/, not /public/, and don't be
    afraid of longer names. /r/ is a bit too terse.

    >
    > public void start(HttpServletRequest r) { // CONSTRUCTOR CANNOT RETURN VOID


    The use of the keyword /void/ makes this not a constructor. That's why you got
    the error.

    > this.r=r;
    > }
    > }


    Try this:

    <code>
    package sp;
    import javax.servlet.http.HttpServletRequest;

    public class Start
    {
    private final HttpServletRequest request;

    public Start( HttpServletRequest req ) // no return type
    {
    if ( req == null )
    {
    throw new NullPointerException( "null request" );
    }
    this.request = req;
    }


    public final HttpServletRequest getRequest()
    {
    return request;
    }
    }
    </code>

    - Lew
     
    Lew, Dec 10, 2006
    #4
  5. On Dec 10, 6:16 am, Lew <> wrote:
    > Tereska wrote:
    > > When I make empty constructor all is OS,but when I add "response" I
    > > have something like this:

    >
    > > org.apache.jasper.JasperException: Unable to compile class for JSP

    >
    > > An error occurred at line: 1 in the jsp file: /index.jsp
    > > Generated servlet error:
    > > G:\Documents and
    > > Settings\Tereska\.netbeans\5.5\apache-tomcat-5.5.17_base\work\Catalina\localhost\FTestNew\org\apache\jsp\index_jsp.java:42:
    > > cannot find symbol
    > > symbol : constructor start(javax.servlet.http.HttpServletRequest)
    > > location: class sp.start
    > > sp.start me = new sp.start(request);
    > > ^
    > > 1 error

    >
    > > I wondering what is wrong. I have that kind of constructor....No, actually, you don't. You defined a method "start()" with void return type.

    > Constructors do not have return types.
    >
    > > package sp;

    >
    > > import javax.servlet.http.HttpServletRequest;

    >
    > > public class start {You chould name your classes with an initial upper-case letter:

    >
    > > public HttpServletRequest r = null; // no need to specify null hereBetter that the instance variable be /private/, not /public/, and don't be

    > afraid of longer names. /r/ is a bit too terse.
    >
    >
    >
    > > public void start(HttpServletRequest r) { // CONSTRUCTOR CANNOT RETURN VOIDThe use of the keyword /void/ makes this not a constructor. That's why you got

    > the error.
    >
    > > this.r=r;
    > > }
    > > }Try this:

    >
    > <code>
    > package sp;
    > import javax.servlet.http.HttpServletRequest;
    >
    > public class Start
    > {
    > private final HttpServletRequest request;
    >
    > public Start( HttpServletRequest req ) // no return type
    > {
    > if ( req == null )
    > {
    > throw new NullPointerException( "null request" );
    > }
    > this.request = req;
    > }
    >
    > public final HttpServletRequest getRequest()
    > {
    > return request;
    > }}</code>
    >
    > - Lew


    Hi, thought you should know - its not very useful to throw your own
    NullPointerException like that. You'd be best off just using the ref
    and letting a normal one happen.

    Unless of course you are really trying to validate the arguments that
    were passed. In that case its better to Throw an
    'IllegalArgumentException or IllegalStateException.

    Andrew
     
    andrewmcdonagh, Dec 10, 2006
    #5
  6. Tereska

    Tereska Guest

    Thank You very much guys!!! Now All is OK!

    Best Regards!
     
    Tereska, Dec 10, 2006
    #6
  7. Tereska

    Lew Guest

    andrewmcdonagh wrote:
    > Hi, thought you should know - its not very useful to throw your own
    > NullPointerException like that. You'd be best off just using the ref
    > and letting a normal one happen.
    >
    > Unless of course you are really trying to validate the arguments that
    > were passed. In that case its better to Throw an
    > 'IllegalArgumentException or IllegalStateException.


    Au contraire, it is very useful. Throwing the exception in the constructor,
    which otherwise would not, catches the error at the point it's introduced.
    Waiting until the request is dereferenced to throw the NPE is way too late.
    The earlier you catch a bug, the easier it is to understand and cure.

    Besides, non-null element is an invariant of the class, as I showed it. As
    such it should be established in the constructor. (The get method could thus
    use "assert request != null;") Waiting for the ref to throw the NPE is
    formally incorrect as well as harder to debug.

    The decision of NPE or IllegalArgumentException has been discussed by wiser
    heads than mine, who seem to agree that it is a matter of preference. I agree
    that IllegalArgumentException would be appropriate here, but many are used to NPE.

    - Lew
     
    Lew, Dec 11, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Birtle
    Replies:
    2
    Views:
    2,048
    John Saunders
    Oct 16, 2003
  2. Girish bharadwaj
    Replies:
    0
    Views:
    694
    Girish bharadwaj
    Sep 15, 2004
  3. Kai Grossjohann
    Replies:
    2
    Views:
    805
    Anton Spaans
    Dec 8, 2003
  4. Tim Arnold

    pass object or use self.object?

    Tim Arnold, Apr 6, 2010, in forum: Python
    Replies:
    10
    Views:
    403
    Bruno Desthuilliers
    Apr 9, 2010
  5. Gerhard Pretorius

    Cannot pass Request object to VB COM DLL (cross post)

    Gerhard Pretorius, Aug 13, 2003, in forum: ASP General
    Replies:
    4
    Views:
    298
Loading...

Share This Page