国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

servlet

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

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

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

先記錄下功能代碼吧,關于servlet的原理,等之后學習更深入了,再補充一下。

生命周期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); } //設置網(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); }}關于Session/** * * session是存在服務器端的,cookie是存在客戶端的。 * session可與瀏覽器關聯(lián),每個窗口有獨一無二的sessionId.session就是服務端的一個內(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 { //獲取當前的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(); //設置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ā)表
主站蜘蛛池模板: 昌江| 平邑县| 新泰市| 重庆市| 韩城市| 霍州市| 赤峰市| 滕州市| 德化县| 灵石县| 绵竹市| 北票市| 施甸县| 土默特左旗| 綦江县| 延边| 麦盖提县| 景东| 神池县| 乌兰浩特市| 信丰县| 桓台县| 宁国市| 旌德县| 喀什市| 区。| 麻江县| 南陵县| 安龙县| 阿图什市| 清流县| 宁远县| 彩票| 乌兰浩特市| 济宁市| 信丰县| 江津市| 南宫市| 红原县| 青阳县| 会昌县|