当前位置:网站首页>Servlet基础详细版
Servlet基础详细版
2022-08-04 10:05:00 【我爱布朗熊】
在了解Servlet之前,我们需要先了解一下Tomcat
一、了解Servlet
1.Servlet是JavaEE规范之一,规范就是接口
2.Servlet是JavaWeb三大组件之一(三大组件分别是:Servlet程序、Filter过滤器、Listener监听器)
3.Servlet是运行在服务器上的一个java小程序,它可以接收客户端发送过来的请求,并响应数据给客户端。
二、手动实现Servlet程序
1. 创建工程

2.步骤及程序
①编写一个类去实现Servlet接口
②实现service方法处理请求,并响应数据
③到web.xml中去配置Servlet程序的访问地址
程序

package com.company.web_tomcat;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet2 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* service方法专门用来处理请求和响应的
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("hello servlet");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
<?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">
<!-- servlet标签给Tomcat配置Servlet程序-->
<servlet>
<!-- 给Servlet程序起一个别名(一般是类名) -->
<servlet-name>HelloServlet2</servlet-name>
<!-- 是Servlet全类名 -->
<servlet-class>com.company.web_tomcat.HelloServlet2</servlet-class>
</servlet>
<!-- servlet-mapping标签给Servlet程序配置一个访问地址,如果不加这个标签的话,上面标签servlet-name便会报错-->
<servlet-mapping>
<!-- 此标签的作用是告诉服务器是告诉服务器,我当前配置的地址是给哪个Servlet程序使用 -->
<servlet-name>HelloServlet2</servlet-name>
<!-- 配置访问地址
/ 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径
/hello http://ip:port/工程路径/hello -->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>下图中蓝色区域“Application context”就是工程路径(可以自行修改)

最终运行成功后,会弹出一个页面,如下图所示(下面访问的是工程路径结尾)

至于为什么会弹出上图,原因就是我们在index.jsp中编写的内容
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
<br/>
<a href="hello-servlet">Hello Servlet</a>
</body>
</html>http://localhost:8080/web_Tomcat_war_exploded/ 我们在此路径后面添加hello会发生下图现象
即访问 http://localhost:8080/web_Tomcat_war_exploded/hello
控制台输出了“hello servlet” (此原因就是在上面web.xml中配置的,可在上面仔细阅读xml文件)

3.URL地址到Servlet程序的访问(怎么访问到的程序)
http://localhost:8080/web_Tomcat_war_exploded/hello
为什么我们敲上面这个地址,就可以访问到我们的HelloServlet2程序呢?

4.Servlet的生命周期
第一步:执行Servlet构造器方法
第二步:执行init初始化方法
第一二步是在第一次访问的时候创建Servlet程序会调用
第三步:执行service方法(每次访问都会执行)
第四步:执行destroy销毁方法(只有在web工程停止的时候才会执行)
package com.company.web_tomcat;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet2 implements Servlet {
public HelloServlet2() {
System.out.println("1 构造器方法");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2 初始化方法");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* service方法专门用来处理请求和响应的
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("3 service方法");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("4 销毁方法");
}
}
执行web工程

如果将页面一直刷新的话,会不断的执行service方法

将程序停止

边栏推荐
- 2022-08-03 第六小组 瞒春 学习笔记
- Mysql 存储引擎简介
- 《福格行为模型》:如何养成好习惯?
- 常用的输入对象
- Introduction to the core methods of the CompletableFuture interface
- 多媒体和物联网技术让版本“活”起来 129张黑胶唱片“百年留声”
- No module named 'flask_misaka' has been resolved [BUG solution]
- 物体颜色的来源
- LeetCode581+621+207
- [Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
猜你喜欢

EastWave应用:自动计算光子晶体透反率

I am 37 this year, and I was rushed by a big factory to...

如何直击固定资产管理的难题?
![Detailed explanation of telnet remote login aaa mode [Huawei eNSP]](/img/cf/aaf3a0b794b1076423fc5b90ecc9f0.png)
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]

LVS+Keepalived群集部署

《福格行为模型》:如何养成好习惯?

参数优化文档介绍

Win11系统重装用什么好 一键重装Win11教程

Win11如何隐藏输入法悬浮窗?
![Could you please talk about how the website is accessed?[Interview questions in the web field]](/img/06/5ecc617edc4131c31f71d5e019a64c.png)
Could you please talk about how the website is accessed?[Interview questions in the web field]
随机推荐
matlab练习程序(多线段交点)
HTB-Sense
无代码平台多行文字入门教程
sqlilabs less-38~39
leetcode单调栈经典例题——最大矩形
I am 37 this year, and I was rushed by a big factory to...
HCIP 交换实验
MindSpore:MindSpore GPU版本安装问题
js文字转语音播报
Win11不识别蓝牙适配器的解决方法
数据使用要谨慎——不良数据带来严重后果
Four common methods of network attacks and their protection
在测试集上训练,还能中CVPR?这篇IEEE批判论文是否合理?
IDEA 自动导入的配置(Auto import)
Producer and Consumer Problems in Concurrent Programming
无代码平台描述文字入门教程
KubeDNS 和 CoreDNS
Introduction to Mysql storage engine
被Win11安全中心误删除的文件怎么恢复?
iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
https://blog.csdn.net/weixin_51351637/article/details/126110360?spm=1001.2014.3001.5502