??C++?????????JAVA HttpServlet??????
?????????? ???????[ 2016/8/25 13:34:53 ] ??????????????????? C++ ????
????????HttpServletResponse ??
???????????????д??????????????????????????????????????????????????Щ???????????????????????????????? HttpServletResponse.hpp ??????
/**
* ???? HTTP ?????????? Content-Type ???????????????????
* text/html ?? text/html; charset=utf8 ???
* @param value {const char*} ????
*/
void setContentType(const char* value);
/**
* ???? HTTP ????????????????????????? setContentType ????
* ????????????????????????????????
* @param charset {const char*} ???????????????
*/
void setCharacterEncoding(const char* charset);
/**
* ???? HTTP ?????е?????1xx?? 2xx?? 3xx?? 4xx?? 5xx
* @param status {int} HTTP ??????? ?磺200
*/
void setStatus(int status);
/**
* ??? cookie
* @param name {const char*} cookie ??
* @param value {const char*} cookie ?
* @param domain {const char*} cookie ?洢??
* @param path {const char*} cookie ?洢·??
* @param expires {time_t} cookie ???????????????????
* ???? cookie ?????????(??)
*/
void addCookie(const char* name?? const char* value??
const char* domain = NULL?? const char* path = NULL??
time_t expires = 0);
/**
* ???? HTTP ???????????÷???????????????????? HTTP
* ??????????????
*/
bool sendHeader(void);
/**
* ??? HTTP ????????????????????????? sendHeader ????
* ?? HTTP ?????????????????????? HTTP ??????
* @return {ostream&}
*/
ostream& getOutputStream(void) const;
????setCharacterEncoding???÷??????? HTTP ?????? HTTP ?????????????????????ú??????????????????????????? html ???????????????????????????????????????????? HTTP ????????????????????????????????????
????setContentType???÷??????????? HTTP ?????е? Content-Type ??Σ????? xml ?????????? text/xml???? html ?????????? text/html???????????????? image/jpeg ??????????????????????????????÷???????????????????????????????????????????д??setContentType(“text/html; charset=utf8”)??????÷???????setContentType(“text/html”); setCharacterEncoding(“utf8”)
????setStatus?????? HTTP ???????????????????????????????????????????????
????addCookie???? HTTP ????????? cookie ?????
????sendHeader?????? HTTP ??????
????getOutputStream???ú??????????????????????????????????д HTTP ??????????壨???? ostream ?????????????????include/ostream.hpp????
????????????????????????????????????HttpSession ?????????????? session ??????????
???????HttpSession ??
????????????????????????????????? HttpServet ?????????????????????????????????????У?
????/**
????* ?????????????洢???? session ???????????????????÷???
????* @param name {const char*} session ???????
????* @return {const char*} session ???????????????????
????* ??????
????*/
????virtual const char* getAttribute(const char* name) const;
????/**
????* ???÷?????? session ???? session ??????????????÷???
????* @param name {const char*} session ???????
????* @param name {const char*} session ??????
????* @return {bool} ???? false ??????????
????*/
????virtual bool setAttribute(const char* name?? const char* value);
??????????????????????????鷽????????? HttpSession ?? session ????洢??????? memcached????????о?????????????????????????????????????洢???????????????????????????????????????? session id ?????????????????鷽????
????protected:
????/**
????* ??????? session ???? ID ??????????????÷???
????* @param buf {char*} ?洢?????????
????* @param len {size_t} buf ????????С??buf ????????С????
????* 64 ???????
????*/
????virtual void createSid(char* buf?? size_t len);
???????????????????????????????滹??????????????????????Щ????÷???
?????塢???
?????????????????? CGI ?????????????г???????????? apache ?? cgi-bin/ ???????????????????????
// http_servlet.cpp : ??????????ó????????
//
#include "lib_acl.hpp"
using namespace acl;
//////////////////////////////////////////////////////////////////////////
class http_servlet : public HttpServlet
{
public:
http_servlet(void)
{
}
~http_servlet(void)
{
}
// ?????? HTTP GET ???????????
virtual bool doGet(HttpServletRequest& req?? HttpServletResponse& res)
{
return doPost(req?? res);
}
// ?????? HTTP POST ???????????
virtual bool doPost(HttpServletRequest& req?? HttpServletResponse& res)
{
// ?????????????? session ??????????????????????????????
const char* sid = req.getSession().getAttribute("sid");
if (sid == NULL || *sid == 0)
req.getSession().setAttribute("sid"?? "xxxxxx");
// ?????θ??????????? session ??????????
sid = req.getSession().getAttribute("sid");
// ?????????????????? cookie ?
const char* cookie1 = req.getCookieValue("name1");
const char* cookie2 = req.getCookieValue("name2");
// ??????? HTTP ????
// ???? cookie
res.addCookie("name1"?? "value1");
// ???t????????????????? cookie
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 ???????????????嶠??
res.setContentLength(buf.length());
// ???? http ????
if (res.sendHeader() == false)
return false;
// ???? http ?????
if (res.write(buf) == false)
return false;
return true;
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
int main(void)
{
#ifdef WIN32
acl::acl_cpp_init(); // win32 ????????????????
#endif
http_servlet servle;
// cgi ???????
servlet.doRun("127.0.0.1:11211"); // ??????У????趨 memcached ??????????127.0.0.1:11211
// ???????????????
return 0;
}
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
??C++????????????C++ lvalue??rvalueC++11????????C++???????????????C++?е?????????????????C++?????????C++???Windows????λ??C/C++???????????????????JAVA??C??C++??????????c++??python???????????????????????????????C++???????C++?е????????C++????????????????C++ ???????????????C++?????????????????????C++????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????