xzc520

           

          2006年10月18日

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

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



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

          springMVC

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

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

          2006年10月17日

          junit in eclipse

          1.介紹一下junit
          ???junit是一個用來單元測試的工具,它可以針對一個/多個類的單個或多個方法進行測試,還可以自動化套件測試.將junit.jar包從www.junit.org.載下來放到eclipse 項目中的java build path中.
          2.創建一個TestCase
          ???File > New > JUnit Test Case 或者點擊"newTestCase.gif"來創建一個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) {
          ????????? }
          ???? }
          }
          這個例子有兩個方法需要測試,第一個方法測試list中沒有任何對象,第二個方法測試沒有使用斷言,它一定會成功

          3.創建一個TestSuite
          ???通過測試套件可以運行多個測試用例
          ?? (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 嫁蛙 閱讀(204) | 評論 (0)編輯 收藏

          2006年10月16日

          Spring概念理解

          Inversion
          of Control Containers(IOC)由容器來控制程序間的關系,而非程序來控制,就是所謂的容器控制反轉。
          Dependency Injection 容器動態的將某種動態關系注入到主鍵當中。

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

          Type2 設值注入? 它由容器通過Setter方法完成依賴關系的設置
          Type3構造了注入?
          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
          ???? 由容器將依賴關系注入到組件當中,在運行期間由spring根據配置文件,將其它對象的引用通過組件提供的setter方法進行設定。運行期間動態生成對象并設定它對依賴關系.
          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
          ? 創建并且維護Bean實例它可以配置
          ??????1.? Bean的屬性值及其依賴關系(即對其它Bean的引用)
          ??????2.? Bean的創建模式(是否單例模式)
          ??????3.? Bean的初始化和銷毀方法
          ??????4.? Bean的依賴關系

          ApplicationContext
          ??????覆蓋了BeanFactory所有的方法,而且提供了新的功能。它有以下擴展功能
          ???1.國際化的支持
          ???2.資源訪問
          ???3.事件傳播
          ???4.多實例加載
          ??? spring提供了可配置的ApplicationContext加載機制。加載器有兩種選擇:ContextLoaderListener或ContextLoaderServlet一個利用servlet2.3的Listener接口實現而另一個則利用Servlet接口實現。
          在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容器會自動加載WEB-INF/applicationContext.xml初始化ApplicationContext實例.如果你指定配置文件就可以通過context-param指定:
          ??????<context-param>
          ?????????<param-name>contextConfigLocation</param-name>
          ?????????<param-value>/WEB-INF/myApplicationContext.xml</param-value>
          ??????</context-param>
          配置完成后就可以通過WebApplicationContextUtils.getWebApplicationContext方法在Web應用中獲取ApplicationContext的引用.




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

          2006年10月13日

          最簡單的spring入門示例

          spring至關重要的一環就是裝配,即配置文件的編寫,接下來我按剛才實際過程中一步步簡單講解。

          首先,要在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>

          實際上,spring的配置文件可以分切到多個xml文件,我們這個簡單的示例就把它配置到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>

          ?

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

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

          為了能正確載入DispatcherServlet等配置文件,我們要在web.xml中配置一個上下文載入器ContextLoaderListener或者ContextLoaderServlet,我們這里為了兼容版本較低的Serlvet容器(實際上我采用的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>
          這樣就全部配置完畢了,當然,上面的ntx.xml是我在項目完成以后才配置完成的,這里不再多講,有bean元素的配置大家可以參考有關資料理解,很容易理解的,下面再給出完整的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>
          根據ntx.xml知道,總共有三個java文件,LoginController.java是控制器,繼承了最簡單的Controller(實際上spring有很多控制器供我們選擇),接下來是一個簡單控制器的源碼
          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層的接口以及實現,較簡單,
          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;
          ????}

          }

          好了,最后是兩個jsp文件,一個index.jsp用來顯示一個表單,輸入名字,一個showResult.jsp用來顯示結果,只貼出相關的代碼

          <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="沒有結果"/>
          ? </body>

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

          This is the Result:

          你的名字是:gavin

          ?

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

          2006年10月12日

          將函數做為數據

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

          ??? 我們可以?? var a = square(4)?? a 16
          ??? ?? ?? ?? ? var b = square????? b 就相當于函數squre,它是squre的一個引用,執行效果一樣?? ??
          ??? ?? ?? ?? ? var c = b(10) ? ? ? c 100
          ??? 2.將函數指定為一個對象屬性的用法
          ??? ?? var o? = new Object;
          ??? ?? o.square = new Function("x","return x*x;");
          ??? ?? var y = o.square(10);
          ??? 3.利用函數直接量,將它賦給數組元素;
          ????? 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.將函數做為數據
          ??? ?? 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 嫁蛙 閱讀(157) | 評論 (0)編輯 收藏

          2006年9月11日

          簡單回顧一下tomcat服務器虛擬目錄和站點的設置

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

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

          僅列出標題  

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 农安县| 宁国市| 建始县| 榕江县| 临颍县| 城口县| 射洪县| 临西县| 龙游县| 儋州市| 陆河县| 河东区| 长治县| 余江县| 淮滨县| 昆明市| 陆良县| 崇州市| 平顶山市| 泰和县| 包头市| 乌拉特后旗| 洛浦县| 宁化县| 长沙县| 虎林市| 镇宁| 莆田市| 贵州省| 建湖县| 家居| 秭归县| 金昌市| 曲沃县| 汉川市| 新宁县| 常德市| 徐闻县| 韶山市| 鄄城县| 潜江市|