常用的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); }}新聞熱點
疑難解答