当前位置:网站首页>servlet tutorial 1: environment setup and new servlet project
servlet tutorial 1: environment setup and new servlet project
2022-08-07 06:20:00 【Enlightened gentleman】
Servlet(Server Applet)是 Java Servlet 的简称,称为小服务程序或服务连接器.狭义的 Servlet是指 Java 语言实现的一个接口,广义的 Servlet 是指任何实现了这个 Servlet 接口的类,一般情况下,人们将 Servlet 理解为后者
从本文开始,The author will servlet A summary of related usages,Easy to view and use while working
环境准备,需要已经安装 Java 和 Tomcat,如果没有安装可以参考
Java 安装:https://blog.csdn.net/wsjzzcbq/article/details/87926635
Tomcat 安装:https://blog.csdn.net/wsjzzcbq/article/details/87953594
Eclipse 或 STS 继承 Tomcat:https://blog.csdn.net/wsjzzcbq/article/details/102687559
开发工具使用 STS
目录
1、新建 servlet 项目
打开 eclipse 或 sts 编辑器
新建 servlet 项目,依次点击 File、New、Other...

找到 Web、选择 Dynamic Web Project

项目名称

点击 Finish
servlet 项目创建完成
2、新建 web.xml 文件
在 WEB-INF 目录下新建 web.xml 文件(This file can not be created,因为 Servlet 3.0 Annotations were introduced as an alternative web.xml 文件配置,这里为了演示 xml 方式配置 servlet)

新建 web.xml 文件


点击 Source,将 web.xml 3.0 文件内容复制到 Source 中
web.xml 3.0 文件内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>
3、新建 DemoServlet
新建 DemoServlet

然后将 DemoServlet 继承 HttpServlet
If the project cannot be packaged HttpServlet,You need to configure it for the project server 环境,步骤如下
项目右键 Build Path、Configure Build Path...

点击 Add Library...

选择 Server Runtime

选择 Tomcat

Finish

点击 Apply and Close
配置完成
DemoServlet 继承 javax.servlet.http.HttpServlet,重写 doGet、doPost 方法
DemoServlet 完整代码
package com.learn;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get请求");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post请求");
}
}
在 web.xml 文件中配置 DemoServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- DemoServlet配置 -->
<servlet>
<servlet-class>com.learn.DemoServlet</servlet-class>
<servlet-name>demoServlet</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>demoServlet</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>配置说明
servlet-class: 配 servlet 全路径
servlet-name: 自定义,和 servlet-mapping 中 name 保持一致
url-pattern: servlet 对应的请求 url
4、运行项目
在项目上右键 Run As、Run on Server

选择 Tomcat ,Finish

浏览器请求:http://localhost:8080/servlet-learn/demo
运行效果

servlet 请求成功
servlet 环境搭建完成
至此完
边栏推荐
猜你喜欢
随机推荐
R文件找不到问题
为什么NIO比BIO效率高
数组扁平化
dp,dpi,px知识补充
功夫不负有心人,机械师T58完美吃上黑贫果
2022A特种设备相关管理(电梯)特种作业证考试题库模拟考试平台操作
R语言train函数训练模型报错(Error in summary.connection(connection) : 链结不对)
Spark基础【运行架构、RDD】
MyTinySTL的deque源码分析
Tag: top stack
VoLTE基础自学系列 | VoWIFI架构图
简述FileInputStream与BufferedInputStream的区别
2020年初全国行政区划矢量数据
servlet 教程 2:返回 jsp 页面
某音App protobuf协议还原逆向分析
cron 表达式
C陷阱——数组越界引发的死循环问题
Unity Shader: Blend混合
slot插槽二三事
servlet 教程 1:环境搭建和新建 servlet 项目









