之前对l的配置⼀直没有太多的重视仔细学习了⼀下发现⾥⾯的东西还是需要值得注意的记录学习的脚步
下⾯的l⽂件是我此⽂进⾏测试的最后内容先贴出来每⼀个标签需要的话都加了必要的解释了如下
<?xml version="1.0" encoding="UTF-8"?>
<!-- The web-app element is the root of the deployment descriptor for a web
application. Note that the sub-elements of this element can be in the arbitrary
order. Because of that, the multiplicity of the elements of distributable,
session-config, welcome-file-list, jsp-config, login-config, and locale-encoding-mapping-list
was changed from "?" to "*" in this schema. However, the deployment descriptor
instance file must not contain multiple elements of session-config, jsp-config,
and login-config. When there are multiple elements of welcome-file-list or
locale-encoding-mapping-list, the container must concatinate the element
contents. The multiple occurance of the element distributable is redundant
and the container treats that case exactly in the same way when there is
only one distributable. -->
<web-app xmlns="java.sun/xml/ns/j2ee" xmlns:xsi="/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="java.sun/xml/ns/j2ee
java.sun/xml/ns/j2ee/web-app_2_4.xsd">
<!-- ⽤于映射本地⽂件在⽹页上显⽰为utf-8编码 -->
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>zh</locale>
<encoding>utf-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
<welcome-file-list>
<!-- The welcome-file element contains file name to use as a default welcome
file, such as index.html -->
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 显⽰在web容器管理界⾯中的名字 -->
<display-name>Archetype Created Web Application</display-name>
<description>⽤于测试l的⽂件</description>
<!-- The context-param element contains the declaration of a web application's
servlet context initialization parameters. -->
<context-param>
<param-name>initName</param-name>
<param-value>初始化servletcontext参数</param-value>
</context-param>
<icon>
<small-icon>/images/first.gif</small-icon>
<large-icon>/images/ganjue.jpg</large-icon>
</icon>
<!-- 使⽤过滤器设置request和response的编码 -->
<filter>
<filter-name>charsetEncodingFilter</filter-name>
<!-- The fully qualified classname of the filter. -->
<filter-class>com.undergrowth.maven.CharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<!-- The listener-class element declares a class in the application must
be registered as a web application listener bean. The value is the fully
qualified classname of the listener class. -->
<listener-class>com.undergrowth.maven.ServletContextListenerImp</listener-class>
</listener>
<!-- 以res的后缀的⽂件的mime类型为text/plain
如果使⽤maven依赖的tomcat mime-type的类型不会变
所以将webconfiguration.war改为单独的tomcat 中运⾏即可看到输出的类型变化
-->
<mime-mapping>
<extension>res</extension>
<!-- window下有很多mime
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MIME\Database\Content Type
常见的例如:
text/html text/plain text/css text/xml
image/gif image/jpeg image/bmp image/png
audio/x-midi audio/mpeg audio/m4p audio/midi
video/mpeg  video/avi video/x-flv video/3gpp
application/x-tar application/rtf application/msword application/msaccess
application/vnd.ms-excel-->
<mime-type>text/html</mime-type>
</mime-mapping>
<!-- 限制web资源的访问 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>textInfos</web-resource-name>
<!-- 在/resources⽬录下的资源都需要访问权限 -->
<url-pattern>/resources/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<!-- 访问的⾓⾊只能是manager-gui,才能访问 -->
<role-name>manager-gui</role-name>
</auth-constraint>
<!-- The development web server does not support HTTPS connections. It ignores
the transport guarantee, so paths intended for use with HTTPS can be tested
using regular HTTP connections to the development web server. -->
<!-- <user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint> -->
</security-constraint>
<!-- 表⽰访问限制的web资源时采⽤的认证⽅式最简单输⼊属于manager-gui⾓⾊下的⽤户名与密码即可 --> <login-config>
<auth-method>BASIC</auth-method>
</login-config>
<!-- 表⽰可以访问web限制资源的⾓⾊此处的⾓⾊名称需要与
tomcat的conf⽬录下的l⾥⾯配置的⾓⾊名匹配
例如我的在l⾥⾯经过上⾯的和tomcat的配置后 admin⽤户即可访问web的限制资源
<role rolename="manager-gui"/>
<role rolename="pc"/>
<user username="admin" password="admin" roles="manager-gui"/>
<user username="under" password="under" roles="pc"/>
-->
<security-role>
<role-name>manager-gui</role-name>
</security-role>
<servlet>
<servlet-name>ServletMapping</servlet-name>
<!-- The servlet-class element contains the fully qualified class name of
the servlet. -->
<servlet-class>com.undergrowth.maven.ServletMapping</servlet-class>
<!-- The init-param element contains a name/value pair as an initialization
param of a servlet filter -->
<init-param>
<param-name>name</param-name>
<param-value>张三</param-value>
</init-param>
<init-param>
<param-name>age</param-name>
<param-value>22</param-value>
</init-param>
<!-- The load-on-startup element indicates that this servlet should be
loaded (instantiated and have its init() called) on the startup of the web
application. The optional contents of these element must be an integer indicating
the order in which the servlet should be loaded. If the value is a negative
integer, or the element is not present, the container is free to load the
servlet whenever it chooses. If the value is a positive integer or 0, the
container must load and initialize the servlet as the application is deployed.
The container must guarantee that servlets marked with lower integers are
loaded before servlets marked with higher integers. The container may choose
the order of loading of servlets with the same load-on-start-up value. -->
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet>
<servlet-name>RuntimeServlet</servlet-name>
<servlet-class>com.undergrowth.maven.RuntimeServlet</servlet-class>
</servlet>
<!-- 给jsp映射成servlet取名测试 -->
<servlet>
<servlet-name>jspServlet</servlet-name>
<jsp-file>/jspServ.jsp</jsp-file>
</servlet>
<!-- The <servlet-mapping> element specifies a URL pattern and the name
of a declared servlet to use for requests whose URL matches the pattern.
The URL pattern can use an asterisk (*) at the beginning or end of the pattern
to indicate zero or more of any character. (The standard does not support
wildcards in the middle of a string, and does not allow multiple wildcards
in one pattern.) The pattern matches the full path of the URL, starting with
and including the forward slash (/) following the domain name. -->
<servlet-mapping>
<servlet-name>ServletMapping</servlet-name>
<url-pattern>/ServletMapping</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RuntimeServlet</servlet-name>
<url-pattern>/RuntimeServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jspServlet</servlet-name>
<url-pattern>/jspServlet.ser</url-pattern>
</servlet-mapping>
<!-- The session-timeout element defines the default session timeout interval
for all sessions created in this web application. The specified timeout must
be expressed in a whole number of minutes. If the timeout is 0 or less, the
container ensures the default behaviour of sessions is never to time out.
If this element is not specified, the container must set its default timeout
period. -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- Note: At present, you cannot configure custom error handlers for some
error conditions. Specifically, you cannot customize the 404 response page
when no servlet mapping is defined for a URL, the 403 quota error page, or
the 500 server error page that appears after an App Engine internal error. -->
<error-page>
<error-code>404</error-code>
<!-- The location element contains the location of the resource in the web application relative to the root of the web application. The value of the location must have a leading `/'. -->  <location>/index.jsp</location>
</error-page>
<error-page>
<!-- 当出现下⾯的异常的时候跳转到runtime.jsp界⾯ -->
<exception-type>com.undergrowth.maven.RuntimeExceptionTest</exception-type>
<location>/runtime.jsp</location>
</error-page>
</web-app>
对于上⾯的内容来看看 servlet、filter、listener、资源访问等等的实例分别如下
1.Listener的实例在上⾯的l⽂件中有⼀段的配置
<listener>
<!-- The listener-class element declares a class in the application must
be registered as a web application listener bean. The value is the fully
qualified classname of the listener class. -->
<listener-class>com.undergrowth.maven.ServletContextListenerImp</listener-class>
</listener>
此的实现⽂件 ServletContextListenerImp.java 如下
package com.undergrowth.maven;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ServletContextListenerImp implements ServletContextListener {
private Logger Logger(ServletContextListenerImp.class);
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
// TODO Auto-generated method stub
logger.info("初始化:"+SimpleName());
//获取在l中配置的context-param中的值
String ServletContext().getInitParameter("initName");
logger.info("初始化ServletContext的值:"+initName);
}
}
在tomcat启动的时候打印如下信息会看到Listener在web项⽬启动的时候加载
2 Filter  实例看到上⾯的图⽚中有⼀⾏信息获取过滤的编码:utf-8  即为过滤器的初始化信息
A filter is a class that acts on a request like a servlet, but may allow the handling of the request to continue with other filters or servlets. 在l中 filter的配置如下
<!-- 使⽤过滤器设置request和response的编码 -->
<filter>
<filter-name>charsetEncodingFilter</filter-name>
<!-- The fully qualified classname of the filter. -->
<filter-class>com.undergrowth.maven.CharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
过滤器 CharsetEncodingFilter.java 如下
package com.undergrowth.maven;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class CharsetEncodingFilter implements Filter {
private Logger Logger(CharsetEncodingFilter.class);
private String encoding="utf-8";
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
logger.info("修改request和response的编码");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
filterChain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
InitParameter("encoding");
logger.info("获取过滤的编码:"+encoding);
}
}
通过上⾯的⽰例发现在项⽬启动的时候回先实例  Listerer 然后是 Filter
3 Servlet的映射实例
上⾯的l⽂件中
<servlet>
<servlet-name>ServletMapping</servlet-name>
<!-- The servlet-class element contains the fully qualified class name of
the servlet. -->
<servlet-class>com.undergrowth.maven.ServletMapping</servlet-class>
<!-- The init-param element contains a name/value pair as an initialization
param of a servlet filter -->
<init-param>
<param-name>name</param-name>
<param-value>张三</param-value>
</init-param>
<init-param>
<param-name>age</param-name>
<param-value>22</param-value>
</init-param>
<!-- The load-on-startup element indicates that this servlet should be
loaded (instantiated and have its init() called) on the startup of the web
application. The optional contents of these element must be an integer indicating    the order in which the servlet should be loaded. If the value is a negative
integer, or the element is not present, the container is free to load the
servlet whenever it chooses. If the value is a positive integer or 0, the
container must load and initialize the servlet as the application is deployed.
The container must guarantee that servlets marked with lower integers are
loaded before servlets marked with higher integers. The container may choose    the order of loading of servlets with the same load-on-start-up value. -->
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<!-- The <servlet-mapping> element specifies a URL pattern and the name
of a declared servlet to use for requests whose URL matches the pattern.
The URL pattern can use an asterisk (*) at the beginning or end of the pattern
to indicate zero or more of any character. (The standard does not support
wildcards in the middle of a string, and does not allow multiple wildcards
in one pattern.) The pattern matches the full path of the URL, starting with
and including the forward slash (/) following the domain name. -->
log4j2 logger标签
<servlet-mapping>
<servlet-name>ServletMapping</servlet-name>
<url-pattern>/ServletMapping</url-pattern>
</servlet-mapping>
Servlet的实现类ServletMapping.java
package com.undergrowth.maven;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ServletMapping extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static Logger Logger(ServletMapping.class);
/**
* Constructor of the object.
*/
public ServletMapping() {
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 {
logger.info("request的编码:"+CharacterEncoding());

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。