使用Ideal创建javaWeb项目没用使用maven的模板直接通过开发工具新建项目.通过配置pom文件,修改文件结构,配置Tomcat容器最终实现Hello Servlet输出.
1,点击”Create New Project”,根据Ideal指引新建一个项目
pom文件通过xml格式保存项目的信息.如,打包方式,依赖关系,插件等
2,修改编目规则为UTF-8,添加相关依赖,添加插件,修改打包方式
<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.0modelVersion>
<groupId>cn.dsliang.librarygroupId>
<artifactId>libraryartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<packaging>warpackaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.3version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.18.1version>
<configuration>
<skipTests>trueskipTests>
configuration>
plugin>
plugins>
build>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
<scope>runtimescope>
dependency>
dependencies>
project>
Idea默认生成的项目结构并不是Web项目对于的项目结构.接下来完成一下四个步骤.
1,在main目录下添加webapp目录
2,webapp目录下添加WEB-INF目录
3,WEB-INF目录下添加web.xml文件
4,web.xml文件添加对应代码
<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">
web-app>
1,点击”Run”->”Edit Configurations”
2,点击”+”选择”Tomcat Server”->Local
4,点击”Deployment”选项卡配置项目,首先点击”+”选择”library:war”
5,在”Application context”中输入项目名称
6,取消”After launch”勾选.点击”OK”完成Tomcat容器设置
到此Tomcat服务器已经配置完成.点击运行项目会自动加载到Tomcat容器中.
编写一个简单的Servlet不用我多说了.
package cn.dsliang.library;
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;
/**
* Created by dsliang on 2017/10/18.
*/
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hello Servlet!");
return;
}
}
参考:
关于javax.servlet-api和servlet-api区别
http://blog.csdn.net/qq_20314665/article/details/73436510
IDEA的配置问题,如何配置让其把class文件输出到classes文件夹
https://segmentfault.com/q/1010000005359797/a-1020000005360560
MAVEN Scope使用
http://acooly.iteye.com/blog/1788890
Maven最佳实践:版本管理
http://juvenshun.iteye.com/blog/376422
Maven项目命名规范
http://blog.csdn.net/limm33/article/details/60959044