[关闭]
@cxm-2016 2016-12-23T11:30:02.000000Z 字数 3665 阅读 1936

Java Web (一)——创建工程

Web

版本:1
作者:陈小默
声明:禁止商业,禁止转载


使用正式版IDEA(不是社区版),创建一个普通的maven项目,然后补全目录为web应用目录。

添加 Java Web 的 Maven 依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>cxm</groupId>
  7. <artifactId>cxm</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>war</packaging>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <kotlin.version>1.0.5</kotlin.version>
  13. </properties>
  14. <build>
  15. <plugins>
  16. <plugin>
  17. <groupId>org.apache.maven.plugins</groupId>
  18. <artifactId>maven-compiler-plugin</artifactId>
  19. <version>3.6.0</version>
  20. </plugin>
  21. <plugin>
  22. <groupId>org.apache.maven.plugins</groupId>
  23. <artifactId>maven-surefire-plugin</artifactId>
  24. <version>2.19.1</version>
  25. </plugin>
  26. <plugin>
  27. <groupId>org.jetbrains.kotlin</groupId>
  28. <artifactId>kotlin-maven-plugin</artifactId>
  29. <version>${kotlin.version}</version>
  30. <executions>
  31. <execution>
  32. <id>compile</id>
  33. <phase>compile</phase>
  34. <goals>
  35. <goal>compile</goal>
  36. </goals>
  37. </execution>
  38. <execution>
  39. <id>test-compile</id>
  40. <phase>test-compile</phase>
  41. <goals>
  42. <goal>test-compile</goal>
  43. </goals>
  44. </execution>
  45. </executions>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. <dependencies>
  50. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  51. <dependency>
  52. <groupId>javax.servlet</groupId>
  53. <artifactId>javax.servlet-api</artifactId>
  54. <version>3.1.0</version>
  55. <scope>provided</scope>
  56. </dependency>
  57. <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
  58. <dependency>
  59. <groupId>javax.servlet.jsp</groupId>
  60. <artifactId>jsp-api</artifactId>
  61. <version>2.2</version>
  62. <scope>provided</scope>
  63. </dependency>
  64. <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
  65. <dependency>
  66. <groupId>javax.servlet</groupId>
  67. <artifactId>jstl</artifactId>
  68. <version>1.2</version>
  69. <scope>runtime</scope>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.jetbrains.kotlin</groupId>
  73. <artifactId>kotlin-stdlib</artifactId>
  74. <version>${kotlin.version}</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.jetbrains.kotlin</groupId>
  78. <artifactId>kotlin-test</artifactId>
  79. <version>${kotlin.version}</version>
  80. <scope>test</scope>
  81. </dependency>
  82. </dependencies>
  83. </project>

在 IDEA 中配置 Tomcat

首先,我们小在IDEA中配置Tomcat,详细过程如下

  1. 单击IDEA的Edit Configurations,弹出Run/Debug Configurations对话框。
  2. 单击+按钮,选择Tomcat Service -> Local 选项。
  3. 给当前服务器设置一个名字,比如smart,取消勾选After launch选项。
  4. 单击Application Service下拉框的Configure按钮,配置一个Tomcat,配置完成后,在下拉框选择该Tomcat。
  5. 切换到Deployment选项卡,单击+按钮,选择Artifact选项,弹出 Select Artifact to Deploy对话框。
  6. 选择 chapter1:war exploded,然后在Application context中输入服务器名称/smart
  7. 切换回Service选项卡,在On frame deactivation下拉框中选择Upload resources

Hello Servlet

接下来我们使用一个简单的示例测试是否成功

创建一个Servlet

  1. package com.github.cccxm.smart
  2. import javax.servlet.http.HttpServlet
  3. import javax.servlet.http.HttpServletRequest
  4. import javax.servlet.http.HttpServletResponse
  5. /**
  6. * HelloServlet
  7. */
  8. class HelloServlet : HttpServlet() {
  9. override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
  10. val writer = resp.writer
  11. writer.write("hello servlet")
  12. writer.flush()
  13. }
  14. }

修改web.xml文件

  1. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  4. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <display-name>smart</display-name>
  7. <servlet>
  8. <servlet-name>HelloServlet</servlet-name>
  9. <servlet-class>com.github.cccxm.smart.HelloServlet</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>HelloServlet</servlet-name>
  13. <url-pattern>/hello</url-pattern>
  14. </servlet-mapping>
  15. </web-app>

然后点击运行按钮,等待服务器启动之后我们打开浏览器,输入http://127.0.0.1:8080/smart/hello,访问后就能看见浏览器上打印了一句hello servlet

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注