Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Passing Servlet Context to a non servlet object.

Reply
Thread Tools

Passing Servlet Context to a non servlet object.

 
 
Andrew Purser
Guest
Posts: n/a
 
      11-03-2004
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);
}
}
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      11-03-2004
Andrew Purser wrote:
> 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?

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      11-03-2004
Andrew Purser wrote:
> 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
 
Reply With Quote
 
Alex Kay
Guest
Posts: n/a
 
      11-04-2004
(Andrew Purser) wrote in message news:<. com>...

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

>
> 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);
> }
> }

 
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
how can I get the servlet access url from servlet context shaoanqing@gmail.com Java 4 04-20-2007 09:10 AM
Getting servlet context path without a servlet request. stuart.j.wood@gmail.com Java 3 06-06-2006 06:38 AM
Strange Context Error: Context 0x197ee0 is disconnected in VS 2005 =?Utf-8?B?U3VuU21pbGU=?= ASP .Net 0 01-10-2006 03:59 PM
Context.Items vs Context.Handler (passing values between pages) VS_NET_DEV ASP .Net 2 05-25-2004 01:16 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM



Advertisments