The Artima Developer Community
Sponsored Link

Java Answers Forum
Passing objects to servlet

1 reply on 1 page. Most recent reply: Aug 6, 2004 5:08 PM by Mark Holmes

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
engin

Posts: 1
Nickname: engin
Registered: Aug, 2004

Passing objects to servlet Posted: Aug 6, 2004 12:59 AM
Reply to this message Reply
Advertisement
Hi

I have a servlet in my program, which receives messages from clients. The servlet instantiates a parser, which reads the messages and invokes other objects. The problem is that, the "other objects" I mentioned have already been instantiated, and I have to pass them as parameters to servlet, but I can't. Because the container handles the servlet instantiation.

To visualize it:

public class ClientListenerServlet extends HttpServlet {
//..
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
//..
Parser p = new Parser();
p.parse(message);
//..etc
}
//..
}

public class Parser() {
public void parse(String s) {
otherObject.doSomething(); // ==> I can't pass otherObject as parameter to Parser and Servlet
}
}

any suggestions?


Mark Holmes

Posts: 1
Nickname: rampage
Registered: Aug, 2004

Re: Passing objects to servlet Posted: Aug 6, 2004 5:08 PM
Reply to this message Reply
Hey,

One idea is to use the ServletContext (sort of 'global variable' storage for your servlet....anyway).

You can set attributes in this, just like with the request, and access them anytime in the servlet. This way you can make the "other objects" parmeters to the Parser (or inject them via setters), that you get out of the context along with grabbing the message:

example:

public void doPost(HttpServletRequest req,
HttpServletResponse resp)
{
// for thoroughness ;) ..
HttpSession session = req.getSession();
ServletContext context = session.getServletContext();
OtherObject other =
(OtherObject) context.getAttribute("other");

// note no binding of parser to context

Parser p = new Parser();
p.setOtherObject(other);
p.parse(message);
//..etc
}

... and Parser ...

public class Parser
{
this.other;
// ...

public void setOtherObject(OtherObject other)
{
if (null != other) {
this.other = other;
}
}

// ... and ...

public void parse(String message)
{
// ...
this.otherObject.doSomething()
// ...
}
}

Just one idea.... hope it helps

Flat View: This topic has 1 reply on 1 page
Topic: Runtime.exec() .. another question. Previous Topic   Next Topic Topic: What is difference between an Application Server and Web Server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use