web.xml
ロード順番:
context-param -> listener -> filter -> servlet
基本
<web-app>
1.アイコン
<icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>
2.名称
<display-name>Tomcat Example</display-name>
3.説明
<disciption>Tomcat Example servlets and JSP pages.</disciption>
4.初期パラメタ
<context-param>
<param-name>parameter</para-name>
<param-value>test</param-value>
<description>It is a parameter.</description>
</context-param>
5.フィルター
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>com.test.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6.リスナー
<listener>
<listerner-class>listener.SessionListener</listener-class>
</listener>
7.Servlet
一般
<servlet>
<servlet-name>init</servlet-name>
<servlet-class>InitServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>init</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
複雑
<servlet>
<servlet-name>init</servlet-name>
<servlet-class>InitServlet</servlet-class>
<jsp-file>/config.jsp</jsp-file>
<init-param>
<param-name>flg</param-name>
<param-value>true</param-value>
</init-param>
正の数/0:小さい数字からロード;負の数/未定:はじめてアクセス時ロード
<load-on-startup>0</load-on-startup>
<run-as>
<description>anonymous access</description>
<role-name>tomcat</role-name>
</run-as>
</servlet>
<servlet-mapping>
<servlet-name>init</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
8.セッションタイムアウト(単位:分)
<session-config>
<session-timeout>120</session-timeout>
</session-config>
9.MIME
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
10.ウェルカムページ
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
11.エラーページ
方法1:エラーコードにより
<error-page>
<error-code>404</error-code>
<location>/notFound.jsp</location>
</error-page>
方法2:例外の種類により
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
12.TLD(タグライブラリ)
<taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
</taglib>
<jsp-config>
<taglib>
<taglib-uri>
http://jakarta.apache.org/tomcat/debug-taglib
</taglib-uri>
<taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
</taglib>
</jsp-config>
13.リソース
<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>
14.リソース参照
<resource-ref>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>JNDI JDBC DataSource</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
15.セキュリティ
<security-constraint>
<display-name>Example</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/jsp/security/protected/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>
16.ログイン
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Authentiation Area</realm-name>
<form-login-config>
<form-login-page>
/jsp/security/protected/login.jsp
</form-login-page>
<form-error-page>
/jsp/security/protected/error.jsp
</form-error-page>
</form-login-config>
</login-config>
17.セキュリティロール
<security-role>
<role-name>tomcat</role-name>
</security-role>
18.Web環境
<env-entry>
<env-entry-name>minExample</env-entry-name>
<env-entry-value>1</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
19.EJB
<ejb-ref>
<description>EJB reference</decription>
<ejb-ref-name>ejb/Account</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>com.test.AccountHome</home>
<remote>com.test.Account</remote>
</ejb-ref>
20.ローカルEJB
<ejb-local-ref>
<description>Loacal EJB reference</decription>
<ejb-ref-name>ejb/Order</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.test.OrderHome</local-home>
<local>com.test.Order</local>
</ejb-local-ref>
</web-app>
応用
Struts 1.x
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-nested</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>
Struts 2.x
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher 古いバージョン
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 新
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
JSF
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServelet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern> *.xhtml, *.faces
</servlet-mapping>
context-param
javax.faces.CONFIG_FILES
javax.faces.DEFAULT_SUFFIX
javax.faces.FACELETS_BUFFER_SIZE
javax.faces.FACELETS_REFRESH_PERIOD
javax.faces.FACELETS_SKIP_COMMENTS
javax.faces.LIFECYCLE_ID
javax.faces.STATE_SAVEING_METHOD
javax.faces.PROJECT_STAGE Development,UnitTest,SystemTest,Production
javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER
javax.faces.LIBRARIES
Spring
Servletとして
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>ContextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/springServlet/*</url-pattern>
</servlet-mapping>
listenerとして
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 書き方1 -->
<param-value>
/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
</param-value>
<!-- 書き方2 -->
<param-value>
classpath:spring/applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
※classpathは/WEB-INF/classes
★初期化パラメータの設定
方法1.Servlet・Filter単位の初期化パラメータ(特定Servlet・Filterのみ使える)
<init-param>
<param-name>param1</param-name>
<param-value>aaa</param-value>
</init-param>
利用例
ServletConfig sc = getServletConfig();
String param1 = sc.getInitParameter("param1");
方法2.コンテキスト単位の初期化パラメータ(すべてServlet・Filter使える)
<context-param>
<param-name>globel1</param-name>
<param-value>bbb</param-value>
</context-param>
利用例
ServletContext scon = getServletConfig().getServletContext();
String globel1 = scon.getInitParameter("globel1");
方法3.AnnotationによるDI
<env-entry>
<env-entry-name>hello</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>This is test.</env-entry-value>
</env-entry>
利用例
@Resource(name="hello")
private String hello;