本文整理汇总了Java中javax.servlet.ServletResponse.setContentType方法的典型用法代码示例。如果您正苦于以下问题:Java ServletResponse.setContentType方法的具体用法?Java ServletResponse.setContentType怎么用?Java ServletResponse.setContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletResponse
的用法示例。
在下文中一共展示了ServletResponse.setContentType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: service
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
public void service(ServletRequest servletRequest, ServletResponse response) throws ServletException, IOException {
ClientSettings settings = new ClientSettings(
options.getOption(SupportService.SUPPORT_EMAIL_ADDR),
options.getOption(SupportService.SUPPORT_EMAIL_SUBJECT),
options.getOption(SupportService.OUTSIDE_COMMUNICATION_DISABLED),
options.getOption(AccelerationOptions.ENABLE_SUBHOUR_POLICIES),
options.getOption(UIOptions.ALLOW_LOWER_PROVISIONING_SETTINGS),
options.getOption(UIOptions.TABLEAU_TDS_MIMETYPE));
String environment = config.allowTestApis ? "DEVELOPMENT" : "PRODUCTION";
final ServerData indexConfig = new ServerData(environment, serverHealthMonitor, config.getConfig(), settings, getVersionInfo(), supportService.getClusterId().getIdentity());
Template tmp = templateCfg.getTemplate("/index.html");
response.setContentType("text/html; charset=utf-8");
OutputStreamWriter outputWriter = new OutputStreamWriter(response.getOutputStream());
try {
tmp.process(ImmutableMap.of("dremio", indexConfig), outputWriter);
outputWriter.flush();
outputWriter.close();
} catch (TemplateException e) {
throw new IOException("Error rendering index.html template", e);
}
}
示例3: showStatus
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
/**
* Show the status of the conf and the filter to the user.
*
* @param request
* to get status info from
* @param response
* response to show the status on.
* @throws java.io.IOException
* if the output cannot be written
*/
private void showStatus(final HttpServletRequest request, final ServletResponse response) throws IOException {
log.debug("showing status");
if (allowConfSwapViaHttp) {
String newConfPath = request.getParameter("conf");
if (!StringUtils.isBlank(newConfPath)) {
confPath = newConfPath;
loadUrlRewriterLocal();
}
}
Status status = new Status(confLastLoaded, this);
status.displayStatusInContainer(request);
response.setContentType("text/html; charset=UTF-8");
response.setContentLength(status.getBuffer().length());
final PrintWriter out = response.getWriter();
out.write(status.getBuffer().toString());
out.close();
}
示例4: service
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<!DOCTYPE html>\n"+
"<html>\n"+
"<head>\n"+
"<meta charset='UTF-8'>\n"+
"<title>Hello Servlet</title>\n"+
"</head>\n"+
"<body>\n"+
"<h2>Hello Servlet!</h2>\n"+
"Go <a href='"+
request.getServletContext().getContextPath()+
"'>Back</a>\n"+
"</body>\n"+
"</html>");
pw.close();
}
示例5: onTimeout
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
public void onTimeout(AsyncEvent event) throws IOException {
// in this time, maybe:
// 1.invocation in executor's queue
// 2.already executing in executor
// 3.already send response
// to avoid concurrent, must lock request
ServletRequest request = event.getSuppliedRequest();
HttpServletRequestEx requestEx = (HttpServletRequestEx) request.getAttribute(RestConst.REST_REQUEST);
synchronized (requestEx) {
ServletResponse response = event.getAsyncContext().getResponse();
if (!response.isCommitted()) {
LOGGER.error("Rest request timeout, method {}, path {}.", requestEx.getMethod(), requestEx.getRequestURI());
// invocation in executor's queue
response.setContentType(MediaType.APPLICATION_JSON);
// we don't know if developers declared one statusCode in contract
// so we use cse inner statusCode here
((HttpServletResponse) response).setStatus(ExceptionFactory.PRODUCER_INNER_STATUS_CODE);
PrintWriter out = response.getWriter();
out.write(TIMEOUT_MESSAGE);
response.flushBuffer();
}
request.removeAttribute(RestConst.REST_REQUEST);
}
}
示例6: doFilter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
public void doFilter(ServletRequest servletRequest, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
if (isWhiteReq(request.getRequestURI())) {
chain.doFilter(request, response);
} else {
String token = request.getHeader("token");
if (StringUtils.isNotBlank(token)) {
try {
Token tokenInfo = TokenUtil.getTokenInfo(token);
if (tokenInfo != null) {
Long now = System.currentTimeMillis();
if (now - tokenInfo.getTime() < 1000 * 60 * 30) {
String value = tokenInfo.getValue();
TokenUtil.setTokenInfo(token, value);
WebUtil.saveCurrentUser(request, value);
}
}
} catch (Exception e) {
logger.error("token检查发生异常:", e);
}
}
// 响应
if (DataUtil.isEmpty(WebUtil.getCurrentUser(request))) {
response.setContentType("text/html; charset=UTF-8");
Map<String, Object> modelMap = InstanceUtil.newLinkedHashMap();
modelMap.put("httpCode", HttpCode.UNAUTHORIZED.value());
modelMap.put("msg", HttpCode.UNAUTHORIZED.msg());
modelMap.put("timestamp", System.currentTimeMillis());
PrintWriter out = response.getWriter();
out.println(JSON.toJSONString(modelMap));
out.flush();
out.close();
} else {
chain.doFilter(request, response);
}
}
}
示例7: 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);
}
}
示例8: 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;
}
}
示例9: onLoginSuccess
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
if (!httpServletRequest.getRequestURL().toString().endsWith(".json")) {
issueSuccessRedirect(request, response);
} else {
httpServletResponse.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = httpServletResponse.getWriter();
out.println("{\"code\":200,\"info\":\"登入成功\"}");
out.flush();
out.close();
}
return true;
}
示例10: 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;
}
示例11: _getHtmlWriter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
/**
* @todo Generalize???
*/
private Writer _getHtmlWriter(FacesContext context) throws IOException
{
ServletResponse response = (ServletResponse)
context.getExternalContext().getResponse();
response.setContentType(_HTML_MIME_TYPE);
return response.getWriter();
}
示例12: 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);
}
}
示例13: doFilter
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
response.setContentType("text/plain");
response.getWriter().print("Filter");
chain.doFilter(request, response);
}
示例14: sendErrorWhenNotHttp
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
private void sendErrorWhenNotHttp(ServletResponse response) throws IOException {
response.setContentType(PLAIN_TEXT_MIME_TYPE);
response.getWriter().write(sm.getString("http.403"));
response.getWriter().flush();
}
示例15: sendErrorWhenNotHttp
import javax.servlet.ServletResponse; //导入方法依赖的package包/类
private void sendErrorWhenNotHttp(ServletResponse response)
throws IOException {
response.setContentType(PLAIN_TEXT_MIME_TYPE);
response.getWriter().write(sm.getString("http.403"));
response.getWriter().flush();
}