Java Code Examples for javax.servlet.ServletRequest#setCharacterEncoding()

The following examples show how to use javax.servlet.ServletRequest#setCharacterEncoding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SetCharacterEncodingFilter.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Select and set (if specified) the character encoding to be used to
 * interpret request parameters for this request.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param chain The filter chain we are processing
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)
    throws IOException, ServletException {

    // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
        String characterEncoding = selectEncoding(request);
        if (characterEncoding != null) {
            request.setCharacterEncoding(characterEncoding);
        }
    }

    // Pass control on to the next filter
    chain.doFilter(request, response);
}
 
Example 2
Source File: CharsetEncodingFilter.java    From weiyunpan with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
		FilterChain chain) throws IOException, ServletException {
	/**
	 * 设置页面编码
	 */
	HttpServletRequest request = (HttpServletRequest) req;
	HttpServletResponse response = (HttpServletResponse) resp;
	req.setCharacterEncoding("utf-8");
	resp.setCharacterEncoding("utf-8");
	
	/**
	 * 清除页面缓存
	 */
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", -10);
	chain.doFilter(req, resp);

}
 
Example 3
Source File: SetCharacterEncodingFilter.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Select and set (if specified) the character encoding to be used to
 * interpret request parameters for this request.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param chain The filter chain we are processing
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)
       throws IOException, ServletException {

    // Conditionally select and set the character encoding to be used
    if ((request.getCharacterEncoding() == null)) {
        String encodingIn = selectEncoding(request);
        if (encoding != null) {
            request.setCharacterEncoding(encodingIn);
            response.setContentType("text/html; charset=" + encodingIn);
            response.setCharacterEncoding(encodingIn);
        }
    }

    // Pass control on to the next filter
    chain.doFilter(request, response);

}
 
Example 4
Source File: SetCharacterEncodingFilter.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Select and set (if specified) the character encoding to be used to
 * interpret request parameters for this request.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param chain The filter chain we are processing
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)
    throws IOException, ServletException {

    // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
        String characterEncoding = selectEncoding(request);
        if (characterEncoding != null) {
            request.setCharacterEncoding(characterEncoding);
        }
    }

    // Pass control on to the next filter
    chain.doFilter(request, response);
}
 
Example 5
Source File: BirtSoapMessageDispatcherServlet.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest,
 *      javax.servlet.ServletResponse)
 */
public void service( ServletRequest req, ServletResponse res )
		throws ServletException, IOException
{
	// TODO: since eclipse Jetty doesn't support filter, set it here for
	// workaround
	if ( req.getCharacterEncoding( ) == null )
		req.setCharacterEncoding( IBirtConstants.DEFAULT_ENCODE );

	// workaround for Jetty
	req.setAttribute( "ServletPath", ((HttpServletRequest)req).getServletPath( ) ); //$NON-NLS-1$		
	
	super.service( req, res );
}
 
Example 6
Source File: ViewerFilter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public void doFilter( ServletRequest request, ServletResponse response,
		FilterChain chain ) throws IOException, ServletException
{
	if ( request.getCharacterEncoding( ) == null && encoding != null )
		request.setCharacterEncoding( encoding );
	// for >= 9.3.x jetty needs this property to change request encoding,
	// this might change for future versions
	request.setAttribute("org.eclipse.jetty.server.Request.queryEncoding", encoding);
	chain.doFilter( request, response );
}
 
Example 7
Source File: EncodeFilter.java    From ALLGO with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response,
		FilterChain filterChain) throws IOException, ServletException {
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");
	
	filterChain.doFilter(request, response);

}
 
Example 8
Source File: EncodingFilter.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
    	throws IOException, ServletException {
    	logger.debug("IN");
    	
    	String encoding = filterConfig.getInitParameter("encoding");
    	if(encoding == null) encoding = "UTF-8";        	
    	request.setCharacterEncoding(encoding);
		
    	chain.doFilter(request, response);
}
 
Example 9
Source File: BaseReportEngineServlet.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest,
 *      javax.servlet.ServletResponse)
 */
public void service( ServletRequest req, ServletResponse res )
		throws ServletException, IOException
{
	// TODO: since eclipse Jetty doesn't support filter, set it here for
	// workaround
	if ( req.getCharacterEncoding( ) == null )
		req.setCharacterEncoding( IBirtConstants.DEFAULT_ENCODE );

	// workaround for Jetty
	req.setAttribute( "ServletPath", ((HttpServletRequest)req).getServletPath( ) ); //$NON-NLS-1$		
	
	super.service( req, res );
}
 
Example 10
Source File: EncodeFilter.java    From sso-oauth2 with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
		throws IOException, ServletException {
	req.setCharacterEncoding(code);
	resp.setCharacterEncoding(code);
	chain.doFilter(req, resp);
}
 
Example 11
Source File: EncodingFilter.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Apply the filter
 * 
 * @param request
 *            The HTTP request
 * @param response
 *            The HTTP response
 * @param filterChain
 *            The Filter Chain
 * @throws IOException
 *             If an error occured
 * @throws ServletException
 *             If an error occured
 */
public void doFilter( ServletRequest request, ServletResponse response, FilterChain filterChain ) throws IOException, ServletException
{
    // Wrap the response object. You should create a mechanism
    // to ensure the response object only gets wrapped once.
    // In this example, the response object will inappropriately
    // get wrapped multiple times during a forward.
    response = new EncodingServletResponse( (HttpServletResponse) response );

    // Specify the encoding to assume for the request so
    // the parameters can be properly decoded/.
    request.setCharacterEncoding( REQUEST_ENCODING );

    filterChain.doFilter( request, response );
}
 
Example 12
Source File: EncoderFilter.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse resposne, FilterChain filter)
		throws IOException, ServletException {
	/*HttpServletRequest req = (HttpServletRequest) request;
	System.out.println("sessionId\t"+req.getSession().getId());
	System.out.println(req.getRemoteAddr()+"设定编码");*/
	//设定编码
	request.setCharacterEncoding("UTF-8");
	resposne.setCharacterEncoding("UTF-8");
	filter.doFilter(request, resposne);
}
 
Example 13
Source File: CharacterEncodingFilter.java    From MediaPlayer with GNU General Public License v2.0 4 votes vote down vote up
public void doFilter(ServletRequest request, ServletResponse response,
		FilterChain chain) throws IOException, ServletException {
	request.setCharacterEncoding(encoding);
	response.setContentType("text/html;charset=" + encoding);
	chain.doFilter(request, response);
}
 
Example 14
Source File: EncodingFilter.java    From unitime with Apache License 2.0 4 votes vote down vote up
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
	req.setCharacterEncoding(iEncoding);
	resp.setCharacterEncoding(iEncoding);
	chain.doFilter(req, resp);
}
 
Example 15
Source File: Character.java    From mealOrdering with MIT License 4 votes vote down vote up
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	response.setCharacterEncoding("UTF-8");
	response.setContentType("text/html; charset=UTF-8");
	request.setCharacterEncoding("UTF-8");
	chain.doFilter(request, response);
}
 
Example 16
Source File: CharacterEncodingFilter.java    From ctsms with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
	request.setCharacterEncoding("UTF-8");
	chain.doFilter(request, response);
}
 
Example 17
Source File: KeycloakSessionServletFilter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    servletRequest.setCharacterEncoding("UTF-8");

    final HttpServletRequest request = (HttpServletRequest)servletRequest;

    KeycloakSessionFactory sessionFactory = (KeycloakSessionFactory) servletRequest.getServletContext().getAttribute(KeycloakSessionFactory.class.getName());
    KeycloakSession session = sessionFactory.create();
    Resteasy.pushContext(KeycloakSession.class, session);
    ClientConnection connection = new ClientConnection() {
        @Override
        public String getRemoteAddr() {
            return request.getRemoteAddr();
        }

        @Override
        public String getRemoteHost() {
            return request.getRemoteHost();
        }

        @Override
        public int getRemotePort() {
            return request.getRemotePort();
        }

        @Override
        public String getLocalAddr() {
            return request.getLocalAddr();
        }

        @Override
        public int getLocalPort() {
            return request.getLocalPort();
        }
    };
    session.getContext().setConnection(connection);
    Resteasy.pushContext(ClientConnection.class, connection);

    KeycloakTransaction tx = session.getTransactionManager();
    Resteasy.pushContext(KeycloakTransaction.class, tx);
    tx.begin();

    try {
        filterChain.doFilter(servletRequest, servletResponse);
    } finally {
        if (servletRequest.isAsyncStarted()) {
            servletRequest.getAsyncContext().addListener(createAsyncLifeCycleListener(session));
        } else {
            closeSession(session);
        }
    }
}
 
Example 18
Source File: EncodingFilter.java    From hasting with MIT License 4 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	request.setCharacterEncoding(characterEncoding);
	response.setCharacterEncoding(characterEncoding);
	chain.doFilter(request, response);
}
 
Example 19
Source File: KeycloakSessionServletFilter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    servletRequest.setCharacterEncoding("UTF-8");

    final HttpServletRequest request = (HttpServletRequest)servletRequest;

    KeycloakSessionFactory sessionFactory = (KeycloakSessionFactory) servletRequest.getServletContext().getAttribute(KeycloakSessionFactory.class.getName());
    KeycloakSession session = sessionFactory.create();
    Resteasy.pushContext(KeycloakSession.class, session);
    ClientConnection connection = new ClientConnection() {
        @Override
        public String getRemoteAddr() {
            return request.getRemoteAddr();
        }

        @Override
        public String getRemoteHost() {
            return request.getRemoteHost();
        }

        @Override
        public int getRemotePort() {
            return request.getRemotePort();
        }

        @Override
        public String getLocalAddr() {
            return request.getLocalAddr();
        }

        @Override
        public int getLocalPort() {
            return request.getLocalPort();
        }
    };
    session.getContext().setConnection(connection);
    Resteasy.pushContext(ClientConnection.class, connection);

    KeycloakTransaction tx = session.getTransactionManager();
    Resteasy.pushContext(KeycloakTransaction.class, tx);
    tx.begin();

    try {
        filterChain.doFilter(servletRequest, servletResponse);
    } finally {
        if (servletRequest.isAsyncStarted()) {
            servletRequest.getAsyncContext().addListener(createAsyncLifeCycleListener(session));
        } else {
            closeSession(session);
        }
    }
}
 
Example 20
Source File: SetCharacterEncodingFilter.java    From ralasafe with MIT License 3 votes vote down vote up
/**
 * Select and set (if specified) the character encoding to be used to
 * interpret request parameters for this request.
 * 
 * @param request
 *            The servlet request we are processing
 * @param result
 *            The servlet response we are creating
 * @param chain
 *            The filter chain we are processing
 * 
 * @exception IOException
 *                if an input/output error occurs
 * @exception ServletException
 *                if a servlet error occurs
 */
public void doFilter(ServletRequest request, ServletResponse response,
		FilterChain chain) throws IOException, ServletException {
	// Conditionally select and set the character encoding to be used
	if (ignore || (request.getCharacterEncoding() == null)) {
		String encoding = selectEncoding(request);
		if (encoding != null)
			request.setCharacterEncoding(encoding);
	}

	// Pass control on to the next filter
	chain.doFilter(request, response);
}