Example usage for javax.servlet ServletResponse setCharacterEncoding

List of usage examples for javax.servlet ServletResponse setCharacterEncoding

Introduction

In this page you can find the example usage for javax.servlet ServletResponse setCharacterEncoding.

Prototype

public void setCharacterEncoding(String charset);

Source Link

Document

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.

Usage

From source file:com.ideabase.repository.server.impl.JettyRepositoryServerImpl.java

private Context setupWebServiceHandler() throws InterruptedException, IOException {
    final String tmpDir = mConfiguration.getProperty(KEY_TMPDIR);
    final File tmpDirFile = new File(tmpDir);
    if (!tmpDirFile.exists()) {
        tmpDirFile.mkdirs();//w w  w.ja v  a  2 s . c o  m
    }

    final Context context = new Context(mServer, "/", Context.SESSIONS);

    // add jsp support
    context.addServlet(JspServlet.class, "*.jsp");
    context.setBaseResource(Resource.newClassPathResource("webapp"));
    context.addServlet(DefaultServlet.class, "/");

    // add spring servlet
    final ServletHolder servletHolder = new ServletHolder(DispatcherServlet.class) {
        @Override
        public void handle(final ServletRequest pRequest, final ServletResponse pResponse)
                throws ServletException, UnavailableException, IOException {
            pRequest.setCharacterEncoding(ENCODING_UTF);
            pResponse.setCharacterEncoding(ENCODING_UTF);
            super.handle(pRequest, pResponse);
        }
    };
    servletHolder.setInitParameter("contextConfigLocation", "classpath:/applicationContext.xml");
    servletHolder.setName("webservice-action-servlet");
    context.addServlet(servletHolder, "/rest/*");

    // add spring context listener
    context.getInitParams().put("contextConfigLocation", "classpath:/applicationContext.xml");
    context.addEventListener(new ContextLoaderListener());

    // add log4j servlet
    context.addServlet(Log4jConfigServlet.class, "/log4j");

    return context;
}

From source file:com.zyeeda.framework.web.CharacterEncodingFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    if (this.encoding != null && (this.forceEncoding || StringUtils.isBlank(req.getCharacterEncoding()))) {
        req.setCharacterEncoding(encoding);
        if (this.forceEncoding) {
            res.setCharacterEncoding(this.encoding);
        }//from w  w  w  . j a  v a2s  .  c  om
    }

    chain.doFilter(req, res);
}

From source file:cn.vlabs.duckling.vwb.VWBFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    request.setCharacterEncoding(encoding);
    response.setCharacterEncoding(encoding);
    ThreadLocalCache.init(100);//  w w w .  j  av a2 s .c o m
    if (request instanceof HttpServletRequest) {
        HttpServletRequest req = (HttpServletRequest) request;
        String localeString = getLocaleString(req);
        Locale locale = getLocale(localeString);
        WrapperedRequest wrappered;
        if (locale != null) {
            wrappered = new WrapperedRequest(req, (HttpServletResponse) response, locale);
        } else {
            wrappered = new WrapperedRequest(req, (HttpServletResponse) response);
        }
        filterChain.doFilter(wrappered, response);
    }
    ThreadLocalCache.clear();
}

From source file:info.magnolia.cms.filters.ContentTypeFilter.java

private void setContentType(ServletRequest req, ServletResponse resp) {
    resp.setContentType(MIMEMapping.getMIMEType((HttpServletRequest) req));
    String characterEncoding = MIMEMapping.getContentEncoding((HttpServletRequest) req);

    if (StringUtils.isEmpty(characterEncoding)) {
        characterEncoding = "UTF-8"; //$NON-NLS-1$
    }/*w  w w  .  j a v  a 2  s. co m*/

    resp.setCharacterEncoding(characterEncoding);

    try {
        req.setCharacterEncoding(characterEncoding);
    } catch (IllegalStateException e) {
        log.debug("can't set character encoding for the request", e); //$NON-NLS-1$
    } catch (UnsupportedEncodingException e) {
        log.error("can't set character encoding for the request", e); //$NON-NLS-1$
    }
}

From source file:org.opencms.gwt.CmsGwtService.java

/**
 * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
 *//*ww  w.  j  a va 2  s. c o  m*/
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {

    arg1.setCharacterEncoding(arg0.getCharacterEncoding());
    super.service(arg0, arg1);
}

From source file:o2.web.HelloWorldServlet.java

@Override
public void service(ServletRequest req, ServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    ApplicationContext context = new ClassPathXmlApplicationContext("knights.xml");
    Knight knight = (Knight) context.getBean("knight");
    knight.embarkOnQuest();//from   w w  w  . j  a v  a 2s .c  o m

    response.setContentType("text/html;charset=utf-8");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();
    String title = "Webapp Demo";
    out.println(
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
    out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
    out.println("<head>");
    out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />");
    out.println("<title>" + title + "</title>");
    out.println("<body>");
    out.println("<h1>Hello World!O2</h1>");
    out.println("</body>");
    out.println("</html>");

}

From source file:cc.kune.core.server.rack.filters.rest.RESTServiceFilter.java

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {

    final String methodName = RackHelper.getMethodName(request, pattern);
    final ParametersAdapter parameters = new ParametersAdapter(request);
    LOG.debug("JSON METHOD: '" + methodName + "' on: " + serviceClass.getSimpleName());

    response.setCharacterEncoding("utf-8");

    final String callbackMethod = getCallbackMethod(request);
    final boolean isJsonP = isJSONPRequest(callbackMethod);

    response.setContentType(isJsonP ? "text/javascript" : "text/json");

    final RESTResult result = transactionalFilter.doService(serviceClass, methodName, parameters,
            getInstance(serviceClass));/*  w w w.  ja  v  a  2  s . c  o m*/
    if (result != null) {
        if (result.getOutput() != null) {
            final Object output = wrap(result.getOutput(), isJsonP, callbackMethod);
            if (output != null) {
                final PrintWriter writer = response.getWriter();
                writer.print(output);
                writer.flush();
            } else {
                chain.doFilter(request, response);
            }

        } else {
            chain.doFilter(request, response);
        }
    }
}

From source file:com.netsteadfast.greenstep.sys.GreenStepBaseFormAuthenticationFilter.java

@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
        ServletResponse response) throws Exception {

    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    if (!this.isAjaxRequest(httpServletRequest)) {
        httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + this.getSuccessUrl());
    } else {//from  w  ww  .j a  v  a2 s .  c  o  m
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().write(Constants.NO_AUTHZ_JSON_DATA);
    }
    return false;
}

From source file:com.netsteadfast.greenstep.sys.GreenStepMobileFormAuthenticationFilter.java

@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
        ServletResponse response) throws Exception {

    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    if (!this.isAjaxRequest(httpServletRequest)) {
        httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + this.getSuccessUrl());
    } else {/*from   w  ww . j  ava 2s. c om*/
        response.setCharacterEncoding(Constants.BASE_ENCODING);
        response.setContentType("application/json");
        response.getWriter().write(Constants.NO_AUTHZ_JSON_DATA);
    }
    return false;
}

From source file:edu.cornell.mannlib.vitro.webapp.filters.CharsetEncodingFilter.java

private void setContentTypeOnResponse(HttpServletRequest req, ServletResponse servletResponse) {
    if (servletResponse.getContentType() != null) {
        return;/* w w  w . ja v a 2s  .c  o m*/
    }

    String uri = req.getRequestURI();
    if (!hasExtension(uri)) {
        servletResponse.setContentType("text/html;charset=UTF-8");
        return;
    }

    String typeFromContext = req.getSession().getServletContext().getMimeType(uri);
    if (typeFromContext == null) {
        servletResponse.setContentType("text/html;charset=UTF-8");
        return;
    }

    servletResponse.setContentType(typeFromContext);
    servletResponse.setCharacterEncoding("UTF-8");
}