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

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

servlet

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

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

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

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

生命周期public class TestLiftCycleServlet extends HttpServlet { /** * 實(shí)例化 */ public TestLiftCycleServlet() { System.out.session-Cookie-"+i,"Cookie-Value_S"+i); resp.addCookie(cookie); cookie= new Cookie("Persistent-Cookie-"+i, " hello"); cookie.setMaxAge(3600);//時(shí)效性 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),每個(gè)窗口有獨(dú)一無二的sessionId.session就是服務(wù)端的一個(gè)內(nèi)存,能存儲(chǔ)任何內(nèi)容。鍵值對 * session是針對窗口的。不像cookie擁有路徑訪問問題,同一個(gè)application下的servlet/jsp可以共享一個(gè)session。需要同一個(gè)客戶端窗口 * * 如果瀏覽器支持cookie,創(chuàng)建session時(shí)會(huì)把sessionId保存在cookie里,保持不變。否則sessionId每次都說新生成的 * * session實(shí)現(xiàn): cookie實(shí)現(xiàn)、url重寫實(shí)現(xiàn) * * * cookie(臨時(shí)的,寫在文件里)實(shí)現(xiàn):sessionId存在臨時(shí)cookie里 * 使用url重寫的方式實(shí)現(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)建一個(gè)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() //最近一次訪問的時(shí)間,session是有過期時(shí)間的 +",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()); //添加了一個(gè)刷新,瀏覽器不使用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ā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品久久久久久久午夜片 | 亚洲成人免费视频在线 | 日本在线免费观看 | 久久毛片免费观看 | 久久久久久久网站 | 久久精品一区二区三区不卡牛牛 | 欧美黄色一级片视频 | 免费一级a毛片在线播放视 日日草夜夜操 | 在线a免费观看 | 国产精品久久久久久久久久久久久久久久 | 成人综合免费视频 | 99sesese| 91av99 | 九九精品影院 | 久久精品视频亚洲 | 中日韩乱码一二新区 | 欧美一级在线免费 | 久久久久久三区 | 精品国产乱码久久久久久丨区2区 | 精品国产一区二区三区在线观看 | 亚洲第一色片 | 中韩毛片 | 国产成人小视频在线观看 | 国产91久久精品一区二区 | 久草手机在线 | 久久探花 | 九九热九九热 | 久久爽精品区穿丝袜 | 久久免费观看一级毛片 | 久久99精品久久久久久青青日本 | 欧美成人性色 | 4p一女两男做爰在线观看 | 欧美一级高清免费 | 中文字幕在线观看成人 | 久久精品亚洲一区 | 91成| 一级毛片电影院 | 久久久久久久一区 | chinesexxxx刘婷hd 国产资源视频在线观看 | 噜噜噜躁狠狠躁狠狠精品视频 | 国产精品久久久久久久不卡 |