Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic