HelloWorld
要点
- 实践,创建java web项目。
本系列博客介绍
本系列从项目实战入手,以servlet3.1协议为基础对java web进行讲解。
逐一介绍servlet的各个api使用和原理。
以tomcat服务为例讲解servlet容器的实现原理。
需要的背景知识:
- eclipse、maven的使用。
- http协议内容。
- java基础。
eclipse创建java web项目步骤
既然要从实战出发,我们在系列开头就用eclipse创建一个maven的java web项目。具体的创建步骤如下。关于eclipse和maven的使用这里不做多的介绍,基本上不会影响对servlet的学习。
- 打开【File】->【New】->【Other】菜单,如下图选择【Maven Project】,进行下一步【Next】。
- 面的页面可以自定选择项目的路径。
- 接下来选择图下图所示【mave-archetype-webapp】,在进行下一步。
- 这一步骤中有Group Id 和 Artifact 需要自行填写。Group Id通用就是类似与com.spring, Artifact就类似与spring下的一个子项目core。
- 点击【Finish】完成项目创建。我们会得到一个如下目录结构的工程。其中红色框部分就是我们servlet项目的内容。pom.xml是maven的配置文件(代码见本文最后。)。项目的创建到此就完成了。
创建静态网页
现在我们的项目中什么都还没有,我首先来建一个最原始的静态网页。然后通过eclipse配置的tomcat把项目运行起来。
首先,在webapp目录下创建一个index.html的文件代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
创建完成之后我们右击选择【Debug As】->【Run On Server】。然后选择配置好的tomcat服务器,讲我们的项目添加到tomcat中去运行。(关于eclipse的tomcat使用这里不做介绍,大家可以自己搜索一下)启动tomcat之后我们在浏览器中访问http://localhost:8080/x_servlet/index.html地址,就可以访问到有Hello World字样的静态网页。
这里地址x_servlet是我们的项目名称, locathost就是本机IP地址,8080是tomcat的默认端口号。http协议这里就不深入介绍。
创建servlet
接下来就到我们的servlet,先创建一个servlet的java类。
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getOutputStream().write("Hello Servlet".getBytes());
}
}
FirstServlet类是集成了HttpServlet的,我们在后面详细解释。 实现的doGet方法用来接收浏览器的get请求。resp.getOutputStream().write(“Hello Servlet”.getBytes());这段代码就是向前端返回的内容信息。
除了新建java类之外,我们还要在web.xml里面配置一下。
<servlet>
<servlet-class>com.gzq.servlets.FirstServlet</servlet-class>
<servlet-name>firstServlet</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
再次启动tomcat,访问http://localhost:8080/x_servlet/first。我们可以得到Hello Servlet内容的页面。
标签标示我们想tomcat容器注册了一个servlet,标签告诉tomcat容器我们使用哪个路径可以访问当前的servlet。
本节我们不详细展开讲解,关于servlet的标签使用,servlet容器是如何管理servlet后面详细介绍。
pom.xml文件内容
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gzq</groupId>
<artifactId>x_servlet</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>x_servlet</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java-version>1.8</java-version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
<finalName>x_servlet</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArguments>
<verbose />
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>