Link to home
Start Free TrialLog in
Avatar of Damn
Damn

asked on

How to use JSP tags as servlet filter?

I have an application based on a single servlet, something of a front controller. I would like to add some jsp tag based post processing. How can I write a filter (post) and send output of my servlet to a jsp engine?
So, my url is http://localhost/MyServlet?action=play. After I have processed the servlet, my response will contain some jsp tags, for example:
<html>
<jsp:expression>new java.util.Date().toString()</jsp:expression>
</html>
Now I want these tags to be processed by some jsp tag engine invoked from filter and to get final result:
<html>
15.10.2008
</html>
Avatar of rrz
rrz
Flag of United States of America image

You could forward the request to your JSP with the tags.  Something like  
RequestDispatcher rd = getServletContext().getRequestDispatcher("/your.jsp?d=" + new java.util.Date().toString());  
rd.forward(request,response);
and in your.jsp   you could use  
${param.d}
Avatar of Damn
Damn

ASKER

The thing is, I don't have a .jsp file. My "your.jsp" is actually servlet output, that's why it contains jsp tag. I don't have static jsp files. My  servlet is generating html and now I would like it to hold some jsp tags as well.
So you are essentially building a code generator.  But unless you are planning to save the servlet response onto the file system as a JSP file for later use I don't see the value in using JSP technology.  The value of JSP is the embedding of dynamic content into static HTML.  JSP must be compiled by a servlet container into Java code before it can be used.

Maybe if you posted the exact problem you are trying to solve we could help find you a solution.
I think you will have to create a JSP. It doesn't have to be "static". It can be dynamic. It can respond differently to each set of request parameters sent to it.
Avatar of Damn

ASKER

peter:
We have our own html generator using our inhouse tags.It uses single servlet as front controller. It all works fine and dandy. We now want to add some post-processing that would include jsp tags.
I have posted the exact problem, take a look at the original question:
My servlet will produce jsp source. Now I need to process it with jsp processor before sending it back to the client. And yes, the source it has to be compiled, but I don't see why it can't be done on the fly (Let's say that at the moment I am not worried about performance).
In conclusion, I do not want to generate and  save jsp file to the file system and then redirect user to that jsp, I want to output all at once by original servlet.
Please let me know if this is clear, I hope I have managed to explain the problem.
Avatar of Damn

ASKER

rrz:
I thank you for your support. But do you know HOW I can accomplish that? I know that I can generate jsp page and save it to the disk and then redirect user to the page, but I need to eliminate the intermediate step and send the response with my servlet all in one go.
>But do you know HOW I can accomplish that?  
Please explain your more clearly. What can't you accomplish ?
The request parameter idea was just a simple example. A better way would be to create a javabean(request or session scoped) to hold your data that is generated from servlet. You can forward to the JSP which has code something like  
${yourBean.htmlBlockOne}
${yourBean.dateOne}
${yourBean.htmlBlockTwo}
${yourBean.dateTwo}
${yourBean.htmlBlockThree}
ASKER CERTIFIED SOLUTION
Avatar of peter-blackwood
peter-blackwood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Damn

ASKER

rrz@:
I want my servlet to return html to browser, not to send the redirect via URL to .jsp.
As I said:
So, my url is http://localhost/MyServlet?action=play. After I have processed the servlet, my response will contain some jsp tags, for example:
<html>
<jsp:expression>new java.util.Date().toString()</jsp:expression>
</html>
Now I want these tags to be processed by some jsp tag engine invoked from filter and to get final result:
<html>
15.10.2008
</html>

I do not want a servlet to respond with redirect.
>invoked from filter  
What do you mean ?  Are you talking about an object that implements the javax.servlet.Filter interface ?  
>Now I want these tags to be processed by some jsp tag engine
As far as I know, a request must be made to engage the JSP engine.
Avatar of Damn

ASKER

>What do you mean ?  Are you talking about an object that implements the javax.servlet.Filter interface ?  
Yes, of course. As I said, I have my own servlet doing front controller and I do not want to change this. I want to put jsp processing into the picture and filter seems the most logical way to do it.

>As far as I know, a request must be made to engage the JSP engine.
Not really. JSP engines are just java libraries. I would like to invoke JSP engine programatically.
Something like

org.apache.jasper.servlet.JspServlet servlet = new org.apache.jasper.servlet.JspServlet();
servlet.service(request, response);

That's where I need some help...


>I would like to invoke JSP engine programatically.
I don't know how to do that.  
I have a crazy idea.  Make a JSP file for each tag that you want to use. These JSP files will only contain one tag each and get a request parameter to use as its body. Name them mytag1.jsp , mytag2.jsp , and so on.   In your Filter's doFilter method parse the response from your servlet and find each tag. Use a java.net.HttpURLConnection object  to send a request with the tag body to the appropriate JSP file. Subsistute the response from the JSP for the tag in the servlet's response.    
Personally I think you should redesign and use JSP in the standard way.
Avatar of Damn

ASKER

rrz:
Could you provide small example for your crazy idea?
What do you mean by "and get a request parameter to use as its body?
If I understand correctly, this means that if I have let's say 30 tags in my page, I'll need to invoke 30 jsp pages? That doesn't sound that great...
> if I have let's say 30 tags in my page, I'll need to invoke 30 jsp pages?  
If they all different tags, then yes.  
>That doesn't sound that great...  
No, it doesn't.
>and get a request parameter to use as its body  
For example, mytag1.jsp  would contain  
<my:tag1>${param.body}</my:tag>
and you would call it with  
URL url = new URL("mytag1.jsp ?body=" + java.net.URLEncoder.encode("your body","UTF-8"));
HttpURLConnection urlConnection = (HttpURLConnection)(url.openConnection());



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial