JAVA及相關技術學習

          Servlet

           

          Cookie

          Cookie包含一對Key/Value
          生成一個Cookie并將它寫到用戶硬盤上的語法:
          Cookie theCookie=new Cookie("cookieName","cookieValue");
          response.addCookie(the Cookie);
          如果服務器想從用戶硬盤上獲取Cookie,可以用
          Cookie cookies[]=request.getCookies();
          獲取所有Cookie
          然后調用Cookie的getName方法獲取Cookie的Key,調用Cookie的getValue方法獲取Cookie的Value
          通過Cookie的setMaxAge(int expiry)方法設置Cookie的有效期。超過參數expity指定的時間(以秒為單位),Cookie就會失效。
          例子:
          <html>
             <head><title>jspCookie.jsp</title></head>
             <body>
                <%
                   Cookie[] cookies=request.getCookies();
                   //System.out.println("dsfasfdafda");
                   for(int i=0;i<cookies.length;i++)
                   {
                 %>
              
                 <p>
                   <b>Cookie name:</b>
                   <%=cookies[i].getName()%>
                   <b>Cookie value:</b>
                   <%=cookies[i].getValue()%>
                   </p>
                   <p>
                      <b>Old max age in seconds:</b>
                      <%=cookies[i].getMaxAge()%>
                      <%
                         cookies[i].setMaxAge(60);
                      %>
                      <b>New max age in seconds:</b>
                      <%=cookies[i].getMaxAge()%>
                 <%
                   }
                  
                 %>
                <%!
                   int count1=0;
                   int count2=0;
                 %>
                 <%
                    response.addCookie(new Cookie("cookieName"+count1++,"cookieValue"+count2++));
                  %>
             </body>
          </html>

          posted @ 2009-03-11 14:21 亂せ英豪 閱讀(368) | 評論 (0)編輯 收藏

          JSP聲明

          JSP聲明用于聲明JSP代表的Servlet類的成員變量和方法。
          語法<%! %>
          例如:
          <%! int i=0;%>
          也可聲明函數:
          <!
             public String f(int i)
             {
              if(i<3) return "i<3";
              else return "i>=3";
             }
          >
          每個聲明只在當前JSP頁面中有效,如果希望每個JSP頁面中都包含這些聲明,可以把他們寫成單獨的頁面,然后用<%@include%>指令把這個夜明加到其他頁面中。
          JAVA程序片段:
          JSP文件中,可以在<%和%>標記間直接嵌入任何有效的JAVA語言代碼。這樣嵌入的程序片段稱為Scriptlet。如果在page指令中沒有指定method屬性,則生成代碼默認service方法的主體。

          posted @ 2009-03-11 14:20 亂せ英豪 閱讀(184) | 評論 (0)編輯 收藏

          JSP學習——JSP指令

          JSP指令用來設置和整個JSP網頁相關屬性,如網頁的編碼方式和腳本語言等。
          語法:
          <%@ 指令名 屬性=“值”%>
          常見的3中指令:
          page:可以指定所使用的腳本語言、JSP代表的Servlet實現的接口、Servlet擴展的類以及導入的軟件包。
          語法:
          <%@ page 屬性1=“值1” 屬性2=“值2”%>
          例:
          <%@ page import="java.util.*"  contentType="type/html;charset=GB2312" language="java"%>
          page指令的屬性描述:
          language:指定文件中所使用的腳本語言目前僅JAVA為有效值和默認值。用作整個文件。
          語法:<%@ page language="java"%>
          method:指定JAVA程序片段所屬的方法的名稱。JAVA程序片段會成為制動方法的主體。默認的方法是service方法。當多次使用該指令時,只有第一次范圍是有效的,該方法有效值

          包括:service、doGet和doPost等。
          語法:<%@ page method="doPost"%>
          import:指定導入的JAVA軟件包名或類別列表。
          語法:<%@page import="java.io.*,java.util.Hashtable"%>
          content_type:指定響應結果的MIME類型,默認MIME類型是text/html。默認字符編碼為ISO-8859-1。
          語法:<%@ page content_type="text/html;charset=GB2312"%>
          session="true|false"
          指定JSP頁是否使用Session。默認為TRUE
          語法:<%@ page session="true"%>
          errorPage="error_url"指定當發生異常時,客戶請求被重新定向到那個網頁。
          語法:
          <%@page errorPage="errorpage.jsp"%>
          isErrorPage="true|false":表示此JSP網頁是否為處理異常的網頁。
          語法:<%@ page isErrorPage="true"%>
          include:JSP可以通過include指令來包含其他文件。被調用的可以是JSP文件、HTML文件或文本文件。
          語法:<%@inclue file="relativeURL"%>
          開發網站時,如果多數JSP網頁都含有相同的內容,可以把這部分相同的內容單獨放到一個文件中,其他的JSP文件通過include指令將這個文件包含進來,這樣做可以提高開發網站的效率,而且便于維護網頁。
          taglib:自定義JSP標簽

          posted @ 2009-03-11 14:19 亂せ英豪 閱讀(183) | 評論 (0)編輯 收藏

          ServletContext和Web應用的關系

          Servlet容器在啟動時會加載Web應用,并為每個WEB應用創建唯一的ServletContext對象。可以把ServletContext看成是一個WEB應用的服務器端組件的共享內存。在ServletContext中可以存放共享數據,它提供了4個讀取或設置共享數據的方法:
          setAttribute(String name,Object object):把一個對象和一個屬性名綁定。將這個對象存儲在ServletContext中。
          getAttribute(String name):根據給定的屬性名返回所綁定的對象;
          removeAttribute(String name):根據給定的屬性名從ServletContext中刪除相應的屬性。
          getAttribateNames():返回一個Enumeration對象。它包含了存儲在ServletContext對象中的所有屬性名。
          package mypack;

          import java.io.IOException;
          import java.io.PrintWriter;
          import java.util.*;
          import javax.servlet.*;
          import javax.servlet.http.*;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class ServletContext extends HttpServlet {

           /**
            * Constructor of the object.
            */
           private static final String CONTEXT_TYPE = "text/html";
           private Object Integer;

           public ServletContext() {
            super();
           }

           /**
            * Destruction of the servlet. <br>
            */
           public void destroy() {
            super.destroy(); // Just puts "destroy" string in log
            // Put your code here
           }

           /**
            * The doGet method of the servlet. <br>
            *
            * This method is called when a form has its tag value method equals to get.
            *
            * @param request
            *            the request send by the client to the server
            * @param response
            *            the response send by the server to the client
            * @throws ServletException
            *             if an error occurred
            * @throws IOException
            *             if an error occurred
            */
           public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {

            // response.setContentType("text/html");
            // PrintWriter out = response.getWriter();
            doPost(request, response);
           }

           /**
            * The doPost method of the servlet. <br>
            *
            * This method is called when a form has its tag value method equals to
            * post.
            *
            * @param request
            *            the request send by the client to the server
            * @param response
            *            the response send by the server to the client
            * @throws ServletException
            *             if an error occurred
            * @throws IOException
            *             if an error occurred
            */
           public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
            // 獲得Context的引用
            ServletContext context = (ServletContext) getServletContext();
            //response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            Integer count = (Integer) context.getAttribute("count");
            if (count == null) {
                         count=new Integer(0);
                         context.setAttribute("count",new Integer(0));
            }
            response.setContentType(CONTEXT_TYPE);
            out.print("<html><head><title>");
            out.print(CONTEXT_TYPE+"</title></head><body>");
            out.print("<p>This count is:"+count+"</p>");
            out.print("</body>");
            count=new Integer(count.intValue()+1);
            context.setAttribute("count",count);
           }

           /**
            * Initialization of the servlet. <br>
            *
            * @throws ServletException
            *             if an error occurs
            */
           public void init(ServletConfig config) throws ServletException {
            super.init(config);
            // Put your code here
           }

          }

          posted @ 2009-03-11 14:16 亂せ英豪 閱讀(376) | 評論 (0)編輯 收藏

          創建HttpServlet的方法

          創建自己的HttpServlet類,4個步驟:
          (1)擴展HttpServlet抽象類。
          (2)覆蓋HttpServlet的部分方法,如覆蓋doGet()或doPost方法。
          (3)獲取HTTP請求信息,例如通過HttpServletRequest對象來檢索HTML表單所提交的數據或URL上的查詢字符串。無論是HTML表單數據還是URL上的查詢字符串,在

          HttpServletRequest對象中都以參數名/參數值的形式存放,可以通過一下方法檢索參數信息:
          getParameterNames():返回一個Enumeration對象,它包含了所有的參數名信息
          getParameter(String name):返回參數名name對應的參數值
          getParameterValues():返回一個Enumeration對象,它包含了所有的參數值信息HttpServletResponse對象有一個getWriter()方法。該方法返回一個PrintWriter對象。使用

          PrintWriter的print()或println()方法可以向客戶端發送字符串數據流。
          (4)生成HTTP響應結果。通過HttpServletResponse對象可以生成響應結果。HttpServletResponse對象有一個getWriter()方法,該方法返回一個PrintWriter對象。使用

          PrintWriter的print()或println()方法可以向客戶端發送字符串數據流。
          例:
          package mypack;
          import javax.servlet.*;
          import javax.servlet.http.*;
          import java.io.*;

          public class HelloServlet extends HttpServlet//第一步
          {
          //覆蓋doGet()方法
          public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
          {
              //第三步:獲取HTTP請求中的參數信息
              String clientName=request.getParameter("clientName");
              if(clientNme!=null)
              clientName=new String(clientName.getBytes("ISO-8859-1"),"GB2312");
              else
              clientName="我的朋友";
              //第四步:生成HTTP響應結果
              PrintWriter out;
              String title="HelloServlet";
              String heading1="This is output from HelloServlet by doGet:";
              response.setContentType("text/html;charset=GB2312");/*MIME類型*/
              out=response.getWriter();
              out.print("<html><head><title>"+title+"<title>");
              out.print("</head><body>");
              out.print(heading1);
              out.print("<h1><p>"+clientName+":您好</h1>");
              out.print("</body></html>");
              out.close();    
          }
          }

          部署Servlet:
          在WEB-INF下的Web.xml里增加如下代碼:
          <web-app version="2.5"
           xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
          <servlet>
               <servlet-name>Hello</servlet-name>
               <servlet-class>mypack.Hello</servlet-class>
          </servlet>
          <servlet-mapping>
                     <servlet-name>Hello</servlet-name>
                     <url-pattern>/Hello</url-pattern>
          </servlet-mapping>
          </web>

          posted @ 2009-03-08 12:16 亂せ英豪 閱讀(1718) | 評論 (0)編輯 收藏

          HttpServlet的功能

          HttpServlet的作用是:
          根據客戶發出的HTTP請求,生成響應的HTTP響應結果。HttpServlet首先必須讀取HTTP請求的內容。Servlet容器負責創建HttpRequest對象,并把HTTP請求信息封裝到HttpRequest對

          象中,這大大簡化了HttpServlet解析請求數據的工作量。
          如果沒有HttpServletRequest,HttpServlet只能直接處理Web客戶發出的原始的字符串數據,有了HttpRequest后,只要調用HttpServletRequest的相關方法,就可以方便地讀取

          HTTP請求中任何部分信息,HttpServletRequest中讀取HTTP請求信息的常用方法如下:
          getCookies()           返回HTTP請求的Cookies
          getHeader(String name) 返回參數指定的HTTP請求的Header數據
          getRequestURI()        返回HTTP請求URI
          getQueryString()       返回HTTP請求數據中的查詢字符串
          getMethod()            返回HTTP請求方法。
          Servlet容器向HttpServlet提供了HttpServletResponse對象,HttpServlet可以通過它來生成HTTP響應的沒一個部分內容。HttpServletResponse提供的生成響應數據Header的方法

          如下:
          addCookie(Cookie cookie) 向HTTP響應中加入Cookie
          setHeader(String name,String value) 設置HTTP響應的Header,如果參數name對應的Header已經存在,則覆蓋原來的Header數據
          addHeader(String name,String value) 向HTTP響應加入Header.

          HttpServlet的service方法
          Servlet容器調用自身的方法解析HTTP請求信息。
          1:Web客戶向Servlet容器發出HTTP請求;
          2:Servlet容器解析Web客戶的HTTP請求;
          3:Servlet容器創建一個HttpRequest對象,在這個對象中封裝了HTTP請求信息;
          4:Servlet容器創建一個HttpResponse對象;
          5:Servlet容器調用HttpServlet的service方法,把HttpRequest和HttpResponse對象作為service方法的參數傳給HttpServlet對象;
          6:HttpServlet調用HttpRequest的有關方法,獲取HTTP請求信息;
          7:HttpServlet調用HttpResponse的有關方法,生成響應數據;
          8:Servlet容器把HttpServlet的響應結果傳給Web客戶。

          posted @ 2009-03-08 12:13 亂せ英豪 閱讀(1519) | 評論 (0)編輯 收藏

          HTTP與HttpServlet

          HTTP是一種基于請求/響應模式的協議。

          HTTP請求
          HTTP請求有3個部分構成,分別是:
          請求方法 URI  協議/版本
          請求頭
          請求正文
          例:
          GET/sample.jsp HTTP/1.1

          1.請求方法 URI 協議/版本
          請求的第一行是“方法URI 協議/版本”:
          GET/sample.jsp HTTP/1.1
          以上代碼中“GET”代表請求方法,“/sample.jsp”表示URI,“HTTP/1.1”代表協議和協議的版本。
          根據HTTP標準,HTTP請求可以使用多種請求方法。HTTP1.1支持7中請求方法:GET、POST、HEAD、OPTIONS、PUT、DELETE和TRACE。最常用的:POST和GET。
          URI完整地指定了要訪問的網絡資源,通常只要給出相對于服務器的根目錄大的相對目錄即可,因此總是以“/”開頭。
          協議版本生命了通信的過程中使用的HTTP的版本。
          2.請求頭
          請求頭包含許多有關客戶端環境和請求正文的有用信息。如:瀏覽器所用的語言,正文的長度等。
          Accept:image/gif,image/jpeg,*/*;
          3.請求正文
          請求頭和請求正文之間時一個空行,這個行非常重要,它表示請求頭已經結束,接下來的是請求正文。請求正文中可以包含客戶提交的查詢字符串信息:
              userName=weiqin&password=1234


          HTTP 響應
          由3個部分構成:
          協議  狀態代碼  描述
          響應頭
          響應正文
          HTTP響應例子:
          HTTP/1.1 200 OK

          1.協議 狀態代碼 描述
          HTTP 響應的第一行類似于HTTP請求的第一行,它表示通信所用的協議是HTTP1.1,服務器已經成功地處理了客戶端發出的請求(200 表示成功);
          2.響應頭
          響應頭和請求頭一樣包含許多有用的信息,例如服務器類型,日期時間、內容類型和長度等。
          Server:ApacheTomcat/5.0.12
          3.響應正文
          響應正文就是服務器返回的HTML頁面:
          <html>
            <head></head>
             ...
            <body></body>
          </html>

          posted @ 2009-03-08 12:12 亂せ英豪 閱讀(234) | 評論 (0)編輯 收藏

          Servlet的生命周期

          分為三個階段:初始化階段、響應客戶請求階段和終止階段。
          javax.servlet.Servlet接口定義了3個方法:
          init()、service()、destroy().
          初始化階段:
          在下列情況下Servlet容器裝載Servlet:
          Servlet容器啟動時自動裝在某些Servlet.
          Servlet容器啟動后,客戶首次向Servlet發出請求。
          Servlet的類文件被更新后,重新裝載Servlet.
          //Servlet容器是否在啟動時自動裝載Servlet,這是由在web.xml中為Servlet設置的<load-on-startup>屬性決定的
          Servlet被裝載后,Servlet容器創建一個Servlet實例并且調用Servlet的init()方法進行初始化。在Servlet的整個生命周期中,init方法只會被調用一次。
          重載方式:
          public void init(ServletConfig config) throws ServletException;
          public void init()throws ServletException;
          用第一個方法應該先調用super.init(config)方法確保參數config應用ServletConfig對象;
          用第二個方法可以不用調用super.init()方法,如果要在init方法中訪問ServletConfig對象,可以調用Servlet類的getServletConfig()方法。
          響應客戶請求階段
          Servlet容器創建特定于這個請求的ServletRequest對象和ServletResponse對象,然后調用Servlet的service方法從ServletRequest對象獲得客戶請求信息并處理該請求,通過

          ServletResponse對象向客戶返回響應結果。
          終止階段
          當Web應用被終止,或Servlet容器終止運行,或Servlet容器重新裝載Servlet的新實例時,Servlet容器會先調用Servlet的destroy()方法。在destroy方法中,可以釋放Servlet所

          占用的資源。

          posted @ 2009-03-08 12:12 亂せ英豪 閱讀(168) | 評論 (0)編輯 收藏

          SerletResponse接口:

          SerletResponse接口:
          getOutputStream:返回可以向客戶端發送二進制數據的輸出流對象ServletOutputStream
          getWriter:返回可以向客戶端發送字符數據的PrintWriter對象
          getCharacterEncoding:返回Servlet發送的響應數據的字符編碼。
          getContentType:返回Servlet發送的響應數據的MIME類型。
          setCharacterEncoding:設置Servlet發送的響應數據的字符編碼
          setContentType:設置Servlet發送的響應數據的MIME類型。

          posted @ 2009-03-08 12:11 亂せ英豪 閱讀(100) | 評論 (0)編輯 收藏

          ServletRequest接口:

          getAttribute 根據參數給定的屬性名返回屬性值。
          getContentType:返回客戶請求數據MIME類型。
          getInputStream:返回以二進制數方式直接讀取客戶請求數據的輸入流.
          getParameter:根據給定的參數名返回參數值。
          getRemoteAddr:返回遠程客戶主機的IP地址。
          getRemoteHost:返回遠程客戶主機名。
          getRemotePost:返回遠程主機端口。
          setAtrribute:在ServletRequest中設置屬性。

          posted @ 2009-03-08 12:10 亂せ英豪 閱讀(120) | 評論 (0)編輯 收藏

          僅列出標題
          共3頁: 上一頁 1 2 3 下一頁 

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新龙县| 枣庄市| 宁强县| 嘉义县| 宿州市| 康保县| 广安市| 新建县| 谢通门县| 漠河县| 青州市| 遂川县| 手游| 泗水县| 灵台县| 保德县| 兰考县| 京山县| 云安县| 龙陵县| 富源县| 上思县| 上蔡县| 闵行区| 北安市| 轮台县| 亚东县| 衡东县| 临江市| 台北县| 巴东县| 台南县| 西林县| 昆山市| 开远市| 柳河县| 临桂县| 雷山县| 曲阳县| 丰城市| 武宣县|