Can pass Request Object :(

T

Tereska

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!
 
T

Tereska

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....
 
G

Guest

Tereska said:
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
 
L

Lew

Tereska said:
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
 
A

andrewmcdonagh

Constructors do not have return types.




afraid of longer names. /r/ is a bit too terse.



the error.


<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
 
L

Lew

andrewmcdonagh said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top