Image

Imagekhalidz0r wrote in Imagejava_dev

JSP and Heredoc

I am having a small problem with String handling in JSP.

I have some jsp code that would produce some sort of a report. I need to put this report in the session due to the way the rest of the system is designed.

However, this bunch of code is really long, and it is in a format more like this (This is not actual code, it is simply an example. I do not have the actual code here since I'm not currently at work):

<table>
<tr>
<td>
Report title: <%=report.getTitle()%>
</td>
<td>
Report ID: <%=report.getID()%>
</td>
</tr>
</table>

I know I could have the rest of the system work in a different way, and then I would not even need to put this in the session, but that's not the issue here. There is a huge part of the system working that way, and I cannot change it now. I just need to pass that long resulting string to the setAttribute function of the HttpSession object.

So in fact, what I want to do is:

session.setAttribute("whatever","<table><tr><td>Report title: "+report.getTitle()+"</td><td>Report ID: "+report.getID()+"</td></tr></table>");

But as you can see, if I am going to do the whole long page that way it would look ugly, be unreadable, and the conversion would be tidius. Is there a way to pass this information to the function without having to change the way it looks. For instance, can I do something so that I could use code like: (I am not familiar with the XML-like JSP bits. I never needed them in my code; probably should learn more about them in the future)

<session:setAttribute name="whatever">
<value>
<table>
<tr>
<td>
Report title: <%=report.getTitle()%>
</td>
<td>
Report ID: <%=report.getID()%>
</td>
</tr>
</table>
</value>
</session:setAttribute>

In other words, I need something like the Heredoc function available in Perl and PHP, where they can do something like:

$str = <<<STR
Here goes the multi-line string, it can have embeded variables too.
STR;


So anybody has some idea about how to go about doing this?

Thanks for your time!