posts - 60, comments - 116, trackbacks - 1, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          單獨使用Spring建立簡易開發框架(四)

          Posted on 2007-09-11 19:11 匪客 閱讀(1461) 評論(0)  編輯  收藏 所屬分類: 開發技術

          5. web.xml 配置

          5.1. 基本配置

             web.xml 采用 servlet2.4 規范,默認配置了會話過期時間和網站首頁文件。

          <?xml version="1.0" encoding="UTF-8"?>

          <web-app id="WebApp_ID" 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">

          ?

          ??? <!-- 會話的時間 -->

          ? ??<session-config>

          ????????????? <session-timeout>30</session-timeout>

          ?????? </session-config>

          ?

          ?????? <!-- 歡迎頁面 -->

          ?????? <welcome-file-list>

          ????????????? <welcome-file>index.jsp</welcome-file>

          ?????? </welcome-file-list>

          ??????

          </web-app>

          5.2. 加載 spring 監聽

             在 web.xml 文件中加入如下配置:

          <!-- 設置環境變量,將 web 應用根目錄存儲到環境變量中 -->

          <context-param>

          ??? <param-name>webAppRootKey</param-name>

          ??? <param-value>webapp.root</param-value>

          </context-param>?

          ?

          <!-- 設置環境變量指定 applicationContext.xml 配置文件 -->

          <context-param>

          ??? <param-name>contextConfigLocation</param-name>

          ??? <param-value>

          ??????? /WEB-INF/applicationContext.xml

          ??????? /WEB-INF/applicationContext-admin.xml

          ??????? /WEB-INF/applicationContext-quartz.xml

          ??? </param-value>

          </context-param>???

          ?

          <!-- 設置 context 的監聽,啟動時加載 applicationContext.xml 文件,初始化 applicationContext 實例 -->

          <listener>

          ??? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

          </listener>

          5.3. Log4j 日志

             首先,在 web.xml 文件中加入如下配置:

          <!-- 設置環境變量指定 log4j 的配置文件 -->

          <context-param>

          ??? <param-name>log4jConfigLocation</param-name>

          ??? <param-value>/WEB-INF/log4j.properties</param-value>

          </context-param>

          ?

          <!-- 設置 log4j 的監聽,使日志組件能夠使用,調用 log4jConfigLocation 中設置的環境變量 -->

          <listener>

          ??? <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

          </listener>

           log4j.properties 文件放在 /WEB-INF/ 目錄下, 文件內容如下例,其中根日志級別為 warn ,而在包 cn.idtag 中的類的日志級別為 info

          # 定義根日志的級別和輸出路徑 fatal, error, warn, info, debug

          log4j.rootCategory=warn, stdout, logfile

          ?

          # 在包 cn.idtag 中的類的日志級別

          log4j.logger.cn.idtag=info

          ?

          # 日志輸出參數

          #%c: 日志信息所在類名

          #%d: 日志信息產生時間

          #%p: 日志信息級別

          #%m: 產生的日志具體信息

          #%n: 輸出日志信息換行

          log4j.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %c%n%m%n%n

          ?

          # 控制臺輸出

          log4j.appender.stdout=org.apache.log4j.ConsoleAppender

          log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

          log4j.appender.stdout.layout.ConversionPattern=${log4j.ConversionPattern}

          ?

          # 日志文件輸出

          log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender

          log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/log.txt

          log4j.appender.logfile.Append = true

          log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

          log4j.appender.logfile.layout.ConversionPattern=${log4j.ConversionPattern}

          5.4. 解決中文亂碼

           在 web.xml 文件中加入如下配置,可以解決表單提交的編碼,不需要在每個 *.jsp *.htm 等頁面上編寫代碼“ <%@ page contentType="text/html; charset=gbk" language="java"%> ”了,并且為瀏覽器端設置的“ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ”代碼也不需要寫了,瀏覽器會自動識別,注意建立文件時一定要使用 UTF-8 字符集。

          <!-- 設置字符串過濾器,解決表單 Post 提交中的亂碼問題 -->

          <filter>

          ??? <filter-name>characterEncoding</filter-name>

          ??? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

          ??? <init-param>

          ?? ?????<param-name>encoding</param-name>

          ??????? <param-value>utf-8</param-value>

          ??? </init-param>

          ??? <init-param>

          ??????? <param-name>forceEncoding</param-name>

          ??????? <param-value>true</param-value>

          ??? </init-param>

          </filter>

          <filter-mapping>

          ??? <filter-name>characterEncoding</filter-name>

          ??? <url-pattern>/*</url-pattern>

          </filter-mapping>

          ?

          <!-- 設置 jsp,htm,html 頁面的字符集,是否使用標簽,是否使用 jsp 腳本等,頭尾載入頁面等 -->

          <jsp-config>

          ??? <jsp-property-group>

          ??????? <description> 指定 JSP 文件的配置屬性 </description>

          ??????? <display-name>jspConfiguration</display-name>

          ??????? <url-pattern>*.jsp</url-pattern>

          ??????? <el-ignored>false</el-ignored>

          ??????? <page-encoding>utf-8</page-encoding>

          ??????? <scripting-invalid>false</scripting-invalid>

          ??????? <include-prelude></include-prelude>

          ??????? <include-coda></include-coda>

          ??? </jsp-property-group>

          ?

          ??? <jsp-property-group>

          ??????? <description> 指定 htm 文件的配置屬性 </description>

          ??????? <display-name>jspConfiguration</display-name>

          ??????? <url-pattern>*.htm</url-pattern>

          ??????? <el-ignored>false</el-ignored>

          ??????? <page-encoding>utf-8</page-encoding>

          ??????? <scripting-invalid>false</scripting-invalid>

          ??????? <include-prelude></include-prelude>

          ??????? <include-coda></include-coda>

          ??? </jsp-property-group>

          ?

          ??? <jsp-property-group>

          ??????? <description> 指定 html 文件的配置屬性 </description>

          ??????? <display-name>jspConfiguration</display-name>

          ??????? <url-pattern>*.html</url-pattern>

          ??????? <el-ignored>false</el-ignored>

          ??????? <page-encoding>utf-8</page-encoding>

          ??????? <scripting-invalid>false</scripting-invalid>

          ??????? <include-prelude></include-prelude>

          ??????? <include-coda></include-coda>

          ??? </jsp-property-group>

          ?

          </jsp-config>

           如果應用服務器是 Tomcat ,編輯 Tomcat 根目錄下的“ /conf/server.xml ”文件,找到 Connector 配置部分,在配置結尾加入“ URIEncoding="utf-8" ”:

          <Connector port="8080" maxThreads="150" …… disableUploadTimeout="true"/>

          改為:

          <Connector port="8080" maxThreads="150" …… disableUploadTimeout="true" URIEncoding="UTF-8"/>

           可解決系統內部 GET 方法傳遞中文字符參數的亂碼問題,但是在外部 URL 請求中參數值帶有中文的,必須使用 URLEncode 編碼。

          5.5. 配置 servlet

           在 web.xml 文件中配置 servlet 請求路徑和實現類:

          <servlet>

          ??? <servlet-name>sendSms</servlet-name>

          ??? <servlet-class>cn.idtag.test.servlet.SendSmsServlet</servlet-class>

          </servlet>

          <servlet-mapping>

          ??? <servlet-name>sendSms</servlet-name>

          ??? <url-pattern>/servlet/sendsms</url-pattern>

          </servlet-mapping>

           而 SendSmsServlet.java 文件需要繼承 HttpServlet 類,如果 response 要輸出內容并包含非英文字符,要設置輸出的字符集為 UTF-8

          package cn.idtag.test.servlet;

          ?

          import javax.servlet.http.HttpServletRequest;

          import javax.servlet.http.HttpServletResponse;

          import javax.servlet.http.HttpServlet;

          import javax.servlet.ServletException;

          import java.io.IOException;

          ?

          public class SendSmsServlet extends HttpServlet

          {

          ??? public void doGet(HttpServletRequest request, HttpServletResponse response)

          ??????????? throws ServletException, IOException

          {

          ??????? response.setContentType("text/html;charset=utf-8");

          ??????? PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "utf-8"));

          ??????? try

          ??????? {

          ??????? //do something…

          ??????? } finally

          ??????? {

          ??????????? out.close();

          ??????? }

          ??? }

          ?

          ??? public void doPost(HttpServletRequest request, HttpServletResponse response)

          ??????????? throws ServletException, IOException

          ?? ?{

          ??????? doGet(request, response);

          ??? }

          }


          主站蜘蛛池模板: 嘉峪关市| 汉阴县| 天门市| 曲水县| 宁陵县| 新绛县| 光泽县| 江陵县| 衡东县| 麟游县| 沅江市| 嘉鱼县| 神池县| 和硕县| 醴陵市| 梅河口市| 鄯善县| 犍为县| 阿拉善右旗| 黄平县| 亳州市| 孙吴县| 玉林市| 彭水| 德州市| 盐源县| 类乌齐县| 宁波市| 定安县| 石渠县| 眉山市| 民丰县| 那曲县| 麟游县| 长顺县| 阳谷县| 东乡县| 汉寿县| 庄河市| 白沙| 盐边县|