Trouble implementing an interface, strange compiler error

  • Thread starter Robert Maas, see http://tinyurl.com/uh3t
  • Start date
R

Robert Maas, see http://tinyurl.com/uh3t

I want to set up a test rig for HttpServlet things.
The idea is that my test rig creates (from scratch,
not underneath an actual HTTP/Tomcat/J2EE session)
both HttpServletRequest and HttpServletResponse
objects, populating them with realistic data,
then calls the HttpServlet with those as args,
then upon return checks the HttpServletResponse
object to see if the HttpServlet worked correctly.

Unfortunately I haven't been able to find any
public constructor for any class that implements
HttpServletRequest (I haven't even started looking
at HttpServletResponse because I'm already stumped
here), except one which takes another already-constructed
request object as parameter to constructor thereby
begging the question how to construct one from scratch
in the first place.

So I decided to write my own dummy request class which
would implement the interface hence be acceptable as
parameter to a servlet, starting with this skeleton:

public class DummyHttpServletRequest
implements javax.servlet.http.HttpServletRequest {
}

I tried to compile that, expecting to get a compiler
error that I'm missing one or more of the methods
needed to implement the interface. Instead I got this:

DummyHttpServletRequest.java:2: cannot resolve symbol
symbol : class HttpServletRequest
location: package http
implements javax.servlet.http.HttpServletRequest {
^
1 error

What's wrong, and how can I fix it so I can move ahead with this project??
.
 
B

ByteCoder

(e-mail address removed) (Robert Maas, see http://tinyurl.com/uh3t) wrote in
I want to set up a test rig for HttpServlet things.
The idea is that my test rig creates (from scratch,
not underneath an actual HTTP/Tomcat/J2EE session)
both HttpServletRequest and HttpServletResponse
objects, populating them with realistic data,
then calls the HttpServlet with those as args,
then upon return checks the HttpServletResponse
object to see if the HttpServlet worked correctly.

Unfortunately I haven't been able to find any
public constructor for any class that implements
HttpServletRequest (I haven't even started looking
at HttpServletResponse because I'm already stumped
here), except one which takes another already-constructed
request object as parameter to constructor thereby
begging the question how to construct one from scratch
in the first place.

So I decided to write my own dummy request class which
would implement the interface hence be acceptable as
parameter to a servlet, starting with this skeleton:

public class DummyHttpServletRequest
implements javax.servlet.http.HttpServletRequest {
}

I tried to compile that, expecting to get a compiler
error that I'm missing one or more of the methods
needed to implement the interface. Instead I got this:

DummyHttpServletRequest.java:2: cannot resolve symbol
symbol : class HttpServletRequest
location: package http
implements javax.servlet.http.HttpServletRequest {
^
1 error

What's wrong, and how can I fix it so I can move ahead with this
project?? .

This class:
javax.servlet.http.HttpServletRequestWrapper

"Provides a convenient implementation of the HttpServletRequest
interface that can be subclassed by developers wishing to adapt the
request to a Servlet. This class implements the Wrapper or Decorator
pattern. Methods default to calling through to the wrapped request
object."

I think you are being too hard on yourself, maybe you should try an
'easy' way out and extend the HttpServletRequestWrapper.
 
S

shakah

Robert said:
I want to set up a test rig for HttpServlet things.
The idea is that my test rig creates (from scratch,
not underneath an actual HTTP/Tomcat/J2EE session)
both HttpServletRequest and HttpServletResponse
objects, populating them with realistic data,
then calls the HttpServlet with those as args,
then upon return checks the HttpServletResponse
object to see if the HttpServlet worked correctly.

Unfortunately I haven't been able to find any
public constructor for any class that implements
HttpServletRequest (I haven't even started looking
at HttpServletResponse because I'm already stumped
here), except one which takes another already-constructed
request object as parameter to constructor thereby
begging the question how to construct one from scratch
in the first place.

So I decided to write my own dummy request class which
would implement the interface hence be acceptable as
parameter to a servlet, starting with this skeleton:

public class DummyHttpServletRequest
implements javax.servlet.http.HttpServletRequest {
}

I tried to compile that, expecting to get a compiler
error that I'm missing one or more of the methods
needed to implement the interface. Instead I got this:

DummyHttpServletRequest.java:2: cannot resolve symbol
symbol : class HttpServletRequest
location: package http
implements javax.servlet.http.HttpServletRequest {
^
1 error

What's wrong, and how can I fix it so I can move ahead with this
project??

Is that class in your compilation classpath? After a quick look in
Tomcat's directories, it appears to exist in
../common/lib/servlet-api.jar.
 
R

Ross Bamford

I want to set up a test rig for HttpServlet things.
The idea is that my test rig creates (from scratch,
not underneath an actual HTTP/Tomcat/J2EE session)
both HttpServletRequest and HttpServletResponse
objects, populating them with realistic data,
then calls the HttpServlet with those as args,
then upon return checks the HttpServletResponse
object to see if the HttpServlet worked correctly.

You cannot construct one because it is implementation-specific - the
Servlet Container provides an implementation that suits it's internal
layout (usually as a facade to many other objects).

Instead try:

1) http://www.mockobjects.com/FrontPage.html
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top