当前位置:网站首页>servlet【初识】
servlet【初识】
2022-07-01 02:44:00 【缘友一世】
servlet
1.1 serlvt简介
- servlet就是sun公司开发动态web的一门技术
- Sun在这些Apl中提供一个接口叫做:Servlet,如果你想开发一个Serlvet程序,只需完成两个小步骤:
- 编写一个类,实现Serlvet接口
- 把开发好的Java类部署到web服务器中。
- 把实现了Serlvet接口的java程序叫做:Serlvet
1.2 HelloSerlvet
- Serlvet接口sun公司有两个默认的实现类:HttpServlet
构建一个普通的Maven项目【不使用模板】,删掉里面的src目录,以后就在这个项目里面建立model;这个空的工程就是Maven主工程
关于Maven父子工程的理解:
父项目中会有:
<modules>
<module>servlet-01</module>
</modules>
子项目中会有:
<parent>
<artifactId>javaweb-02-maven</artifactId>
<groupId>com.yang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
父项目中的Java子项目可以直接使用
son extends father
- Maven环境优化
- 修改web.xml为最新的
- 将maven的结构搭建完整
- 编写一个Servlet程序
- 编写一个普通类
- 实现Serlvet接口,直接继承HttpServlet
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的方式不同,可以相互调用,业务逻辑都一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("hello,servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
- 编写Servlet的映射
为什么需要映射:而浏览器需要连接web服务器,我们写的是IAVA程序,但是要通过浏览器访问,所以我们需要再web服务中注册我们写的Servlet,还需给他一个浏览器能够访问的路径
<!--注册Servlet-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.yang.servlet.HelloServlet</servlet-class>
</servlet>
<!--Servlet的请求路径-->
<servlet>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet>
- 配置Tomcat
注意:配置项目发布的路径就可以了 - 启动测试
1.3 Servlet执行原理
1.4 Mapping问题
- 一个Servlet可以指定一个映射路径
- 一个servlet可以指定多个映射路径
- 一个Servlet可以指定通用的映射路径
- 请求默认路径
- 指定一些后缀或者前缀等等……
- 优先级问题
指定了固定的映射路径优先级最高,如果找不到就会走默认的处理请求;
边栏推荐
- Ipmitool download address and possible problems during compilation and installation
- 小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏
- nacos配置中心使用教程
- Xception学习笔记
- 如何在智汀中實現智能鎖與燈、智能窗簾電機場景聯動?
- [JS] [Nuggets] get people who are not followers
- 零基础自学SQL课程 | 窗口函数
- Thread Detach
- 记一次服务部署失败问题排查
- ssh配置免密登录时报错:/usr/bin/ssh-copy-id: ERROR: No identities found 解决方法
猜你喜欢
Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021
园区运营效率提升,小程序容器技术加速应用平台化管理
Pulsar Geo Replication/灾备/地域复制
Nacos configuration center tutorial
Find the length of the common part of two line segments
零基础自学SQL课程 | 窗口函数
The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
MCU firmware packaging Script Software
随机推荐
手机上怎么开户?还有,在线开户安全么?
Nacos configuration center tutorial
Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation
Cluster method synchronous execution framework suona
C language a little bit (may increase in the future)
AI edge computing platform - beaglebone AI 64 introduction
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
SWT / anr problem - storagemanagerservice stuck
记一次服务部署失败问题排查
DenseNet网络论文学习笔记
Mouse over effect I
What is PMP?
I want to know how to open a stock account? Is it safe to open an account online?
Find the length of the common part of two line segments
[PR # 5 A] two way running (state pressure DP)
Pulsar 主题压缩
LabVIEW calculates the camera image sensor resolution and lens focal length
In the industrial Internet, "small" programs have "big" effects
鼠标悬停效果五
ipmitool下载地址和编译安装时可能出现的问题