本文整理匯總了Java中javax.servlet.http.HttpServletResponse.setContentType方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletResponse.setContentType方法的具體用法?Java HttpServletResponse.setContentType怎麽用?Java HttpServletResponse.setContentType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.servlet.http.HttpServletResponse
的用法示例。
在下文中一共展示了HttpServletResponse.setContentType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: downloadSignedFile
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@RequestMapping(value = "/download", method = RequestMethod.GET)
public String downloadSignedFile(@ModelAttribute("signedDocument") InMemoryDocument signedDocument, HttpServletResponse response) {
try {
MimeType mimeType = signedDocument.getMimeType();
if (mimeType != null) {
response.setContentType(mimeType.getMimeTypeString());
}
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition", "attachment; filename=\"" + signedDocument.getName() + "\"");
Utils.copy(new ByteArrayInputStream(signedDocument.getBytes()), response.getOutputStream());
} catch (Exception e) {
logger.error("An error occured while pushing file in response : " + e.getMessage(), e);
}
return null;
}
示例2: execute
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method=retriveMethod(req);
if(method!=null){
invokeMethod(method, req, resp);
}else{
VelocityContext context = new VelocityContext();
context.put("contextPath", req.getContextPath());
String file=req.getParameter("file");
String project = buildProjectNameFromFile(file);
if(project!=null){
context.put("project", project);
}
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
Template template=ve.getTemplate("html/decisiontable-editor.html","utf-8");
PrintWriter writer=resp.getWriter();
template.merge(context, writer);
writer.close();
}
}
示例3: code
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* 生成代碼
*/
@RequestMapping("/code")
@RequiresPermissions("sys:generator:code")
public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{
String[] tableNames = new String[]{};
//獲取表名,不進行xss過濾
HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
String tables = orgRequest.getParameter("tables");
tableNames = JSON.parseArray(tables).toArray(tableNames);
byte[] data = sysGeneratorService.generatorCode(tableNames);
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
示例4: createSuccessResponse
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
public static String createSuccessResponse(Integer httpCode, Object result, SerializerFeature serializerFeature, SerializeFilter filter, HttpServletResponse response){
PrintWriter printWriter = null;
String jsonString = "";
try {
response.setCharacterEncoding(RESPONSE_CHARACTERENCODING);
response.setContentType(RESPONSE_CONTENTTYPE);
response.setStatus(httpCode);
printWriter = response.getWriter();
if(null != result){
if(null!=filter){
jsonString = JSONObject.toJSONString(result,filter,serializerFeature);
}else{
// jsonString = JSONObject.toJSONString(result, serializerFeature);
jsonString = JSONObject.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:ss:mm",serializerFeature);
}
printWriter.write(jsonString);
}
printWriter.flush();
} catch (Exception e) {
log.error("createResponse failed", e);
} finally {
if(null!=printWriter)printWriter.close();
}
return jsonString;
}
示例5: getText1
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@RequestMapping("/text1")
public void getText1(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
try {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("hello");
} catch (IOException e) {
logger.error("getText1 error:", e);
}
}
示例6: doPost
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("DISCONNECT PAIR SERVLET");
String pairId=request.getParameter("pairId");
// String nextPage=request.getParameter("jsp");
int id=Integer.parseInt(pairId);
DataAccess da = new DataAccess();
boolean res=true;
RequestDispatcher req = null;
response.setContentType("text/html");
res = da.disconnectPair(id);
try {
da.closeConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(res){
// response.getWriter().append("Disconnect Pair Successfull");
response.getWriter().append(""+id);
//req = request.getRequestDispatcher(nextPage);
}
else
//response.getWriter().append("Disconnect Pair Is Failure");
req.forward(request, response);
}
示例7: doGet
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Method declaration
*
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String query = request.getQueryString();
if ((query == null) || (query.length() == 0)) {
response.setContentType("text/html");
// [email protected] 20020130 - patch 1.7.0 by fredt
// to avoid caching on the browser
response.setHeader("Pragma", "no-cache");
PrintWriter out = response.getWriter();
out.println(
"<html><head><title>HSQL Database Engine Servlet</title>");
out.println("</head><body><h1>HSQL Database Engine Servlet</h1>");
out.println("The servlet is running.<p>");
if (errorStr == null) {
out.println("The database is also running.<p>");
out.println("Database name: " + dbType + dbPath + "<p>");
out.println("Queries processed: " + iQueries + "<p>");
} else {
out.println("<h2>The database is not running!</h2>");
out.println("The error message is:<p>");
out.println(errorStr);
}
out.println("</body></html>");
}
}
示例8: doGet
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
resp.getWriter().print("envEntry1: " + envEntry1);
resp.getWriter().print(" envEntry2: " + envEntry2);
resp.getWriter().print(" envEntry3: " + envEntry3);
resp.getWriter().print(" envEntry4: " + envEntry4);
resp.getWriter().print(" envEntry5: " + envEntry5);
resp.getWriter().print(" envEntry6: " + envEntry6);
}
開發者ID:sunmingshuai,項目名稱:apache-tomcat-7.0.73-with-comment,代碼行數:12,代碼來源:TesterServletWithAnnotations.java
示例9: processPendingResponses
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
synchronized void processPendingResponses(HttpServletRequest req, HttpServletResponse resp) throws IOException {
JSONArray responses = new JSONArray();
JSONStreamAware pendingResponse;
while ((pendingResponse = pendingResponses.poll()) != null) {
responses.add(pendingResponse);
}
if (responses.size() > 0) {
JSONObject combinedResponse = new JSONObject();
combinedResponse.put("responses", responses);
if (asyncContext != null) {
asyncContext.getResponse().setContentType("text/plain; charset=UTF-8");
try (Writer writer = asyncContext.getResponse().getWriter()) {
combinedResponse.writeJSONString(writer);
}
asyncContext.complete();
asyncContext = req.startAsync();
asyncContext.addListener(new UserAsyncListener());
asyncContext.setTimeout(5000);
} else {
resp.setContentType("text/plain; charset=UTF-8");
try (Writer writer = resp.getWriter()) {
combinedResponse.writeJSONString(writer);
}
}
} else {
if (asyncContext != null) {
asyncContext.getResponse().setContentType("text/plain; charset=UTF-8");
try (Writer writer = asyncContext.getResponse().getWriter()) {
JSON.emptyJSON.writeJSONString(writer);
}
asyncContext.complete();
}
asyncContext = req.startAsync();
asyncContext.addListener(new UserAsyncListener());
asyncContext.setTimeout(5000);
}
}
示例10: processRequest
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
DAOTipoMoeda daoTipoMoeda = null;
TipoMoeda tipoMoeda = null;
RequestDispatcher view = null;
String msg = null;
String tipoMsg = "success";
try {
daoTipoMoeda = new DAOTipoMoeda();
tipoMoeda = new TipoMoeda();
tipoMoeda.setNome(request.getParameter("txtNome"));
tipoMoeda.setSimbolo(request.getParameter("txtSimbolo"));
ArrayList<TipoMoeda> lista = daoTipoMoeda.consultar(tipoMoeda);
request.setAttribute("al", lista);
} catch (Exception exception) {
tipoMsg = "danger";
msg = exception.getMessage();
} finally {
view = request.getRequestDispatcher("/pages/tipoMoedaConsultar.jsp");
request.setAttribute("mensagem", msg);
request.setAttribute("tipoMsg", tipoMsg);
view.forward(request, response);
}
}
示例11: onLogoutSuccess
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
Map<String, String> result = new HashMap<>();
result.put( "result", "success" );
response.setContentType("application/json");
response.getWriter().write( objectMapper.writeValueAsString( result ) );
response.setStatus(HttpServletResponse.SC_OK);
}
示例12: writeAJAXResponse
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
protected void writeAJAXResponse(HttpServletResponse response, String output) throws IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
if (output.length() > 0) {
out.println(output);
}
out.flush();
out.close();
}
示例13: processRequest
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
if(sc==null)
{
sc = getServletConfig();
if(sc!=null) init(sc);
}
Map<String, String> __params = new HashMap<String, String>();
Enumeration __e = request.getParameterNames();
while (__e.hasMoreElements()) {
String tField = __e.nextElement().toString();
__params.put(tField, request.getParameter(tField));
}
__params.put("contextpath", request.getContextPath());
String __result=getResponce(__params);
out.println(__result);
} finally {
out.flush();
out.close();
}
}
示例14: wapPay
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Wap支付
* https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.dfHHR3&
* treeId=203&articleId=105285&docType=1
*
* @throws AlipayApiException
* @throws IOException
*/
public static void wapPay(HttpServletResponse response, AlipayTradeWapPayModel model, String returnUrl,
String notifyUrl) throws AlipayApiException, IOException {
String form = wapPayToString(response, model, returnUrl, notifyUrl);
HttpServletResponse httpResponse = response;
httpResponse.setContentType("text/html;charset=" + AliPayConfig.build().getCharset());
httpResponse.getWriter().write(form);// 直接將完整的表單html輸出到頁麵
httpResponse.getWriter().flush();
}
示例15: ajaxPolling
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Ajax輪詢
* @param response HttpServletResponse
* @param requestTime 開始請求時間
* @throws Exception
*/
@RequestMapping(value="/ajax/polling", method= RequestMethod.GET)
public void ajaxPolling(HttpServletResponse response,long requestTime) throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
Thread.sleep(new Random().nextInt(1000)); //模擬業務處理時間
int num = new Random().nextInt(100); //模擬查詢到的數據,查到就返回
long responseTime = System.currentTimeMillis();
if(num >10 && num <50){ //有數據變化
writer.print(String.format("response time: %s, request time: %s, result: %d", responseTime,requestTime,num));
}else{ //無數據變化
writer.print(String.format("response time: %s, request time: %s, result: %s", responseTime,requestTime,"無數據變化"));
}
}