Passing Servlet Context to a non servlet object.

A

Andrew Purser

As you can tell by my subject line I am fairly new to java and
servlets.

I was hoping I could get some feed back as to the merits / problems /
suggestions of other ways to do it for the code skeleton below.

Basically I have created a Something java class that I want to call
from a servlet. The catch is I want the Something java class to
include Jsps depending on various things. The only way I can see to do
this if for my servlet to pass the ServletContext, HttpRequest and
HttpResponse to the class. I get the feeling this may be a bad thing
to do. Can someone enlighten me?

Cheers,

Andrew.



Code outline :

public class Something {

public void buildPage(ServletContext context,
HttpServletRequest req,
HttpServletResponse res) {
// do various bits and pieces to work out what page
// to include.

context.getRequestDispatcher(jspToInlucde).include(req,res);

}

}

public class MyServlet extends HttpServlet {

doPost() {
Something.buildPage(context,request,response);
}
}
 
S

Sudsy

Andrew said:
As you can tell by my subject line I am fairly new to java and
servlets.

I was hoping I could get some feed back as to the merits / problems /
suggestions of other ways to do it for the code skeleton below.

Basically I have created a Something java class that I want to call
from a servlet. The catch is I want the Something java class to
include Jsps depending on various things. The only way I can see to do
this if for my servlet to pass the ServletContext, HttpRequest and
HttpResponse to the class. I get the feeling this may be a bad thing
to do. Can someone enlighten me?
<snip>

If class Something depends on servlet container facilities, why are you
trying to break it out into a separate class? Sounds like the function-
ality belongs in the servlet. Your architecture is confusing; care to
elaborate?
 
C

Chris Smith

Andrew said:
Basically I have created a Something java class that I want to call
from a servlet. The catch is I want the Something java class to
include Jsps depending on various things. The only way I can see to do
this if for my servlet to pass the ServletContext, HttpRequest and
HttpResponse to the class. I get the feeling this may be a bad thing
to do. Can someone enlighten me?

There's nothing inherently wrong with passing the ServletContext and the
ServletRequest and ServletResponse outside of the servlet class. There
are some concerns in some cases, though, specifically with code re-use.
If your goal is to include JSPs, then it seems you probably don't intend
to use this outside of a servlet environment, so you're probably okay.
If you needed something more reusable, you'd need to consider other
options.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Alex Kay

As you can tell by my subject line I am fairly new to java and
servlets.

I was hoping I could get some feed back as to the merits / problems /
suggestions of other ways to do it for the code skeleton below.

Basically I have created a Something java class that I want to call
from a servlet.

It is common to call other classes from servlets. That is one of the
most powerful things in servlets, you have full access to everything in
Java.
The catch is I want the Something java class to
include Jsps depending on various things. The only way I can see to do
this if for my servlet to pass the ServletContext, HttpRequest and
HttpResponse to the class. I get the feeling this may be a bad thing
to do. Can someone enlighten me?

Instead of passing the ServletContext you can just get it via 'request':-
RequestDispatcher rd = request.getRequestDispatcher ("blah.jsp");
rd.include(...);

Servlets/YourClasses/JSPs/Filters/Taglibs can inter-operate quite well
but under some conditions you can end up making a recursive call.
The thing you include is included again and again. Cute to see
but not useful ;-)

A more common approach is to *set* something in a scope variable
from say your servlet (or classes it calls). Like this:-
synchronized (session) { session.setAttribute("blah", myBlahObject); }

Then from some other place you would simply getAttribute("blah")
and do with it as you wish.

Cheers.
Alex Kay.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top