@cxm-2016
2016-12-23T03:30:02.000000Z
字数 3665
阅读 2261
Web
版本:1
作者:陈小默
声明:禁止商业,禁止转载
使用正式版IDEA(不是社区版),创建一个普通的maven项目,然后补全目录为web应用目录。
<?xml version="1.0" encoding="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.0</modelVersion><groupId>cxm</groupId><artifactId>cxm</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><kotlin.version>1.0.5</kotlin.version></properties><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.0</version></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19.1</version></plugin><plugin><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-maven-plugin</artifactId><version>${kotlin.version}</version><executions><execution><id>compile</id><phase>compile</phase><goals><goal>compile</goal></goals></execution><execution><id>test-compile</id><phase>test-compile</phase><goals><goal>test-compile</goal></goals></execution></executions></plugin></plugins></build><dependencies><!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version><scope>provided</scope></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version><scope>runtime</scope></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-stdlib</artifactId><version>${kotlin.version}</version></dependency><dependency><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-test</artifactId><version>${kotlin.version}</version><scope>test</scope></dependency></dependencies></project>
首先,我们小在IDEA中配置Tomcat,详细过程如下
Edit Configurations,弹出Run/Debug Configurations对话框。+按钮,选择Tomcat Service -> Local 选项。After launch选项。Configure按钮,配置一个Tomcat,配置完成后,在下拉框选择该Tomcat。Deployment选项卡,单击+按钮,选择Artifact选项,弹出 Select Artifact to Deploy对话框。chapter1:war exploded,然后在Application context中输入服务器名称/smartService选项卡,在On frame deactivation下拉框中选择Upload resources。接下来我们使用一个简单的示例测试是否成功
创建一个Servlet
package com.github.cccxm.smartimport javax.servlet.http.HttpServletimport javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponse/*** HelloServlet*/class HelloServlet : HttpServlet() {override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {val writer = resp.writerwriter.write("hello servlet")writer.flush()}}
修改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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><display-name>smart</display-name><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>com.github.cccxm.smart.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping></web-app>
然后点击运行按钮,等待服务器启动之后我们打开浏览器,输入http://127.0.0.1:8080/smart/hello,访问后就能看见浏览器上打印了一句hello servlet