0%

Servlet初体验

走一遍创建一个web应用程序到部署(启动)的流程IDEA + Maven + Tomcat + Servlet + JSP

Servlet初体验

IDEA+Maven+Tomcat+Servlet+JSP

  • 开发工具:IDEA
  • 项目构建工具:Maven
  • 服务器:Tomcat
  • 动态web技术:JSP/Servlet技术

创建工程

构建一个Maven项目

使用IDEA+Maven,创建一个普通Maven项目,删掉不需要的文件,作为主工程,往后在此基础上新建一个个的模块(module)。把需要用到的依赖尽量导入在主工程(project)内,各个子模块(module)都可以使用,从而避免子模块间重复导入依赖。(关于如何创建一个普通maven项目点这里:传送门

导入依赖

关于如何导入依赖,点这里:传送门

导入Servlet、Jsp依赖。百度搜索“Maven 导入Servlet、Jsp依赖”,选择别人分享的比较新的配置(也可以到官网去查找)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 <!--添加Servlet和JSP依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>

或者下边的:

<!-- servlet依赖和jsp依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>

创建子模块

创建子模块(module),鼠标移到工程上方,点击右键,选择新建模块(module)。使用Maven模板新建一个webapp项目。(关于如何使用Maven模板创建项目点这里:传送门

此时在主工程的pom.xml文件中会自动生成:

1
2
3
<modules>
<module>servlet-0001</module>
</modules>

此时在子模块的pom.xml文件中会自动生成:
若没有自动生成则需要手动添加parent标签,里边子标签到主工程去复制过来。

1
2
3
4
5
6
<!--  手动添加parent标签,里边子标签到主工程去复制过来-->
<parent>
<groupId>com.qsdbl</groupId>
<artifactId>javaweb_study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

Maven环境优化

web.xml

web.xml文件是web应用程序的核心配置(例如:配置Servlet映射Session超时配置过滤器配置等)。将Tomcat中的样例文件中的头文件拷过来,替换掉IDEA生成的web.xml文件(换成新的)。

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<!-- 上边的头文件是在Tomcat中复制过来的-->


</web-app>

新建文件夹

在子模块中新建java、resources文件夹,并标记相应的功能(关于如何新建文件夹和标记点这里:传送门

编写Servlet程序

开发时web应用程序各文件建议放的位置:

  1. 编写一个普通类
  2. 实现Servlet接口,继承HttpServlet
    • 一般重写doGet、doPost方法,实现需要的业务
    • 业务代码也可以只写在其中的一个方法中,在另一个方法中调用该方法即可
  3. 编写Servlet映射

Servlet接口sun公司有两个默认的实现类:HttpServlet,GenericServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的不同方式,可以相互调用,业务逻辑都一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter printWriter = resp.getWriter();//响应流(response,响应,发送数据给浏览器的)
printWriter.print("Hello,Servlet!");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//post请求,也放到doGet方法中处理
}
}

Servlet映射

编写Servlet映射(Mapping)。为什么需要Servlet映射:我们写的是Java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务器中注册我们写的Servlet程序,还需要给它设置一个浏览器能够访问的路径。Servlet映射可以有很多种方式,详情见Mapping问题

web.xml文件中进行配置

  • 注册servlet,将servlet-name与某个类绑定
  • servlet映射,servlet-name与某个请求路径绑定
  • 最终请求路径绑定
1
2
3
4
5
6
7
8
9
10
11
12
<!--  web.xml是我们web程序的核心配置-->
<!-- 注册servlet-->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.qsdbl.servlet.HelloServlet</servlet-class>
</servlet>
<!-- 一个servlet对应一个Mapping(映射)-->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<!-- 请求路径-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>

配置Tomcat

将项目部署到Tomcat,具体操作点这里:传送门。注意:配置项目发布的路径。该应用程序将会通过域名/发布路径访问到。

启动web项目

点击工具栏的绿色三角形,启动web应用程序。会生成一个war后缀的文件,这个就是打包的web应用程序。

成功启动后,会自动跳转到浏览器,打开web应用程序根目录下的index.jsp文件。(浏览器地址会打开创建artifacts时所填写的Application context路径,详情见:传送门

当在地址栏里的地址后边添加servlet程序在web.xml中绑定(映射)的地址“hello“时,会执行类com.qsdbl.servlet.HelloServlet展示的文本会变成:Hello,Servlet!

重复使用的配置

创建项目可能需要重复使用到的一些配置和依赖,这里做一个汇总,方便取用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
1、更新web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<!-- 上边的头文件是在Tomcat中复制过来的-->

</web-app>



2、手动添加parent标签:
<!-- 手动添加parent标签,里边子标签到主工程去复制过来-->
<parent>
<groupId>com.qsdbl</groupId>
<artifactId>javaweb_study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>



3、重写方法doGet、doPost:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//业务代码

}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//post请求,也放到doGet方法中处理
}


4、注册servlet:
<!-- web.xml是我们web程序的核心配置-->
<!-- 注册servlet-->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<!-- 一个servlet对应一个Mapping(映射)-->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<!-- 请求路径-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>

依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<dependencies>
<!-- servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- jsp依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- JSTL表达式的依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- standard标签库的依赖-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
若图片不能正常显示,请在浏览器中打开

欢迎关注我的其它发布渠道