Solved

Servlet filter isn't working correctly in Websphere 7

Posted on 2012-04-02
10
1,637 Views
Last Modified: 2012-04-13
Hi,
     
       I have a java servlet filter intercepting the JSP (html) response and converting in to PDF. The code works when deployed in WAS 5.1 but not when deployed to WAS 7 .
 
The following is my filter code, http servlet response wrapper and parts of web.xml.

//ShoppingFilter

GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) resp);
chain.doFilter(req, wrapper);

The issue  in WAS 7 is wrapper.getData() is empty
-----------------------------------------------------------------------------------------------------------------
// Wrapper class
public class GenericResponseWrapper extends HttpServletResponseWrapper {
      private ByteArrayOutputStream output;
      private int contentLength;
      private String contentType;

      public GenericResponseWrapper(HttpServletResponse response) {
            super(response);
            output = new ByteArrayOutputStream();
      }

      public byte[] getData() {
            return output.toByteArray();
      }

      public ServletOutputStream getOutputStream() {
            return new FilterServletOutputStream(output);
      }

      public PrintWriter getWriter() throws IOException {
            return new PrintWriter(getOutputStream(), true);
      }

      public void setContentLength(int length) {
            this.contentLength = length;
            super.setContentLength(length);
      }

      public int getContentLength() {
            return contentLength;
      }

      public void setContentType(String type) {
            this.contentType = type;
            super.setContentType(type);
      }

      public String getContentType() {
            return contentType;
      }
}
-----------------------------------------------------------------------------------------------------------------------
web.xml
    <filter>
          <filter-name>ShoppingFilter</filter-name>
          <display-name>ShoppingFilter</display-name>
          <filter-class>com.xx.yy.ShoppingFilter</filter-class>
    </filter>
      <filter-mapping>
            <filter-name>ShoppingFilter</filter-name>
            <url-pattern>/ggc/shoppingFilter.do</url-pattern>
      </filter-mapping>

Any suggestions on how to make this work in WAS 7 please?
Thanks.
0
Comment
Question by:ram7098
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 37806823
whether you have some other filter's in your web.xml ??
Works only if you have the <filter-mapping> tag appear after the<filter> tag.
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 37806860
deployment descriptor(web.xml) work different for every server
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37807301
Your definitions for the filter look okay.  However, I found this answer on EE about what Websphere does with the order of tags in web.xml -- forcing it to use the web.xml as you have defined it might be the solution:
http://www.experts-exchange.com/Software/Server_Software/Application_Servers/Java/IBM_Websphere/Q_26625087.html
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

Author Comment

by:ram7098
ID: 37808622
Thanks for the replies. The issue isn't servlet filter isn't being invoked but chain.doFilter isn't copying the html response to the wrapper response object.

GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) resp);
chain.doFilter(req, wrapper);

I think WAS 5.1 is using Server 2.3 whereas WAS 7 is probably using Servlet 2.5. We are doing an upgrade with existing code and our web.xml is still using 2.3 dtd. We are using struts version 1.3.  I read for servlet 2.5 the filters have a property called dispatcher which can be configured. If not configured, then default I guess is REQUEST. I am not sure, if that is the reason why it isn't working? I can try creating a different version of web.xml, just for testing on the server......
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">

Any thoughts please?

Thanks.
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 37808807
I think using 2.3 for the servlet version should be fine.  I've read of people using 2.4 with WAS 7, so 2.3 should be okay, too.

In addition to an id, you might try the metadata tag, like:
<web-app metadata-complete="true" id="mywebapp">

I'm not sure id="WebApp" will be distinctive enough -- but maybe it's okay.  The metadata-complete is actually a way of avoiding startup scanning costs, but the person in the other EE question did it, so you might try it.  It's supposed to be related to servlet spec 3.0, so it really shouldn't matter one way or another, but shouldn't hurt.

Another thing to try is mentioned in this post:
http://greatwebguy.com/programming/java/urlrewritefilter-servlet-filter-problem-in-websphere-6105-and-greater/

Apparently if a url is not directly mapped, IBM changed WAS in 6 to not send the url through your filters.  There's a workaround.  Weird choice on IBM's part.
0
 

Author Comment

by:ram7098
ID: 37828676
I tried changing the web.xml and adding custom IBM properties to the web container but the result has been the same. Is there any work around please?
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37830393
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37834360
Great -- does that mean that was the problem you had?  IBM changing how it sends urls through filters sounds like a subtle change, but you're not the only person caught by it.
0
 

Author Comment

by:ram7098
ID: 37844063
The real issue was in the wrapper implementation. The above mentioned response wrapper doesn't work in WAS 7 / Java 6 (with Servlet 2.5 API). Instead of wracking my brain too much, I modified the wrapper (did a code work around) to get it working. Your mentioned web.xml changes where really helpful. Thank you.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37845537
Good, glad to help even if indirectly.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.

717 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