使用Eclipse+maven3外掛開發一個Servlet3.0的簡單例子
1、開啟Eclipse4.2,新建一個Maven專案
1)選擇maven的archetype為maven-archetype-webapp
2)填寫專案資訊
2、完善專案目錄結構
1)手工新增 src/main/java、src/test/java和src/test/resource 三個source folder,在專案上右鍵 New ---->Source Folder,輸入folder name即可。
2)修改Source Folder的編譯路徑,進入Java Build Bath視窗,雙擊每個資料夾下的Output folder,指定路徑。
src/main/java和src/main/resource指定target/calsses,
src/test/java和src/test/resource指定target/test-classes
都選上選上Allow output folders for source folders.
3、把專案轉為 Dynamic Web Module
1)在專案上點選右鍵,選擇屬性,然後選擇Project facets,然後點選Convert to faceted form...
勾選Dynamic Web Module,選擇3.0,表示支援servlet3.0
勾選Java,選擇1.6或更高的jdk版本
然後再點選Further configuration available...,在彈出的視窗中Context directory輸入:src/main/webapp
2)配置部署的程式Deployment Assembly,刪掉test的那兩項,test的程式釋出的時候不需要,新增maven的依賴庫,Eclipse釋出的時候可以釋出到lib目錄中
3)刪除jdk5,新增jdk6
4、pom.xml檔案配置,新增Servlet3依賴、日誌配置等
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.luxh.app</groupId> <artifactId>app</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>app Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 屬性配置 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.10</junit.version> <logback.version>1.0.7</logback.version> <slf4j.version>1.7.2</slf4j.version> </properties> <!-- 依賴配置 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1-b02</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> </dependencies> <build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
5、logback.xml日誌檔案配置,該檔案存放在src/main/resources/目錄下
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 控制檯輸出 --> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="stdout"/> </root> </configuration>
6、編寫Servlet
1)寫個編碼過濾器
package cn.luxh.app.filter; 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 javax.servlet.annotation.WebFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Servlet Filter implementation class EncodingFilter */ @WebFilter("/EncodingFilter") public class EncodingFilter implements Filter { final static Logger LOG = LoggerFactory.getLogger(EncodingFilter.class); public void destroy() { LOG.info("銷燬......"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { LOG.info("初始化......"); } }
2)第一個Servlet
package cn.luxh.app.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Servlet implementation class FirstServlet */ @WebServlet("/FirstServlet") public class FirstServlet extends HttpServlet { final static Logger LOG = LoggerFactory.getLogger(FirstServlet.class); private static final long serialVersionUID = 6833688147345340845L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LOG.info("hello"); PrintWriter out = response.getWriter(); out.print("first servlet"); out.flush(); out.close(); } }
3)web.xml不需要任何配置,用tomcat7釋出,tomcat6不支援Servlet3。訪問:http://localhost:8080/app/FirstServlet