本文整理汇总了C++中HttpServletRequest类的典型用法代码示例。如果您正苦于以下问题:C++ HttpServletRequest类的具体用法?C++ HttpServletRequest怎么用?C++ HttpServletRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HttpServletRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: doGet
void FormTestServlet::doGet(HttpServletRequest& req, HttpServletResponse& resp)
{
ServletOutputStream& ostr = resp.getOutputStream();
ostr <<
"<html>\n"
"<head>\n"
"<title>POCO Form Server Sample</title>\n"
"</head>\n"
"<body>\n"
"<h1>POCO Form Server Sample</h1>\n"
"<h2>GET Form</h2>\n"
"<form method=\"GET\" action=\"/ServletTest/FormTestServlet\">\n"
"<input type=\"text\" name=\"text\" size=\"31\">\n"
"<input type=\"submit\" value=\"GET\">\n"
"</form>\n"
"<h2>POST Form</h2>\n"
"<form method=\"POST\" action=\"/ServletTest/FormTestServlet\">\n"
"<input type=\"text\" name=\"text\" size=\"31\">\n"
"<input type=\"submit\" value=\"POST\">\n"
"</form>\n";
ostr << "<h2>Request</h2><p>\n";
ostr << "Method: " << req.getMethod() << "<br>\n";
ostr << "URI: " << req.getRequestURI() << "<br>\n";
std::vector<std::string> names = req.getParameterNames();
std::vector<std::string>::iterator it = names.begin();
for(; it != names.end(); ++it)
ostr << *it << ":" << req.getParameter(*it);
ostr << "</p>";
ostr << "</body>\n";
}
示例3: 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;
}
}
示例4: doParams
// GET 方式或 POST 方式且满足:
// Content-Type: application/x-www-form-urlencoded
bool doParams(HttpServletRequest& req, HttpServletResponse& res)
{
param1_ = req.getParameter("name1");
param2_ = req.getParameter("name2");
param3_ = req.getParameter("name2");
return doResponse(req, res);
}
示例5: 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);
}
示例6: doResponse
bool doResponse(HttpServletRequest& req, HttpServletResponse& res)
{
// 获得浏览器传来的 cookie 值
const char* cookie1 = req.getCookieValue("name1");
const char* cookie2 = req.getCookieValue("name2");
// 获得 sid session 值
const char* sid = req.getSession().getAttribute("sid");
// 创建 xml 格式的数据体
xml body;
body.get_root().add_child("root", true)
.add_child("content_type", true)
.add_attr("type", (int) req.getRequestType())
.get_parent()
.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")
.get_parent()
.add_child("param", true)
.add_attr("name3", param3_ ? param3_ : "null")
.get_parent()
.get_parent()
.add_child("files", true)
.add_child("file", true)
.add_attr("filename", file1_ ? file1_ : "null")
.get_parent()
.add_child("file", true)
.add_attr("filename", file2_ ? file2_ : "null")
.get_parent()
.add_child("file", true)
.add_attr("filename", file3_ ? file3_ : "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;
}
示例7: show_product
void ProductDetails::show_product(HttpServletRequest& request, HttpServletResponse &response) {
string id = request.getParameter("product_id");
if(id.empty())
throw runtime_error("ProductDetails: product_id parameter not supplyed");
Product product = fetch(id);
request.setAttribute("product", product);
request.getRequestDispatcher("ProductDetailsView.csp")->forward(request,response);
}
示例8: doGet
void ProductDetails::doGet(HttpServletRequest& request, HttpServletResponse &response) {
try{
request.getSession(true); //start session
show_product(request, response);
}catch(const exception& ex) {
request.setAttribute<string>("error",ex.what());
request.setAttribute("product",Product());
request.getRequestDispatcher("ProductDetailsView.csp")->forward(request,response);
}
}
示例9: doResponse
bool doResponse(HttpServletRequest& req, HttpServletResponse& res)
{
// 获得浏览器传来的 cookie 值
const char* cookie1 = req.getCookieValue("name1");
const char* cookie2 = req.getCookieValue("name2");
// 创建 xml 格式的数据体
xml body;
body.get_root().add_child("root", true)
.add_child("content_type", true)
.add_attr("type", (int) req.getRequestType())
.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")
.get_parent()
.add_child("param", true)
.add_attr("name3", param3_ ? param3_ : "null")
.get_parent()
.get_parent()
.add_child("files", true)
.add_child("file", true)
.add_attr("filename", file1_ ? file1_ : "null")
.get_parent()
.add_child("file", true)
.add_attr("filename", file2_ ? file2_ : "null")
.get_parent()
.add_child("file", true)
.add_attr("filename", file3_ ? file3_ : "null");
string buf("<?xml version=\"1.0\"?>");
body.build_xml(buf);
//printf(">>>response: %s\r\n", buf.c_str());
//res.setContentLength(buf.length());
// 不必显示工调用下面过程来发送 http 响应头
//if (res.sendHeader() == false)
// return false;
// 发送 http 响应体,当使用 chunk 传输时,必须最后调用一次发送空数据
if (res.write(buf) == false || res.write(NULL, 0) == false)
return false;
return true;
}
示例10: doPost
void ProductDetails::doPost(HttpServletRequest& request, HttpServletResponse &response) {
try{
try{
add_to_cart(request, response);
}catch(const exception& ex) {
request.setAttribute<string>("error",ex.what());
show_product(request, response);
}
}catch(const exception& ex) {
request.setAttribute<string>("error",ex.what());
request.setAttribute("product",Product());
request.getRequestDispatcher("ProductDetailsView.csp")->forward(request,response);
}
}
示例11: make_naviLink
string Products::make_naviLink(HttpServletRequest& request) {
auto_ptr< vector<string> > names_ptr = request.getParameterNames();
vector<string>& names = *names_ptr;
string link;
for(unsigned i=0;i<names.size(); ++i) {
if(names[i] == "productsPage")
continue;
link += link.empty()?'?':'&';
link += names[i] + "=" + request.getParameter(names[i]);
}
link += (link.empty()?'?':'&') + string("productsPage=");
return ("Products.sxx"+link);
}
示例12: add_to_cart
void ProductDetails::add_to_cart(HttpServletRequest& request, HttpServletResponse &response) {
HttpSession* session = request.getSession(false);
if(!session) throw runtime_error("no session");
typedef vector<cart_item> shopping_cart;
shopping_cart cart = session->getAttribute<shopping_cart>("cart");
cart_item item;
item.quantity = boost::lexical_cast<int>(request.getParameter("quantity"));
item.product = fetch(request.getParameter("product_id"));
if(item.quantity <= 0)
throw runtime_error("Quantity is invalid. It must be a positive number");
cart.push_back(item);
session->setAttribute("cart",cart);
response.sendRedirect("ShoppingCart.sxx");
}
示例13: 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);
}
示例14: DoTrace
void HttpServlet::DoTrace(HttpServletRequest& request, HttpServletResponse& response)
{
std::string CRLF = "\r\n";
std::string responseString = "TRACE " + request.GetRequestUri() +
" " + request.GetProtocol();
std::vector<std::string> reqHeaders = request.GetHeaderNames();
for (std::size_t i = 0; i < reqHeaders.size(); ++i)
{
responseString += CRLF + reqHeaders[i] + ": " +
request.GetHeader(reqHeaders[i]);
}
responseString += CRLF;
response.SetContentType("message/http");
response.SetContentLength(responseString.size());
response.GetOutputStream() << responseString << std::flush;
return;
}
示例15: DoPut
void HttpServlet::DoPut(HttpServletRequest& request, HttpServletResponse& response)
{
std::string protocol = request.GetProtocol();
std::string msg = "Http PUT method not supported";
if (protocol.size() > 2 && protocol.substr(protocol.size()-3) == "1.1")
{
response.SendError(HttpServletResponse::SC_METHOD_NOT_ALLOWED, msg);
}
else
{
response.SendError(HttpServletResponse::SC_BAD_REQUEST, msg);
}
}