Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way to use JSP in place of JDOM or StringBuffers  RSS feed

 
J Jarrett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking something like...

String xml = executeJsp.url("dynamicXml.jsp", dataObject);

I made up the syntax but is there a real equivalent?

I hope so. Thanks in advance.


EDIT: added dataObject because the page wouldn't be dynamic without data
[ February 24, 2006: Message edited by: J Jarrett ]
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you expect that hypothetical method to do?

I don't mean to be rude, but the title of your post sounds rather like "Is there a way to use a paintbrush in place of a bicycle or a strudel?" But obviously I'm not understanding your objective.
 
J Jarrett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I would like is to be able to get the output from the JSP and add it directly to a variable inside my servlet. Using JDOM and StringBuffers to produce dynamic xml adds a lot of complication when all I want to do add some dynamic values to an xml before converting it to a pdf. Maintaining JDOM is a lot harder than maintaining a JSP page...at least for me.

Sorry my original question wasn't very clear. Hopefully it is a little clearer now. I suspect I want a shortcut where one doesn't exist but I thought I would at least ask.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say it's a bit of a Rube Goldberg contrivance, but you can use the URL.openConnection() method to establish a connection to a JSP hosted somewhere.
 
Bruno Boehr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Jarrett,

You have a few options here, but they are all kind of cumbersome.

- as Bear Bibeault suggested, you can access your JSP by its URL; the downside here is that you will be accessing the page as a completely foreign resource, so you won't be able to pass your data as a Java object, but only as request parameters;

- much of the complexity of what you are trying to achieve arises from the fact that a servlet's output is firmly tied to the socket connection. There is a way though to outsmart the container: wrap the response object into your own class that implements HttpServletResponse by simply delegating every method call to the underlying original response object - except one method, getWriter(). To make this job easier there is a class provided on the Servlet API, HttpServletResponseWrapper that does exactly that. All you need to do is override the getWriter() method (and provide a way to get the resulting string back into the invoking servlet):

public class JspResponseWrapper extends HttpServletResponseWrapper {
private StringWriter stringWriter;
private PrintWriter printWriter;

public JspResponseWrapper(HttpServletResponse response) {
super(response);
stringWriter = new StringWriter();
printWriter = new PrintWriter(stringWriter);
}

public PrintWriter getWriter() {
return printWriter;
}

public String getText() {
return stringWriter.getBuffer().toString();
}
}

Then in the servlet's doGet() method you go like:

request.setAttribute("data-object", dataObject);
JspResponseWrapper responseWrapper = new JspResponseWrapper(response);
getServletContext().getRequestDispatcher("dynamicXml.jsp").include(request, responseWrapper);
String yourXml = responseWrapper.getText();

- but you will be infinitely better off [whispering very quietly] if you stop using JSP and switch to template-based rendering that uses a template engine like Velocity (http://jakarta.apache.org/velocity/). With Velocity, for example, solving your problem would take literally just one line of code, but there are lots of other, more important benefits that are mainly of the architectural nature. If you are curious as to what I am talking about, you can check out the link in my signature.
 
J Jarrett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bruno. It is interesting that I ended up at the Velocity site on my own looking for a solution. Your posting confirms that I'm heading down the right path.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!