xzc520

           

          2006年9月11日

          tomcat 中 web.xml配置描述符的用法

          ?display-name元素提供GUI工具可能會(huì)用來標(biāo)記這個(gè)特定的Web應(yīng)用的一個(gè)名稱
          ?description元素給出與此有關(guān)的說明性文本
          ?context-param元素聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。
          ?filter?過濾器元素將一個(gè)名字與一個(gè)實(shí)現(xiàn)javax.servlet.Filter接口的類相關(guān)聯(lián)
          ?filter-mapping?命名一個(gè)過濾器后,就要利用filter-mapping元素把它與一個(gè)或多個(gè)servlet或JSP頁面相關(guān)聯(lián)。
          ?listener?servlet?API的版本2.3增加了對(duì)事件監(jiān)聽程序的支持,事件監(jiān)聽程序在建立、修改和刪除會(huì)話或????????? ?servlet環(huán)境時(shí)得到通知。Listener元素指出事件監(jiān)聽程序類。
          ?servlet?在向servlet或JSP頁面制定初始化參數(shù)或定制URL時(shí),必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項(xiàng)任務(wù)的。
          servlet-mapping?服務(wù)器一般為servlet提供一個(gè)缺省的URL:http://host/webAppPrefix/servlet/ServletName。但是,常常會(huì)更改這個(gè)URL,以便servlet可以訪問初始化參數(shù)或更容易地處理相對(duì)URL。在更改缺省URL時(shí),使用servlet-mapping元素。
          session-config?如果某個(gè)會(huì)話在一定時(shí)間內(nèi)未被訪問,服務(wù)器可以拋棄它以節(jié)省內(nèi)存。可通過使用HttpSession的setMaxInactiveInterval方法?明確設(shè)置單個(gè)會(huì)話對(duì)象的超時(shí)值,或者可利用session-config元素制定缺省超時(shí)值。
          mime-mapping?如果Web應(yīng)用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。
          welcome-file-list元素指示服務(wù)器在收到引用一個(gè)目錄名而不是文件名的URL時(shí),使用哪個(gè)文件。
          error-page元素使得在返回特定HTTP狀態(tài)代碼時(shí),或者特定類型的異常被拋出時(shí),能夠制定將要顯示的頁面。
          taglib元素對(duì)標(biāo)記庫描述符文件(Tag?Libraryu?Descriptor?file)指定別名。此功能使你能夠更改TLD文件的位置,而不用編輯使用這些文件的JSP頁面。
          resource-env-ref 元素聲明與資源相關(guān)的一個(gè)管理對(duì)象。
          resource-ref 元素聲明一個(gè)資源工廠使用的外部資源。
          security-constraint 元素制定應(yīng)該保護(hù)的URL。它與login-config元素聯(lián)合使用
          login-config元素來指定服務(wù)器應(yīng)該怎樣給試圖訪問受保護(hù)頁面的用戶授權(quán)。它與sercurity-constraint元素聯(lián)合使用。
          security-role 元素給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。分別地聲明角色可使高級(jí)IDE處理安全信息更為容易。
          env-entry 元素聲明Web應(yīng)用的環(huán)境項(xiàng)。
          ejb-ref 元素聲明一個(gè)EJB的主目錄的引用。
          ejb-local-ref 元素聲明一個(gè)EJB的本地主目錄的應(yīng)用。



          posted @ 2006-10-18 10:42 嫁蛙 閱讀(396) | 評(píng)論 (0)編輯 收藏

          springMVC

          一般的MVC框架處理的問題包括
          1.將web頁面中的輸入元素封裝成為一個(gè)(請(qǐng)求)數(shù)據(jù)對(duì)象。
          2.根據(jù)請(qǐng)求不同調(diào)用相應(yīng)的邏輯處理單元,并將(請(qǐng)求)數(shù)據(jù)對(duì)象作為參數(shù)傳入。
          3.邏輯處理單元完成運(yùn)算后,返回一個(gè)結(jié)果數(shù)據(jù)對(duì)象。
          4.將結(jié)果數(shù)據(jù)對(duì)象中的數(shù)據(jù)與預(yù)先設(shè)計(jì)好的表現(xiàn)層相融合并展現(xiàn)給用戶。

          posted @ 2006-10-18 00:06 嫁蛙 閱讀(160) | 評(píng)論 (0)編輯 收藏

          junit in eclipse

          1.介紹一下junit
          ???junit是一個(gè)用來單元測(cè)試的工具,它可以針對(duì)一個(gè)/多個(gè)類的單個(gè)或多個(gè)方法進(jìn)行測(cè)試,還可以自動(dòng)化套件測(cè)試.將junit.jar包從www.junit.org.載下來放到eclipse 項(xiàng)目中的java build path中.
          2.創(chuàng)建一個(gè)TestCase
          ???File > New > JUnit Test Case 或者點(diǎn)擊"newTestCase.gif"來創(chuàng)建一個(gè)TestCase

          ???代碼如下
          ???import junit.framework.TestCase;
          ???public class SampleTest extends TestCase {
          ???? private java.util.List emptyList;
          ???? /**
          ????? * Sets up the test fixture.
          ????? * (Called before every test case method.)
          ????? */
          ???? protected void setUp() {
          ????????? emptyList = new java.util.ArrayList();
          ???? }?
          ???? /**
          ????? * Tears down the test fixture.
          ????? * (Called after every test case method.)
          ????? */
          ???? protected void tearDown() {
          ????????? emptyList = null;
          ???? }?
          ???? public void testSomeBehavior() {
          ????????? assertEquals("Empty list should have 0 elements", 0, emptyList.size());
          ???? }?
          ???? public void testForException() {
          ????????? try {
          ?????????????? Object o = emptyList.get(0);
          ?????????????? fail("Should raise an IndexOutOfBoundsException");
          ????????? }
          ????????? catch (IndexOutOfBoundsException success) {
          ????????? }
          ???? }
          }
          這個(gè)例子有兩個(gè)方法需要測(cè)試,第一個(gè)方法測(cè)試list中沒有任何對(duì)象,第二個(gè)方法測(cè)試沒有使用斷言,它一定會(huì)成功

          3.創(chuàng)建一個(gè)TestSuite
          ???通過測(cè)試套件可以運(yùn)行多個(gè)測(cè)試用例
          ?? (3.1)選擇???File > New > Other... > Java > JUnit > JUnit Test Suite. 或者newTestCase.gifOther... > Java > JUnit > JUnit Test Suite,
          import junit.framework.Test;
          import junit.framework.TestSuite;

          ???public class AllTests {

          ????public static Test suite() {
          ????????TestSuite suite = new TestSuite("Test for com.xu.Test");
          ????????//$JUnit-BEGIN$
          ????????suite.addTestSuite(SampleTest .class);
          ????????//$JUnit-END$
          ????????return suite;
          ????}

          }

          posted @ 2006-10-17 15:56 嫁蛙 閱讀(206) | 評(píng)論 (0)編輯 收藏

          Spring概念理解

          Inversion
          of Control Containers(IOC)由容器來控制程序間的關(guān)系,而非程序來控制,就是所謂的容器控制反轉(zhuǎn)。
          Dependency Injection 容器動(dòng)態(tài)的將某種動(dòng)態(tài)關(guān)系注入到主鍵當(dāng)中。

          依賴注入的幾種實(shí)現(xiàn)
          ???type1 接口注入
          ????????????public class ClassA {
          ??????????????????private InterfaceB clzB;
          ??????????????????public doSomething() {
          ????????????????????????Ojbect obj =
          ??????????????????????????????Class.forName(Config.BImplementation).newInstance();
          ?????????????????????????clzB = (InterfaceB)obj;
          ?????????????????????????clzB.doIt()
          ??????????????????}
          ????????????……
          ????????????}
          ???我們通過配置文件動(dòng)態(tài)的加載類,然后強(qiáng)制轉(zhuǎn)換成相應(yīng)的類來實(shí)現(xiàn)動(dòng)態(tài)注入,對(duì)于IOC容器來說加載類的過程由容器來完成。
          web容器采用type1 接口注入的具體應(yīng)用
          public class MyServlet extends HttpServlet {
          public void doGet(
          ?????????HttpServletRequest request,
          ?????????HttpServletResponse response)
          ????????????throws ServletException, IOException {
          ??????……
          ???}
          }

          Type2 設(shè)值注入? 它由容器通過Setter方法完成依賴關(guān)系的設(shè)置
          Type3構(gòu)造了注入?
          public class DIByConstructor {
          ???private final DataSource dataSource;
          ???private final String message;
          ???public DIByConstructor(DataSource ds, String msg) {
          ??????this.dataSource = ds;
          ??????this.message = msg;
          ???}
          ???……
          }

          Bean Wrapper
          ???? 由容器將依賴關(guān)系注入到組件當(dāng)中,在運(yùn)行期間由spring根據(jù)配置文件,將其它對(duì)象的引用通過組件提供的setter方法進(jìn)行設(shè)定。運(yùn)行期間動(dòng)態(tài)生成對(duì)象并設(shè)定它對(duì)依賴關(guān)系.
          Object obj = Class.forName("net.xiaxin.beans.User").newInstance();
          BeanWrapper bw = new BeanWrapperImpl(obj);
          bw.setPropertyValue("name", "Erica");
          System.out.println("User name=>"+bw.getPropertyValue("name"));

          Bean Factory
          ? 創(chuàng)建并且維護(hù)Bean實(shí)例它可以配置
          ??????1.? Bean的屬性值及其依賴關(guān)系(即對(duì)其它Bean的引用)
          ??????2.? Bean的創(chuàng)建模式(是否單例模式)
          ??????3.? Bean的初始化和銷毀方法
          ??????4.? Bean的依賴關(guān)系

          ApplicationContext
          ??????覆蓋了BeanFactory所有的方法,而且提供了新的功能。它有以下擴(kuò)展功能
          ???1.國(guó)際化的支持
          ???2.資源訪問
          ???3.事件傳播
          ???4.多實(shí)例加載
          ??? spring提供了可配置的ApplicationContext加載機(jī)制。加載器有兩種選擇:ContextLoaderListener或ContextLoaderServlet一個(gè)利用servlet2.3的Listener接口實(shí)現(xiàn)而另一個(gè)則利用Servlet接口實(shí)現(xiàn)。
          在web.xml中加入
          ????????????<listener>
          ???????????????<listener-class>
          ??????????????????org.springframework.web.context.ContextLoaderListener
          ???????????? ?</listener-class>
          ?????????? </listener>
          或者:
          ?????????<servlet>
          ????????????<servlet-name>context</servlet-name>
          ????????????<servlet-class>
          ??????????????????org.springframework.web.context.ContextLoaderServlet
          ????????????</servlet-class>
          ????????????<load-on-startup>1</load-on-startup>
          ?????????</servlet>
          Web容器會(huì)自動(dòng)加載WEB-INF/applicationContext.xml初始化ApplicationContext實(shí)例.如果你指定配置文件就可以通過context-param指定:
          ??????<context-param>
          ?????????<param-name>contextConfigLocation</param-name>
          ?????????<param-value>/WEB-INF/myApplicationContext.xml</param-value>
          ??????</context-param>
          配置完成后就可以通過WebApplicationContextUtils.getWebApplicationContext方法在Web應(yīng)用中獲取ApplicationContext的引用.




          posted @ 2006-10-16 23:50 嫁蛙 閱讀(457) | 評(píng)論 (0)編輯 收藏

          最簡(jiǎn)單的spring入門示例

          spring至關(guān)重要的一環(huán)就是裝配,即配置文件的編寫,接下來我按剛才實(shí)際過程中一步步簡(jiǎn)單講解。

          首先,要在web.xml中配置DispatcherServlet,它是作為Spring MVC的前端控制器.必須在web.xml中配置好,如下

          ?

          < servlet >?
          ?????
          <servlet-name>ntx</servlet-name>?
          ?????
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>?
          ?????
          <load-on-startup>1</load-on-startup>?
          </servlet>

          實(shí)際上,spring的配置文件可以分切到多個(gè)xml文件,我們這個(gè)簡(jiǎn)單的示例就把它配置到ntx.xml中

          ?

          ?<?xml?version="1.0"?encoding="UTF-8"?>?
          <!DOCTYPE?beans?PUBLIC
          ????"-//SPRING//DTD?BEAN//EN"
          ????"http://www.springframework.org/dtd/spring-beans.dtd"
          >?
          ?
          <beans
          ??
          default-autowire="no"?
          ??default-lazy-init
          ="false"?
          ??default-dependency-check
          ="none"?
          >?
          ?
          ????
          <bean?id="loginService"?class="ntx.service.serviceimpl.LoginServiceImpl"/>?
          ????????
          ????
          <bean??id="loginController"?class="ntx.controller.LoginController">?
          ????????
          <property?name="loginService">?
          ????????????
          <ref?bean="loginService"/>?
          ????????
          </property>?
          ????????
          <property?name="gotoUrl">?
          ????????????
          <value>/showResult.jsp</value>?
          ????????
          </property>?
          ????
          </bean>?
          ????
          ????
          <bean?id="SimpleUrlHandlerMapping"?class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">?
          ????????
          <property?name="mappings">?
          ????????????
          <props>?
          ????????????????
          <prop?key="/userLogin.do">loginController</prop>?
          ????????????
          </props>?
          ????????
          </property>?
          ????
          </bean>?
          </beans>

          ?

          配置好上面的這些后,要在WEB-INF下要建立ntx-servlet.xml如下:

          ?

          ?<?xml?version="1.0"?encoding="UTF-8"?>?
          <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">?
          <beans>?
          ????
          <bean?id="viewResolver"?class="org.springframework.web.servlet.view.InternalResourceViewResolver">?
          ????????
          <property?name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>?
          ????????
          <property?name="prefix"><value></value></property>?
          ????????
          <property?name="suffix"><value></value></property>?
          ????????
          ????
          </bean>?
          </beans>

          ?

          接下來,要指明哪些請(qǐng)求將交給spring的DispatcherServlet來處理,所以在web.xml中添加<servlet-mapping>

          ?<servlet-mapping>?
          ????????
          <servlet-name>ntx</servlet-name>?
          ????????
          <url-pattern>*.do</url-pattern>?
          </servlet-mapping>

          為了能正確載入DispatcherServlet等配置文件,我們要在web.xml中配置一個(gè)上下文載入器ContextLoaderListener或者ContextLoaderServlet,我們這里為了兼容版本較低的Serlvet容器(實(shí)際上我采用的2.4),采用第二種:

          ?

          ?<servlet>?
          ?????
          <servlet-name>context</servlet-name>?
          ?????
          <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>?
          ?????
          <load-on-startup>100</load-on-startup>?
          </servlet>
          這樣就全部配置完畢了,當(dāng)然,上面的ntx.xml是我在項(xiàng)目完成以后才配置完成的,這里不再多講,有bean元素的配置大家可以參考有關(guān)資料理解,很容易理解的,下面再給出完整的web.xml配置以及java
          <?xml?version="1.0"?encoding="UTF-8"?>
          <web-app?version="2.4"?
          ????xmlns
          ="http://java.sun.com/xml/ns/j2ee"?
          ????xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"?
          ????xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee?
          ????http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          >

          ???
          <context-param>
          ???????
          <param-name>contextConfigLocation</param-name>
          ???????
          <param-value>/WEB-INF/ntx.xml</param-value>
          ???
          </context-param>
          ???
          <servlet>
          ?????
          <servlet-name>ntx</servlet-name>
          ?????
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          ?????
          <load-on-startup>1</load-on-startup>
          ???
          </servlet>
          ???
          <servlet>
          ?????
          <servlet-name>context</servlet-name>
          ?????
          <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
          ?????
          <load-on-startup>100</load-on-startup>
          ???
          </servlet>
          ???
          <servlet-mapping>
          ????????
          <servlet-name>ntx</servlet-name>
          ????????
          <url-pattern>*.do</url-pattern>
          ????
          </servlet-mapping>
          ????
          <welcome-file-list>
          ????????
          <welcome-file>index.jsp</welcome-file>
          ????
          </welcome-file-list>
          </web-app>
          根據(jù)ntx.xml知道,總共有三個(gè)java文件,LoginController.java是控制器,繼承了最簡(jiǎn)單的Controller(實(shí)際上spring有很多控制器供我們選擇),接下來是一個(gè)簡(jiǎn)單控制器的源碼
          package?ntx.controller;

          import?javax.servlet.http.HttpServletRequest;
          import?javax.servlet.http.HttpServletResponse;

          import?org.springframework.web.servlet.ModelAndView;
          import?org.springframework.web.servlet.mvc.Controller;

          import?ntx.service.LoginService;
          public?class?LoginController?implements?Controller{

          ????
          private?LoginService?loginService?;
          ????
          private?String?gotoUrl;
          ????
          public?ModelAndView?handleRequest(HttpServletRequest?request,HttpServletResponse?response)?throws?Exception?{
          ????????String?userName?
          =?request.getParameter("userName");
          ????????
          this.getUserInfo(request,?userName);
          ????????
          ????????
          return?new?ModelAndView(this.getGotoUrl());
          ????}

          ????
          ????
          private?void?getUserInfo(HttpServletRequest?request,String?userName){
          ????????String?userInfo?
          =?loginService.getUserInfo(userName);
          ????????request.setAttribute(
          "userInfo",?userInfo);
          ????}

          ????
          ????
          public?String?getGotoUrl()?{
          ????????
          return?gotoUrl;
          ????}

          ????
          public?void?setGotoUrl(String?gotoUrl)?{
          ????????
          this.gotoUrl?=?gotoUrl;
          ????}

          ????
          public?LoginService?getLoginService()?{
          ????????
          return?loginService;
          ????}

          ????
          public?void?setLoginService(LoginService?loginService)?{
          ????????
          this.loginService?=?loginService;
          ????}

          ????
          }

          還有service層的接口以及實(shí)現(xiàn),較簡(jiǎn)單,
          package?ntx.service;
          public?interface?LoginService?{

          ????
          public?String?getUserInfo(String?userName);
          }
          package?ntx.service.serviceimpl;

          import?ntx.service.LoginService;

          public?class?LoginServiceImpl?implements?LoginService?{
          ????
          public?String?getUserInfo(String?userName){
          ????????
          ????????
          return?"你的名字是:"?+?userName;
          ????}

          }

          好了,最后是兩個(gè)jsp文件,一個(gè)index.jsp用來顯示一個(gè)表單,輸入名字,一個(gè)showResult.jsp用來顯示結(jié)果,只貼出相關(guān)的代碼

          <body>
          ??? This is my Test Spring page. <br>
          ??? <div>
          ??? ?<form method="post" action="/userLogin.do">
          ???? ?<input type="text" name="userName" size="30"/><br/>
          ???? ?<input type="submit" value="提交"/>
          ???? </form>
          ??? </div>
          ? </body>

          <body>
          ??? This is the Result: <br>
          ?<c:out value="${userInfo}" default="沒有結(jié)果"/>
          ? </body>

          發(fā)布到tomcat或者其它Servlet容器可以正常使用,提交以后將顯示:

          This is the Result:

          你的名字是:gavin

          ?

          posted @ 2006-10-13 10:28 嫁蛙 閱讀(1134) | 評(píng)論 (1)編輯 收藏

          將函數(shù)做為數(shù)據(jù)

          ??? 1.用變量引用函數(shù),該變量執(zhí)行和函數(shù)一樣的效果
          ??
          ??? 函數(shù)定義以后? 如 function square(x) { return x*x;}

          ??? 我們可以?? var a = square(4)?? a 16
          ??? ?? ?? ?? ? var b = square????? b 就相當(dāng)于函數(shù)squre,它是squre的一個(gè)引用,執(zhí)行效果一樣?? ??
          ??? ?? ?? ?? ? var c = b(10) ? ? ? c 100
          ??? 2.將函數(shù)指定為一個(gè)對(duì)象屬性的用法
          ??? ?? var o? = new Object;
          ??? ?? o.square = new Function("x","return x*x;");
          ??? ?? var y = o.square(10);
          ??? 3.利用函數(shù)直接量,將它賦給數(shù)組元素;
          ????? var a? = new Array(3);
          ??? ??? a[0] = function(x){return x*x;};
          ??? ??? a[1] = 10;
          ??? ??? a[2] = a[0](a[1]);
          ??? ??? alert("a[2] = "+a[2]);
          ??? 4.將函數(shù)做為數(shù)據(jù)
          ??? ?? function add(x,y){ return x+y};
          ??? ?? function subtract(x,y){return x-y};
          ??? ?? function multiply(x,y){return x*y};
          ?????? function divide(x,y){return x/y};
          ?????? function operate(operator,operand1,operand2){
          ??? ?? ?? ? return operator(operand1,operand2);
          ??? ??? }
          ??? ?? var i = operate(add,operate(add,2,3),subtract(10,8));
          ??? ?? alert(i);

          ??? ??
          ???
          ??? ?? ? ?? ??? ?

          posted @ 2006-10-12 23:15 嫁蛙 閱讀(159) | 評(píng)論 (0)編輯 收藏

          簡(jiǎn)單回顧一下tomcat服務(wù)器虛擬目錄和站點(diǎn)的設(shè)置

          簡(jiǎn)單的設(shè)置一個(gè)站點(diǎn)
          在conf文件來的server.xml中的<host></host>標(biāo)簽中加入
          ?<Context docBase="E:\eclipse\workspace\chinamaga\WebContent" path="" debug="0"/>
          指到相應(yīng)的文件就可以, 這樣http://localhost/就可以訪問資源了.如果是虛擬目錄將path="xxx"就可以了.

          posted @ 2006-09-11 00:03 嫁蛙 閱讀(260) | 評(píng)論 (0)編輯 收藏

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 鹤庆县| 阳城县| 巴东县| 广平县| 周至县| 嘉兴市| 垫江县| 黎川县| 遂宁市| 苗栗县| 高要市| 电白县| 那坡县| 武功县| 永仁县| 商洛市| 高尔夫| 德钦县| 河南省| 东港市| 丰宁| 武威市| 特克斯县| 台北市| 郑州市| 巨鹿县| 永兴县| 三原县| 如皋市| 庆阳市| 安陆市| 景东| 荆州市| 墨竹工卡县| 轮台县| 金溪县| 电白县| 项城市| 淳化县| 康乐县| 福海县|