HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Core J2EE Patterns"
Vol. 7, Issue 1, p. 32

	


Listing 1 
 
 public class StandardEncodeFilter
extends BaseEncodeFilter {
    // Creates new StandardEncodeFilter
    public StandardEncodeFilter() {}
    
    public void doFilter(javax.servlet.ServletRequest
                         servletRequest,javax.servlet.ServletResponse
                         servletResponse,javax.servlet.FilterChain
                         filterChain)
    throws java.io.IOException, javax.servlet.ServletException {
        String contentType =
        servletRequest.getContentType();
        if ((contentType == null) ||
            contentType.equalsIgnoreCase("application/x-www-form-urlencoded")) {
            translateParamsToAttributes(servletRequest, servletResponse);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
    private void translateParamsToAttributes(ServletRequest request, ServletResponse response)
    {
        Enumeration paramNames =
        request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String)
                               paramNames.nextElement();
            String [] values;
            values = request.getParameterValues(paramName);
            System.err.println("paramName = " + paramName);
            if (values.length == 1)
                request.setAttribute(paramName, values[0]);
            else
                request.setAttribute(paramName, values);
        }
    }
}

 
 
 Listing 2 
 
 public class MultipartEncodeFilter extends
BaseEncodeFilter {
    public MultipartEncodeFilter() {}
    public void doFilter(javax.servlet.ServletRequest
                         servletRequest, javax.servlet.ServletResponse
                         servletResponse,javax.servlet.FilterChain
                         filterChain)
    throws java.io.IOException,
    javax.servlet.ServletException {
        String contentType =
        servletRequest.getContentType();
        // Only filter this request if it is multipart
        // encoding
        if (contentType.startsWith("multipart/form-data")){
            try {
                String uploadFolder =
                getFilterConfig().getInitParameter("UploadFolder");

                if (uploadFolder == null) uploadFolder = ".";
                /** The MultipartRequest class is:
                * Copyright (C) 2001 by Jason Hunter
                * <[email protected]>. All rights reserved.
                **/
                MultipartRequest multi = new
                                         MultipartRequest(servletRequest,
                                                          uploadFolder,
                                                          1 * 1024 * 1024 );
                Enumeration params =
                multi.getParameterNames();
                while (params.hasMoreElements()) {
                    String name = (String)params.nextElement();
                    String value = multi.getParameter(name);
                    servletRequest.setAttribute(name, value);
                }

                Enumeration files = multi.getFileNames();
                while (files.hasMoreElements()) {
                    String name = (String)files.nextElement();
                    String filename =
                    multi.getFilesystemName(name);
                    String type = multi.getContentType(name);
                    File f = multi.getFile(name);
                    // At this point, do something with the
                    // file, as necessary
                }
            }
            catch (IOException e)
            {
                LogManager.logMessage("error reading or saving file"+ e);
            }
        } // end if
        filterChain.doFilter(servletRequest,
                             servletResponse);
    } // end method doFilter()
}


  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.