使用Maven3管理和发布Web项目

这里用的是Maven-3.0.3。。老样子,先建一个Maven工程


这是Maven3的核心文件pom.xml

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jadyer.blog</groupId>
	<artifactId>blog-servlet</artifactId>
	<version>0.0.1.Release</version>
	<!-- 注意这里为war -->
	<packaging>war</packaging>
	<name>玄玉的手工博客系统之Servlet模块</name>
	
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>4.10</junit.version>
		<javax.servlet-api.version>3.0.1</javax.servlet-api.version>
		<javax.servlet.jsp-api.version>2.2.1</javax.servlet.jsp-api.version>
		<maven-war-plugin.version>2.2</maven-war-plugin.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>${javax.servlet-api.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>${javax.servlet.jsp-api.version}</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	
	<build>
		<!-- 下面,以aa代指生成的target下Web应用的文件夹名称,以bb代指发布到\tomcat\webapps\目录下的文件名称 -->
		<!-- 1、aa以<finalName>值为准,并且无视<warName>值。若未指定<finalName>值,则默认为'artifactId-version' -->
		<!-- 2、bb以<warName>值为优先。若未指定<warName>值,则以<finalName>值为准。若二者均未指定,则默认为'artifactId-version' -->
		<finalName>myBlogServlet</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>${maven-war-plugin.version}</version>
				<configuration>
					<!-- Run As Maven build : clean package 就可以发布到tomcat下了,并按照<warName>命名 -->
					<!-- 访问应用时,访问该地址即可:http://IP:Port/warName/ -->
					<warName>MyMavenStudyWebWarFile</warName>
					<outputDirectory>D:\Develop\apache-tomcat-6.0.35\webapps</outputDirectory>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

下面是位于//src//main//java//中的普通Servlet----HomeServlet.java

package com.jadyer.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HomeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("YouName", "吴三省");
		request.getRequestDispatcher("/jadyer.jsp").forward(request, response);
	}
}

然后在//src//main//下手工建立//webapp//和//webapp//WEB-INF//目录


下面是位于//src//main//webapp//WEB-INF//目录中的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
		<servlet-name>HomeServlet</servlet-name>
		<servlet-class>com.jadyer.servlet.HomeServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HomeServlet</servlet-name>
		<url-pattern>/servlet/HomeServlet</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>servlet/HomeServlet</welcome-file>
	</welcome-file-list>
</web-app>

最后是位于//src//main//webapp//目录中的jadyer.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<h2>欢迎:${YouName}</h2>

你可能感兴趣的:(maven,tomcat,Web,servlet,Build,encoding)