麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

servlet

2019-11-11 05:44:02
字體:
供稿:網(wǎng)友

常用的web服務(wù)器有: 1、IIS,出自微軟 2、Apache,linux,C語言寫的 3、tomcat是Apache項目中的一個,java寫的

servlet是服務(wù)器上運行的程序其主要功能在于交互式地瀏覽和修改數(shù)據(jù),生成動態(tài)Web內(nèi)容。

先記錄下功能代碼吧,關(guān)于servlet的原理,等之后學(xué)習(xí)更深入了,再補充一下。

生命周期public class TestLiftCycleServlet extends HttpServlet { /** * 實例化 */ public TestLiftCycleServlet() { System.out.session-Cookie-"+i,"Cookie-Value_S"+i); resp.addCookie(cookie); cookie= new Cookie("Persistent-Cookie-"+i, " hello"); cookie.setMaxAge(3600);//時效性 resp.addCookie(cookie); } //設(shè)置網(wǎng)頁的字符編碼格式為gb2312 resp.setContentType("text/html;charset=gb2312"); PrintWriter pw = resp.getWriter(); //2 獲取cookie pw.println("獲取到cookie"); Cookie[] cookies = req.getCookies(); if(cookies!=null){ Cookie cookie = null; for(int i = 0; i < cookies.length; i++){ cookie= cookies[i]; pw.println("name:"+cookie.getName()+" value:"+cookie.getValue()); } } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("post"); doGet(req, resp); }}關(guān)于Session/** * * session是存在服務(wù)器端的,cookie是存在客戶端的。 * session可與瀏覽器關(guān)聯(lián),每個窗口有獨一無二的sessionId.session就是服務(wù)端的一個內(nèi)存,能存儲任何內(nèi)容。鍵值對 * session是針對窗口的。不像cookie擁有路徑訪問問題,同一個application下的servlet/jsp可以共享一個session。需要同一個客戶端窗口 * * 如果瀏覽器支持cookie,創(chuàng)建session時會把sessionId保存在cookie里,保持不變。否則sessionId每次都說新生成的 * * session實現(xiàn): cookie實現(xiàn)、url重寫實現(xiàn) * * * cookie(臨時的,寫在文件里)實現(xiàn):sessionId存在臨時cookie里 * 使用url重寫的方式實現(xiàn)session,response。encodeURL(),轉(zhuǎn)碼,url后面添加sessionId * *http://blog.csdn.net/robbyo/article/details/17733743 */public class SessionDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //獲取當(dāng)前的session,若無,就創(chuàng)建一個session HttpSession mySession = req.getSession(true); resp.setContentType("text/html"); PrintWriter out= resp.getWriter(); String heading; Integer accessCount = (Integer)mySession.getAttribute("accessCount"); if(accessCount==null){ accessCount = new Integer(0); heading="welcome,new comer"; }else{ heading="welcome back"; accessCount = new Integer(accessCount.intValue()+1); } mySession.setAttribute("accessCount", accessCount); out.println("<H3>show info saved in session</H3>"+accessCount); out.println("<H3>create Information</H3>"); out.println(" new Session:"+mySession.isNew()); out.println("session id:"+mySession.getId()+ ",create time:"+mySession.getCreationTime() //最近一次訪問的時間,session是有過期時間的 +",last access time:"+mySession.getLastAccessedTime()); out.println("<H3>Request Information</H3>"); out.println("Session ID from Request: " //上次使用的session + req.getRequestedSessionId()); out.println("<BR>Session ID via Cookie: " //方式1:通過cookie + req.isRequestedSessionIdFromCookie()); out.println("<BR>Session ID via rewritten URL: " //方式2:通過重寫url + req.isRequestedSessionIdFromURL()); out.println("<BR>Valid Session ID: " //session是否有效 + req.isRequestedSessionIdValid()); //添加了一個刷新,瀏覽器不使用cookie,就在url里面添加session out.println("<a href="+"SessionDemo"+">simple url </a>"); //經(jīng)測試,360不行,Chrome可以.response.encodeURL的功能是url后添加session。 String urlWithSession = resp.encodeURL("SessionDemo"); out.println("<a href='"+urlWithSession+"'>url with session</a>"); out.close(); // close output stream } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("post"); doGet(req, resp); }}application/** * application里面的值是共享的 * @author dell * */public class TestServletContext extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=gb2312"); PrintWriter out = resp.getWriter(); //獲取application ServletContext application = this.getServletContext(); //設(shè)置application里的內(nèi)容 Integer accessCount = (Integer)application.getAttribute("accessCount"); if(accessCount==null){ accessCount = new Integer(0); }else{ accessCount = new Integer(accessCount.intValue()+1); } application.setAttribute("accessCount", accessCount); //獲取application里的內(nèi)容 out.println("get count:"+accessCount); }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 91成人影库 | 国产羞羞网站 | 欧美成人精品欧美一级 | 免费一级在线观看 | 孕妇体内谢精满日本电影 | 91九色蝌蚪在线 | 欧美日韩在线播放 | 蜜桃成品人免费视频 | 日本在线免费观看 | 一级黄色影院 | 一级片免费在线 | 日韩在线播放中文字幕 | 免费欧美一级视频 | 悠悠成人资源亚洲一区二区 | 黄色免费电影网址 | 欧美日韩精品不卡一区二区三区 | 国产精品久久久久久久午夜片 | 极色品影院 | 久久国产精品系列 | 国产精品久久久久网站 | 中文字幕观看 | 国产亚洲精品久久久久5区 综合激情网 | 久久精品国产清自在天天线 | 国产精品成人一区二区三区吃奶 | 国产免费专区 | 国产毛片自拍 | 在线免费91 | 美女视频黄a视频免费全过程 | 日本成人一区二区 | 欧洲成人综合网 | 欧美国产成人在线 | 狠狠干导航 | 久久一本日日摸夜夜添 | 亚洲骚图 | 精品亚洲夜色av98在线观看 | 牛牛a级毛片在线播放 | 日韩视频一二三 | av噜噜噜噜 | 欧美日韩免费一区 | 伦一区二区三区中文字幕v亚洲 | 免费久久精品 |