Solved

Servlet filter isn't working correctly in Websphere 7

Posted on 2012-04-02
10
1,542 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
  • 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 26

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
 

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 26

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 26

Expert Comment

by:mrcoffee365
ID: 37830393
0
 
LVL 26

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 26

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 48
countPairs challenge 7 58
countAbc challenge 9 50
topping3 challenge 14 52
Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now