// 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); }
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; }
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; }
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); } }
void TestServlet::doGet(HttpServletRequest& req, HttpServletResponse& resp) { std::ostringstream ss(""); ss << ++_counter << ") TestServlet:" << std::endl; std::cout << ss.str() << std::endl; log(ss.str()); log("TestServlet: This is a hello from TestServlet\n"); ServletOutputStream& out = resp.getOutputStream(); ss.str(""); ss << "<html><body>Servlet access count: " << _counter; out.println(ss.str()); if("" == _sessionId) { const HttpSession* pSession = req.getSession(); if(pSession) _sessionId = pSession->getId(); } out.println("<br> Servlet Name:" + getServletName()); out.println("<br> Session Id:" + _sessionId); out.println("<hr>"); std::vector<std::string> names = getInitParameterNames(); std::vector<std::string>::iterator it = names.begin(); out.println("<ul><b>Servlet Init Parameters:</b><br>"); for(; it != names.end(); ++it) { ss.str(""); ss << "<li><b>" << *it << "</b>: " << getInitParameter(*it) << "<br>"; out.println(ss.str()); } out.println("</ul>"); names = req.getParameterNames(); out.println("<ul><b>Request Parameters:</b><br>"); it = names.begin(); for(; it != names.end(); ++it) { ss.str(""); ss << "<li><b>" << *it << "</b>:" << req.getParameter(*it) << "<br>"; out.println(ss.str()); } out.println("</ul>"); out.println("</body></html>"); }
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"); }
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; }
void DefaultTestServlet::doGet(HttpServletRequest& req, HttpServletResponse& resp) { std::ostringstream ss(""); ss << ++_counter << ") TestServlet:" << std::endl; log(ss.str()); log("TestServlet: This is a log entry from DefaultTestServlet\n"); ServletOutputStream& out = resp.getOutputStream(); ss.str(""); std::string sessionId = req.getSession()->getId(); ss << "<html><body>Hello world #" << _counter << "<br>from servlet [" << this->getServletName() << "]<br>Session ID: [" << sessionId << ']'; out.println(ss.str()); std::vector<std::string> names = getInitParameterNames(); std::vector<std::string>::iterator it = names.begin(); out.println("<ul><b>Servlet Init Parameters:</b><br>"); for(; it != names.end(); ++it) { ss.str(""); ss << "<li><b>" << *it << "</b>: " << getInitParameter(*it) << "<br>"; out.println(ss.str()); } out.println("</ul>"); names = req.getParameterNames(); out.println("<ul><b>Request Parameters:</b><br>"); it = names.begin(); for(; it != names.end(); ++it) { ss.str(""); ss << "<li><b>" << *it << "</b>:" << req.getParameter(*it) << "<br>"; out.println(ss.str()); } out.println("</ul>"); out.println("</body></html>"); }
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; }