IDEA12里用maven创建java web project

1、new project 选择maven module

IDEA12里用maven创建java web project_第1张图片

输入groupid,即报名

artifactid即project名称

2、勾选create from archetype下面的

org.apache.maven.archetypes:maven-archetype-webapp

IDEA12里用maven创建java web project_第2张图片

3、配置pom.xml


<dependencies>
        <!-- JUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>provided</scope>
        </dependency>


        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>


        <!-- Log4J -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>


在pom.xml文件右键,maven,reimport 

配置web.xml及log4j


<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
           version="3.0">

     <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>log4j-init</servlet-name>
        <servlet-class>Log4jInit</servlet-class>
        <init-param>
            <param-name>log4j</param-name>
            <param-value>WEB-INF/log4j.properties</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>log4j-init</servlet-name>
        <url-pattern>/never</url-pattern>
    </servlet-mapping>
</web-app>


在src的main目录下新增java文件夹,然后右键open module settings

IDEA12里用maven创建java web project_第3张图片

将java文件夹放入source path

然后新增Log4jInit.java


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

import org.apache.log4j.PropertyConfigurator;

public class Log4jInit extends HttpServlet {

    public void init() throws ServletException {
	   String prefix = getServletContext().getRealPath("/");

	        String file = getInitParameter("log4j");
	        System.setProperty("webappRoot",prefix);    
	        System.out.println("................log4j start");
                System.out.println(System.getProperty("user.dir"));
	        if (file != null) {

	               PropertyConfigurator.configure(prefix + file);

	        }
    }

}

在webapp下的WEB-INF下增加log4j.properties

log4j.rootLogger=DEBUG,R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %p %c{1}.%M(%L) | %m%n
log4j.appender.R.MaxFileSize=30MB
log4j.appender.R.MaxBackupIndex=1  
log4j.appender.R.file=${webappRoot}WEB-INF/log/app.log

配置hello servlet


import org.apache.log4j.Logger;


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 java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/hello")
public class Servlet extends HttpServlet {
    private static final Logger logger = Logger.getLogger(Servlet.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("############hello from log4j");
        logger.info("hello");
        PrintWriter out = response.getWriter();
        out.print("hello servlet");
        out.flush();
        out.close();
    }
}

配置server

IDEA12里用maven创建java web project_第4张图片

配置deployment

IDEA12里用maven创建java web project_第5张图片

然后启动server即可。


你可能感兴趣的:(maven,log4j,servlet,idea)