简介
本文介绍如何使用 Eclipse 建立 Java 最简 Web 项目,并使用 tomcat 运行。
步骤
源码:https://github.com/HustLion/java_web_hello_world
新建 web 项目。File->New->Other->Maven->Next->maven-archetype-webapp->输入group id等->确认生成项目。图文步骤可以参考:Eclipse 创建 java maven 的 Hello world 项目
然后会在 jsp 文件上出现红叉,提示是:
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
。此时,增加 javax 依赖<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency>
下载apache, http://mirrors.ustc.edu.cn/apache/tomcat/tomcat-7/v7.0.78/bin/apache-tomcat-7.0.78.zip 到项目根目录下。解压。(此处仅仅是示例,正式使用时应该放在其他的合适位置。并且这样放入会报错,但是这些错误与项目无关,可以正常运行。)
右键单击项目,run as -> run on server -> Apache -> Tomcat v7.0 Server -> next -> 选择刚刚解压的目录 -> finish
然后可以见到 Hello world 页面。
命令行输出:
五月 28, 2017 8:43:22 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:java_web_hello_world' did not find a matching property. 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server version: Apache Tomcat/7.0.78 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server built: May 10 2017 15:02:19 UTC 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server number: 7.0.78.0 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: OS Name: Mac OS X 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: OS Version: 10.12.4 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Architecture: x86_64 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: JVM Version: 1.8.0_112-b16 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: JVM Vendor: Oracle Corporation 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: CATALINA_BASE: /Users/hustlion/myWiki/assets/toolHub/tmp/spring-learn/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: CATALINA_HOME: /Users/hustlion/myWiki/assets/toolHub/tmp/spring-learn/java_web_hello_world/apache-tomcat-7.0.78 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dcatalina.base=/Users/hustlion/myWiki/assets/toolHub/tmp/spring-learn/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dcatalina.home=/Users/hustlion/myWiki/assets/toolHub/tmp/spring-learn/java_web_hello_world/apache-tomcat-7.0.78 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dwtp.deploy=/Users/hustlion/myWiki/assets/toolHub/tmp/spring-learn/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Djava.endorsed.dirs=/Users/hustlion/myWiki/assets/toolHub/tmp/spring-learn/java_web_hello_world/apache-tomcat-7.0.78/endorsed 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dfile.encoding=UTF-8 五月 28, 2017 8:43:22 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/hustlion/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. 五月 28, 2017 8:43:22 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-8080"] 五月 28, 2017 8:43:22 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-bio-8009"] 五月 28, 2017 8:43:22 下午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1545 ms 五月 28, 2017 8:43:22 下午 org.apache.catalina.core.StandardService startInternal 信息: Starting service Catalina 五月 28, 2017 8:43:22 下午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/7.0.78 五月 28, 2017 8:43:23 下午 org.apache.catalina.loader.WebappClassLoaderBase validateJarFile 信息: validateJarFile(/Users/hustlion/myWiki/assets/toolHub/tmp/spring-learn/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/java_web_hello_world/WEB-INF/lib/javax.servlet-api-3.1.0.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class 五月 28, 2017 8:43:23 下午 org.apache.catalina.startup.TldConfig execute 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 五月 28, 2017 8:43:23 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-bio-8080"] 五月 28, 2017 8:43:23 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-bio-8009"] 五月 28, 2017 8:43:23 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 696 ms
参考
- https://maven.apache.org/plugins-archives/maven-archetype-plugin-1.0-alpha-7/examples/webapp.html
- 标准版eclipse配置J2EE插件,使用maven创建web项目(spring JDBC)
- Spring MVC HelloWorld Using Maven in Eclipse
- How to create dynamic web project using maven in eclipse
- How to configure Apache Tomcat in eclipse
- Download and install button not enabled,neon 的一个 bug 导致 tomcat 的 download and install 不可用。所以升级到 oxygen。
- https://www.eclipse.org/downloads/packages/release/Oxygen/RC1
- Installing Apache Tomcat Server
- Step by Step Guide to Setup and Install Apache Tomcat Server in Eclipse Development Environment (IDE)
- https://stackoverflow.com/questions/17745834/unknown-version-of-tomcat-was-specified-with-tomcat-7-0-42
- How to add Tomcat server in Eclipse IDE
- https://www.tutorialspoint.com/maven/maven_web_application.htm
- How to create a Web Application Project with Maven