Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to get the response of a servlet into a string?  RSS feed

 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys... I have to work with a very old project written with java servlets... with wich i am not exactly an expert... I'm facing a problem that I have no idea about how to solve it...

The thing is that I have a jsp wich is a "pattern", whose code is something like this (but much more complicated, of course):





To fill up the pattern, I have a servlet which sets the session attributes "header", "left", "right" and "footer", which are supplied by an external web service, and finally performs a forward to the pattern. So, something like that:



But... what about the "content"? Welll... the PROBLEM is that the "content" should be the output of any other servlet in the project... any other server, with its own forward to any other jsp in the project... something for example like this:



This login servlet, goes to Wellcome.jsp if everything is ok, and to Error.jsp if there was an error. What I need is that this output were encapsulated as "content" in the pattern jsp, using PatternServlet. Probable the url calling would be something as:

http://localhost:9080/MyProject/Pattern.servlet?page=Login.servlet


but, is that possible? how could i do it? every thing i try, performs a "response already commited error"



 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it with a filter if you wrap the response object.
The Servlet API as an HttpServletResponseWrapper that you can override to do this (which will save you a bunch of typing).

I have an example app that does this to capture the output of the default jsp page in the ROOT app that ships with Tomcat.
http://simple.souther.us/capture.war

It takes the output and dumps it to stdout.
That should be enough to get you pointed in the right direction.
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Ben. I'm sure it will help a lot. I'll tell you when I solve it
 
Devaka Cooray
Marshal
Posts: 5564
710
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that you should use something like EL (Expression Language) instead of using these Scriptlets (java codes) in JSPs.
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:Also, note that you should use something like EL (Expression Language) instead of using these Scriptlets (java codes) in JSPs.


I'm afraid I don't understand... do you mean changing the pattern.jsp instead of writing the PatternServlet.java? how?

Well... I shouldn't change the pattern. jsp... it's an imposition...

Thank you very much anyway... regards
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Ben, could you help me a little bit more, please? I am quite confused...

How do I invoke the doFilter method? and where? and how do I build the FilterChain to it??

I'm so sorry for being such an inept

Really thanks again...

 
Devaka Cooray
Marshal
Posts: 5564
710
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pepa, what I meant is you should replace the <%= %> tags with something like EL, standard tags, or JSTL tags. For an example,
<%= session.getAttribute("header") %> could be written as:
${sessionScope.header} in EL.

About the filter, doFilter method gets invoked when a given request/response passes through it. In other words, YOU should not invoke that method. Same way, FilterChain object is generated by the servlet container when accessing this method.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pepa Jul wrote:Hey, Ben, could you help me a little bit more, please? I am quite confused...

How do I invoke the doFilter method? and where? and how do I build the FilterChain to it??

I'm so sorry for being such an inept
Really thanks again...


The code to the filter and the httpServletWrapper are in the war file under WEB-INF/classes/test

There isn't much there.
The filter chain is built in the deployment descriptor (WEB-INF/web.xml).

What this code does is wrap httpServletResponse with an object that in effect overrides the toString method with a method that returns the response output stream as a string.

 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:Pepa, what I meant is you should replace the <%= %> tags with something like EL, standard tags, or JSTL tags. For an example,
<%= session.getAttribute("header") %> could be written as:
${sessionScope.header} in EL.


Oh, ok... I should be slepping.... I'll try But, by the way, I have no problem whith header, footer, left and right...


Devaka Cooray wrote:About the filter, doFilter method gets invoked when a given request/response passes through it. In other words, YOU should not invoke that method. Same way, FilterChain object is generated by the servlet container when accessing this method.


I'm afraid it is too much esoteric for me... But that helps me to start... thanks!



 
Devaka Cooray
Marshal
Posts: 5564
710
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pepa Jul wrote:But, by the way, I have no problem whith header, footer, left and right...

Eliminating scriptlets is for the best practice.

Pepa Jul wrote:I'm afraid it is too much esoteric for me...

Using filters is not that hard.
As Ben said, what you have to do is create your filter classes and just specify them in web.xml
The container is responsible for the runtime work.
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, ok, what I hadn't got was the web.xml thing... I think I'll be able to do it now. I'll tell you.

Thank you very much you both again
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:Eliminating scriptlets is for the best practice.

Oh, sure! Thanks for the tip
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again I just come back to say you, again, THANKS!

Ben, your tip worked like a charm

All I had to do was re-write the Filter.java something like this




And then, in web.xml, config the filter and a filter.mapping for each servlet I want to pattern. I could even delete the Pattern.servlet, in fact.



And Devaka, your help was useful too... but the EL doesen't work... do you hava an idea about that?

Regards
 
Devaka Cooray
Marshal
Posts: 5564
710
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pepa Jul wrote:... but the EL doesen't work... do you hava an idea about that?

ItDoesntWorkIsUseless
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:
Pepa Jul wrote:... but the EL doesen't work... do you hava an idea about that?

ItDoesntWorkIsUseless


Ok, ok... you know I was not so afraid about that... that's why I "forgot" to explain more... (this and also the fact that I am not used to use forums... )

It just print the same string "${sessionScope.whatever}" in the generated htm. I mean, I doesn't seem to interpret it...

Maybe has something to do with this (which I can't change, by the way):

 
Devaka Cooray
Marshal
Posts: 5564
710
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might have not properly specified the Servlets 2.4 XML Schema in your web.xml or you might using an older version of the container.
If thats the case, adding this code on top of your JSP would solve your problem:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:If thats the case, adding this code on top of your JSP would solve your problem:

I would highly discourage that. Fix the real problem in your deployment descriptor rather than applying a band-aid to each and every JSP page.

Proper setup is covered in the JSP FAQ.
 
Devaka Cooray
Marshal
Posts: 5564
710
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Fix the real problem in your deployment descriptor rather than applying a band-aid to each and every JSP page.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pepa Jul wrote:Hello again I just come back to say you, again, THANKS!

Ben, your tip worked like a charm ...



Glad it's working.
One thing to keep in mind is that the wrapper always treats the output as text so you want to be careful not to accidentally apply it requests for binary data (images, PDFs, etc...).

 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... just one remaining issue...

I got all the servlets I wanted filtered by




But I need also to "patternize" some jsp which are directly called from their URL (by clicking some link). I tried

A)


B)


C)



D)



...nothing works... no errors... just it doesn't "patternize", it just shows the page normally... any ideas about??


Thank you again
 
Pepa Jul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ups! Sorry! It works as D) ... I had forgot the first "/"...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!