Hello,

In order to study/practice using struts 2, I am using a the struts2-archetype-starter maven archetype.

I've come across a problem trying to add my own filter, and was hoping someone could point me in the right direction.

I'm using eclipse with maven for my build process, and Tomcat 8.5 as a localhost server.


I have been able to set up some basic actions. I am now trying to add a filter to set the encoding of requests, so that I can handle Japanese input. To do this, I referenced this resource about filters, to create my own custom filter, which I reference in the web.xml file of my project

Filters reference source: https://www.oracle.com/java/technologies/filters.html

However, when I try to access my project's url, I get a 404 error.

I've tried adding breakpoints to my filter and debugging the project on the server, but the breakpoint is never hit. (I am able to debug and use breakpoints otherwise)

To my web.xml file I have added the filter declaration:

    <filter>
        <filter-name>MyEncoder</filter-name>
<filter-class>jono_group.mav_arch_2.filters.MyChaEnFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>Shift_JIS</param-value>
        </init-param>
    </filter>


and this filter mapping

    <filter-mapping>
        <filter-name>MyEncoder</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

They are positioned infront of all the other filters and filter-mappings respectively, with the intention that it will executed at the front of the filter chain.

With the above filter and filter-mapping, the maven build (clean package runs successfully, reporting no errors. But I get the 404. As soon as I remove both of them, the 404 error goes away and my actions work as expected.

Any help would be greatly appreciated.

My filter class is as follows:

package jono_group.mav_arch_2.filters;

import java.io.IOException;
import java.nio.file.DirectoryStream.Filter;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyChaEnFilter implements Filter
{

    private FilterConfig filterConfig = null;
    private String encoding;

    public void doFilter(ServletRequest request,
    ServletResponse response, FilterChain chain) throws
    IOException, ServletException {
        String encoding = selectEncoding(request);
        if (encoding != null)
        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws
    ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
    }

    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }

    public void destroy() {
        this.filterConfig = null;
    }

    @Override
    public boolean accept(Object entry) throws IOException {
        // TODO Auto-generated method stub
        return false;
    }
}

Regards,

Jonathan



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to