?
Solved

How to use JSP tags as servlet filter?

Posted on 2008-10-15
15
Medium Priority
?
862 Views
Last Modified: 2013-11-24
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>
0
Comment
Question by:Damn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 22722361
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}
0
 

Author Comment

by:Damn
ID: 22722824
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.
0
 
LVL 1

Expert Comment

by:peter-blackwood
ID: 22723150
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.
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 28

Expert Comment

by:rrz
ID: 22723154
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.
0
 

Author Comment

by:Damn
ID: 22723329
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.
0
 

Author Comment

by:Damn
ID: 22723358
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.
0
 
LVL 28

Expert Comment

by:rrz
ID: 22723538
>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}
0
 
LVL 1

Accepted Solution

by:
peter-blackwood earned 600 total points
ID: 22724317
Sorry, I've never done what you are proposing. You can try Jasper - Tomcat's JSP engine. (http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html)  Jasper knows how to do on-the-fly JSP compilation from files (detects changes) but I don't know how to configure it to do "in-proc" compilation, if it is possible.

But you could download the source and dig around, you might be able to leverage some of the compiler code.

HTH
0
 

Author Comment

by:Damn
ID: 22727662
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.
0
 
LVL 28

Expert Comment

by:rrz
ID: 22727833
>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.
0
 

Author Comment

by:Damn
ID: 22730167
>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...


0
 
LVL 28

Expert Comment

by:rrz
ID: 22731892
>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.
0
 

Author Comment

by:Damn
ID: 22748823
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...
0
 
LVL 28

Expert Comment

by:rrz
ID: 22750153
> 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());



0
 
LVL 28

Assisted Solution

by:rrz
rrz earned 600 total points
ID: 22750226
No, that won't work. You will need full URL not relative path.  Try something like  

<%
   String requestURL = request.getRequestURL().toString();
   String servletPath = request.getServletPath();
   String appURL = requestURL.substring(0, requestURL.indexOf(servletPath));
   URL url = new URL(appURL + "/mytag1.jsp ?body=" + java.net.URLEncoder.encode("your body","UTF-8"));
   HttpURLConnection urlConnection = (HttpURLConnection)(url.openConnection());
   urlConnection.connect();
   BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
   String line = null;
   while((line = reader.readLine()) != null)
   out.print(line + "<br/>");
%>

Open in new window

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question