本文整理汇总了C++中HttpServletResponse::setContentType方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpServletResponse::setContentType方法的具体用法?C++ HttpServletResponse::setContentType怎么用?C++ HttpServletResponse::setContentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpServletResponse
的用法示例。
在下文中一共展示了HttpServletResponse::setContentType方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: doPost
// POST 方法
virtual bool doPost(HttpServletRequest& req, HttpServletResponse& res)
{
// 如果 session 项不存在,则设置
#if 0
const char* sid = req.getSession().getAttribute("sid");
if (*sid == 0)
req.getSession().setAttribute("sid", "xxxxxx");
#endif
// 创建 HTTP 响应头
res.addCookie("name1", "value1");
res.addCookie("name2", "value2", ".test.com", "/", 3600 * 24);
// res.setStatus(400); // 可以设置返回的状态码
// 两种方式都可以设置字符集
if (0)
res.setContentType("text/xml; charset=gb2312");
else
{
res.setContentType("text/xml");
res.setCharacterEncoding("gb2312");
}
// 获得 HTTP 请求的数据类型,正常的参数类型,即 name&value 方式
// 还是 MIME 数据类型,还是数据流类型
http_request_t request_type = req.getRequestType();
if (request_type == HTTP_REQUEST_NORMAL)
return doParams(req, res);
else if (request_type == HTTP_REQUEST_MULTIPART_FORM)
return doUpload(req, res);
assert(request_type == HTTP_REQUEST_OCTET_STREAM);
return doOctetStream(req, res);
}
示例2: doPost
// POST 方法
virtual bool doPost(HttpServletRequest& req, HttpServletResponse& res)
{
first_ = false;
logger("request one now");
// 创建 HTTP 响应头
res.addCookie("name1", "value1");
res.addCookie("name2", "value2", ".test.com", "/", 3600 * 24);
res.setChunkedTransferEncoding(true);
res.setKeepAlive(true);
// res.setStatus(400); // 可以设置返回的状态码
// 两种方式都可以设置字符集
if (0)
res.setContentType("text/xml; charset=gb2312");
else
{
res.setContentType("text/xml");
res.setCharacterEncoding("gb2312");
}
// 获得 HTTP 请求的数据类型,正常的参数类型,即 name&value 方式
// 还是 MIME 数据类型,还是数据流类型
http_request_t request_type = req.getRequestType();
if (request_type == HTTP_REQUEST_NORMAL)
return doParams(req, res);
else if (request_type == HTTP_REQUEST_MULTIPART_FORM)
return doUpload(req, res);
assert(request_type == HTTP_REQUEST_OCTET_STREAM);
return doOctetStream(req, res);
}
示例3: doPost
virtual bool doPost(HttpServletRequest& req, HttpServletResponse& res)
{
const char* sid = req.getSession().getAttribute("sid");
if (*sid == 0)
req.getSession().setAttribute("sid", "xxxxxx");
sid = req.getSession().getAttribute("sid");
const char* cookie1 = req.getCookieValue("name1");
const char* cookie2 = req.getCookieValue("name2");
// 创建 HTTP 响应头
res.addCookie("name1", "value1");
res.addCookie("name2", "value2", ".test.com", "/", 3600 * 24);
// res.setStatus(400); // 可以设置返回的状态码
// 两种方式都可以设置字符集
if (0)
res.setContentType("text/xml; charset=gb2312");
else
{
res.setContentType("text/xml");
res.setCharacterEncoding("gb2312");
}
const char* param1 = req.getParameter("name1");
const char* param2 = req.getParameter("name2");
// 创建 xml 格式的数据体
xml body;
body.get_root().add_child("root", true)
.add_child("sessions", true)
.add_child("session", true)
.add_attr("sid", sid ? sid : "null")
.get_parent()
.get_parent()
.add_child("cookies", true)
.add_child("cookie", true)
.add_attr("name1", cookie1 ? cookie1 : "null")
.get_parent()
.add_child("cookie", true)
.add_attr("name2", cookie2 ? cookie2 : "null")
.get_parent()
.get_parent()
.add_child("params", true)
.add_child("param", true)
.add_attr("name1", param1 ? param1 : "null")
.get_parent()
.add_child("param", true)
.add_attr("name2", param2 ? param2 : "null");
string buf;
body.build_xml(buf);
// 发送 http 响应头
if (res.sendHeader() == false)
return false;
// 发送 http 响应体
if (res.getOutputStream().write(buf) == -1)
return false;
return true;
}
示例4: doGet
void RefreshTestServlet::doGet(HttpServletRequest& req, HttpServletResponse& resp)
{
Timestamp now;
std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT));
Timespan ts(_started.elapsed());
resp.setHeader("Transfer-Encoding", "chunked");
resp.setContentType("text/html");
resp.getOutputStream()
<< "<html><head><title>Poco Servlet Engine Test</title>"
<< "<meta http-equiv=\"refresh\" content=\"1\"></head>"
<< "<body>"
<< "<p style=\"text-align: center; font-size: 48px;\">Current UTC time: "
<< dt
<< "</p>"
<< "<p style=\"text-align: center; font-size: 48px;\">Elapsed: "
<< std::setw(2) << std::setfill('0') << ts.hours() << ':'
<< std::setw(2) << std::setfill('0') << ts.minutes() << ':'
<< std::setw(2) << std::setfill('0') << ts.seconds()
<< "</p>"
<< "<p style=\"text-align: center; font-size: 48px;\">Hit Count: "
<< ++_hitCounter
<< "</p>"
<< "</body></html>";
}
示例5: doGet
void doGet(HttpServletRequest& req, HttpServletResponse &res) {
res.setContentType("text/plain");
ostream& out = res.getOutputStream();
out << "pid: " << getpid() << endl << endl;
string action = req.getParameter("action");
if(action == "list") {
servlet::loader& loader = servlet::loader::instance();
out << "Loaded servlets(" << loader.pool.size() << "):" << endl;
int cnt = 0;
char timebuf[256];
for(std::map<std::string, servlet::loader::pool_entry>::const_iterator i = loader.pool.begin();
i != loader.pool.end(); ++i)
{
apr_rfc822_date(timebuf, i->second.atime);
HttpServlet* servlet = i->second.servlet;
out << ++cnt << ": " << (servlet ? typeid(*servlet).name() : "NULL") <<
"\t(Access: " << timebuf << ", Load: ";
apr_rfc822_date(timebuf, i->second.ltime);
out << timebuf << ")" << endl;
}
}else if(action == "load") {
string path = req.getParameter("path");
servlet::loader::instance().load(path);
out << "Loaded " << path << endl;
}else {
out << "Unknown action: " << action << endl;
}
}
示例6: doOther
virtual bool doOther(HttpServletRequest&, HttpServletResponse& res,
const char* method)
{
res.setStatus(400);
res.setContentType("text/xml; charset=gb2312");
// 发送 http 响应头
if (res.sendHeader() == false)
return false;
// 发送 http 响应体
string buf;
buf.format("<root error='unkown method: %s' />\r\n", method);
(void) res.getOutputStream().write(buf);
return false;
}
示例7: doGet
void doGet(HttpServletRequest& request, HttpServletResponse &response) {
response.setContentType("text/html");
ostream& out = response.getOutputStream();
out << "<html><body>" << endl;
HttpSession* session = request.getSession();
if(session->hasAttribute("lastVisit")) {
time_t lastVisit = session->getAttribute<time_t>("lastVisit");
out << "Witaj ponownie. Pierwszy raz odwiedziles nas: <br>"
<< ctime(&lastVisit) << endl;
}else{
out << "Witaj po raz pierszy!" << endl;
time_t lastVisit = time(NULL);
session->setAttribute("lastVisit", lastVisit);
}
out << "</body></html>" << endl;
}
示例8: doGet
void doGet(const HttpServletRequest& request, HttpServletResponse &response) {
//dbg_stop();
response.setContentType("text/html");
ostream& out = response.getOutputStream();
out << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""
"\"http://www.w3.org/TR/html4/loose.dtd\">" << endl;
out << "<html><head><title>test</title></head><body>" << endl;
out << "<table border>" << endl;
dbi_result result = dbi_conn_query(conn, "SELECT * FROM store_products");
while(dbi_result_next_row(result)) {
out << "<tr>" << endl;
long product_id = dbi_result_get_long(result, "product_id");
long category_id = dbi_result_get_long(result, "category_id");
string name = dbi_result_get_string(result, "product_name");
float price = dbi_result_get_float(result, "price");
string img = dbi_result_get_string(result, "image_url");
string desc = dbi_result_get_string(result, "description");
bool recomended = dbi_result_get_long(result, "is_recommended")==1;
out << "<td>" << product_id << "</td>" <<endl;
out << "<td>" << category_id << "</td>" <<endl;
out << "<td>" << name << "</td>" <<endl;
out << "<td>" << price << "</td>" <<endl;
out << "<td>" << img << "</td>" <<endl;
out << "<td>" << desc << "</td>" <<endl;
out << "<td>" << recomended << "</td>" <<endl;
out << "</tr>" << endl;
}
out << "</table>" << endl;
out << "</body></html>" << endl;
dbi_result_free(result);
}
示例9: doGet
void doGet(HttpServletRequest& req, HttpServletResponse &res) {
//dbg_stop();
res.setContentType("text/html");
ostream& out = res.getOutputStream();
out << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\""
"\"http://www.w3.org/TR/html4/loose.dtd\">" << endl;
out << "<html><head><title>test</title></head><body>" << endl;
out << "PID: " << getpid() << "<hr>" << endl;
auto_ptr< vector<string> > names = req.getHeaderNames();
out << "<div style='border: solid black'>" << endl;
out << "Headers:<ul>" << endl;
for(unsigned i=0; i< names->size();++i) {
out << "<li>" << (*names)[i] << "<ul>";
auto_ptr< vector<string> > values = req.getHeaders((*names)[i]);
for(unsigned j=0; j < values->size();++j) {
out << "<li>" << (*values)[j] << "</li>";
}
out << "</ul>\n";
}
out << "</ul></div><br>\n";
out << "<div style='border: solid black'>" << endl;
out << "Cookies:<ul>" << endl;
auto_ptr< vector<Cookie> > cookies = req.getCookies();
for(unsigned i=0;i<cookies->size();++i)
out << "<li><u>" << (*cookies)[i].getName() << "</u> "
<< (*cookies)[i].getValue() << "</li>\n" << endl;
out << "</ul></div><br>\n";
out << "<div style='border: solid black'>" << endl;
out << "Parameters:<ul>" << endl;
names = req.getParameterNames();
for(unsigned i=0; i< names->size();++i) {
out << "<li>" << (*names)[i] << "<ul>";
auto_ptr< vector<string> > values = req.getParameterValues((*names)[i]);
for(unsigned j=0; j < values->size();++j) {
out << "<li>" << (*values)[j] << "</li>\n";
// Cookie c(names[i], req.getParameter(names[i]));
// res.addCookie(c);
}
out << "</ul>\n";
}
out << "</ul></div><br>";
HttpSession* session = req.getSession();
if(session){
int cnt;
if(session->isNew()){
cnt = 1;
session->setAttribute("cnt",cnt);
}else{
cnt = session->getAttribute<int>("cnt");
cnt++;
session->setAttribute("cnt",cnt);
}
out << "Your counter: " << cnt << endl;
out << "<br>ID: " << session->getId() << endl;
}else
out << "<h4>No session</h4>";
out << "</body></html>" << endl;
}