目录结构优先。。。。。。
|--pom.xml
|--src
|--main
|--java
|--com
|--mydomain
|--TestServlet.java
|--resources
|--webapp
|--index.jsp
|--WEB-INF
|--web.xml
在这个测试项目里面,文件非常少,把单元测试文件夹暂时省了,有需要可以根据我其它几个EJB实例里讲到的,添加进去,简单点有利于初学的人去大概了解一下maven,高手就别在这转了,不适合你,呵呵。
其实我没写什么代码,就一个文件名为TestServlet.java的Servlet,其它的都是文件夹,index.jsp也写上Hello World几个字,有需要可以自己加!再加上在web.xml里配置了几行,就这样,其实就是方便大家了解Maven,至于稍深一点的,往后会写,再稍深一点的,我不会,谢谢合作!
好了,下面就把TestServlet.java文件代码贴出来看看
package com.mydomain;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//extend HttpServlet这个类,就是一个Servlet了
public class TestServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
//通常学习的话doGet跟doPost就用下面这种方式将它们合并起来玩就行了
doPost(httpServletRequest, httpServletResponse);
}
@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
//下面这些代码输出一些非常基本的信息,大家可以忽略它
String contextPath = httpServletRequest.getContextPath();
String authType = httpServletRequest.getAuthType();
String charactorEncoding = httpServletRequest.getCharacterEncoding();
String contentType = httpServletRequest.getContentType();
String method = httpServletRequest.getMethod();
String pathInfo = httpServletRequest.getPathInfo();
String pathTranslated = httpServletRequest.getPathTranslated();
String protocol = httpServletRequest.getProtocol();
String queryString = httpServletRequest.getQueryString();
String remoteAddress = httpServletRequest.getRemoteAddr();
String remoteHost = httpServletRequest.getRemoteHost();
String remoteUser = httpServletRequest.getRemoteUser();
String requestURI = httpServletRequest.getRequestURI();
String requestedSessionId = httpServletRequest.getRequestedSessionId();
String scheme = httpServletRequest.getScheme();
String serverName = httpServletRequest.getServerName();
String servletPath = httpServletRequest.getServletPath();
//将信息输出到页面上
PrintWriter printWriter = httpServletResponse.getWriter();
printWriter.println("contextPath = " + contextPath);
printWriter.print("
");
printWriter.println("authType = " + authType);
printWriter.print("
");
printWriter.println("charactorEncoding = " + charactorEncoding);
printWriter.print("
");
printWriter.println("contentType = " + contentType);
printWriter.print("
");
printWriter.println("method = " + method);
printWriter.print("
");
printWriter.println("pathInfo = " + pathInfo);
printWriter.print("
");
printWriter.println("pathTranslated = " + pathTranslated);
printWriter.print("
");
printWriter.println("protocol = " + protocol);
printWriter.print("
");
printWriter.println("queryString = " + queryString);
printWriter.print("
");
printWriter.println("remoteAddress = " + remoteAddress);
printWriter.print("
");
printWriter.println("remoteHost = " + remoteHost);
printWriter.print("
");
printWriter.println("remoteUser = "+ remoteUser);
printWriter.print("
");
printWriter.println("requestURI = " + requestURI);
printWriter.print("
");
printWriter.println("requestedSessionId = " + requestedSessionId);
printWriter.print("
");
printWriter.println("scheme = " + scheme);
printWriter.print("
");
printWriter.println("serverName = " + serverName);
printWriter.print("
");
printWriter.println("servletPath = " + servletPath);
}
}
下面则是web.xml文件,非常简单的
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
Archetype Created Web Application
Test Servlet
com.mydomain.TestServlet
Test Servlet
/TestServlet
当然,Maven项目少不了pom文件,也贴出来吧,给大家剪剪
4.0.0
com.mydomain
wartest
war
1.0-SNAPSHOT
Maven Webapp Test
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
servlet-api
2.3
wartest
maven-compiler-plugin
2.0.2
1.5
1.5
UTF-8
maven-resources-plugin
2.2
UTF-8
好了,写完了,下面进到项目根目录,运行一下
mvn install
就可以在根目录的target得到一个wartest.war了,把它放到Tomcat里面,或者Jboss里面,或者任何其它Web container都可以的,怎么样mvn 还算容易吧