本文整理汇总了Java中javax.servlet.ServletResponse.setCharacterEncoding方法的典型用法代码示例。如果您正苦于以下问题:Java ServletResponse.setCharacterEncoding方法的具体用法?Java ServletResponse.setCharacterEncoding怎么用?Java ServletResponse.setCharacterEncoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletResponse
的用法示例。
在下文中一共展示了ServletResponse.setCharacterEncoding方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onPreHandle
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
if (this.isAccessAllowed(request, response, mappedValue) && this.isLoginRequest(request, response)) {
if (((HttpServletRequest)request).getRequestURL().toString().endsWith(".json")){
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("{\"code\":200,\"info\":\"already logined\"}");
out.flush();
out.close();
}else {
WebUtils.issueRedirect(request,response,this.getSuccessUrl());
}
return false;
}
return super.onPreHandle(request, response, mappedValue);
}
示例2: writeJson
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
/**
* response 输出JSON
* @param response
* @param resultMap
* @throws IOException
*/
public static void writeJson(ServletResponse response, Map<String, Object> resultMap){
PrintWriter out = null;
try {
response.setCharacterEncoding("UTF-8");
out = response.getWriter();
out.print(JSON.toJSONString(resultMap));
} catch (Exception e) {
logger.error("输出JSON异常:", e);
}finally{
if(null != out){
out.flush();
out.close();
}
}
}
示例3: redirectToLogin
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
protected void redirectToLogin(ServletRequest req, ServletResponse res) throws IOException {
HttpServletRequest request = (HttpServletRequest) req;
if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
res.setContentType("text/xml");
res.setCharacterEncoding("UTF-8");
String loginUrl = getLoginUrl();
if (loginUrl.startsWith("/") || !loginUrl.startsWith("http")) {
// If it is a relative URL, use the context path.
loginUrl = request.getContextPath() + loginUrl;
}
res.getWriter().printf(FACES_REDIRECT_XML, loginUrl);
} else {
super.redirectToLogin(req, res);
}
}
示例4: onAccessDenied
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
if (isLoginRequest(request, response)) {
if (isLoginSubmission(request, response)) {
if (log.isTraceEnabled()) {
log.trace("Login submission detected. Attempting to execute login.");
}
return executeLogin(request, response);
} else {
if (log.isTraceEnabled()) {
log.trace("Login page view.");
}
// allow them to see the login page ;)
return true;
}
} else {
if (log.isTraceEnabled()) {
log.trace("Attempting to access a path which requires authentication. Forwarding to the "
+ "Authentication url [" + getLoginUrl() + "]");
}
if (!((HttpServletRequest)request).getRequestURL().toString().endsWith(".json")) {
saveRequestAndRedirectToLogin(request, response);
} else {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("{\"code\":-203,\"info\":\"login\"}");
out.flush();
out.close();
}
return false;
}
}
示例5: onLoginFailure
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
if (!((HttpServletRequest)request).getRequestURL().toString().endsWith(".json")) {
setFailureAttribute(request, e);
return true;
}
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
String message = e.getClass().getSimpleName();
if ("IncorrectCredentialsException".equals(message)
|| "UnknownAccountException".equals(message)
) {
out.println("{\"code\":-100010,\"info\":\"账号或密码错误\"}");
}else if("ExcessiveAttemptsException".equals(message)){
out.println("{\"code\":-100020,\"info\":\"密码错误次数超过限制,请10分钟后重试!\"}");
}else if("LockedAccountException".equals(message)){
out.println("{\"code\":-100030,\"info\":\"账号已停用!\"}");
} else {
out.println("{\"code\":-100500,\"info\":\"未知错误\"}");
}
out.flush();
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
示例6: doFilter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(defaultEncoding);
response.setCharacterEncoding(defaultEncoding);
chain.doFilter(request, response);
}
示例7: doFilter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
String loginURI = req.getContextPath() + "/login";
boolean loggedIn = session != null && session.getAttribute("user") != null;
boolean loginRequest = req.getRequestURI().equals(loginURI);
boolean resourceRequest = req.getRequestURI().startsWith(req.getContextPath() + RESOURCE_IDENTIFIER + "/");
boolean ajaxRequest = "partial/ajax".equals(req.getHeader("Faces-Request"));
if (loggedIn || loginRequest || resourceRequest) {
if (!resourceRequest) {
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
res.setHeader("Pragma", "no-cache");
res.setDateHeader("Expires", 0);
}
chain.doFilter(new AuthRequestWrapper((HttpServletRequest) request), response);
} else if (ajaxRequest) {
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
response.getWriter().printf(AJAX_REDIRECT_XML, loginURI);
} else {
res.sendRedirect(loginURI);
}
}
示例8: doFilter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
filterChain.doFilter(request, response);
}
示例9: doFilter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if(encoding!=null){
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
}
filterChain.doFilter(request, response);
}
示例10: doFilter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
/**
* @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);
}