Servlet

本文介绍Servlet的概念及其实现动态网页的功能,详细解释了Servlet的工作流程、生命周期,并通过示例展示了如何统计Servlet的访问次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Servlet概述

Servlet就是运行在web服务器上的java程序,本质上就是java类。为了实现动态网页的目标,简单来说:Servlet的主要功能在于交互式地浏览和修改数据,生成动态web内容,是web开发服务的

Servlet运行过程

1.客户端发送请求到服务器
2.服务器Tomcat到配置文件web.xml找到servlet-mapping映射到servlet-class找到相应的Servlet类
3.Demo相应的类extends HttpServlet(重写doGet、doPost),根据request对象获取浏览器请求的数据,Servlet会将返回的数据封装到response对象中
4.服务器将相应返回给客户端

Servlet生命周期

1.web项目部署到tomcat加载;客户端第一次请求Servlet,Servlet会创建Servlet实例。
2.初始化;加载完Servlet,必须进行初始化,init方法被调用
3.初始化之后,等待客户端请求,如果有发请求 就调用Servlet里面service()方法,根据用户请求,调用doPost或者doGet方法
4.最后。Servlet调用destory方法销毁
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>	
	<h1>统计Servlet的访问次数</h1>
	<a href="/javaee_day03/countServlet" >访问CountServlet</a><br/>
	<a href="/javaee_day03/showServlet" >展示CountServlet被访问次数</a><br/>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>javaee_day03</display-name> 
  <servlet>
    <description></description>
    <display-name>CountServlet</display-name>
    <servlet-name>CountServlet</servlet-name>
    <servlet-class>xinfei.code.count.CountServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CountServlet</servlet-name>
    <url-pattern>/countServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>ShowServlet</display-name>
    <servlet-name>ShowServlet</servlet-name>
    <servlet-class>xinfei.code.count.ShowServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ShowServlet</servlet-name>
    <url-pattern>/showServlet</url-pattern>
  </servlet-mapping>
</web-app>
public class CountServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ServletContext context = getServletContext();
		Integer count = (Integer)context.getAttribute("count");
		count++;
		//覆盖上下文
		context.setAttribute("count", count);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);	
		
	}
	@Override
	public void init() throws ServletException {
		//获取上下文对象
		ServletContext context = getServletContext();
		context.setAttribute("count", 0);
	}
}
public class ShowServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ServletContext context = getServletContext();
		Object count = context.getAttribute("count");
		response.getWriter().print("被访问了"+ count +"次");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

Servlet如何同时处理多个请求?

Servlet是默认采用单例,多线程的方式进行。只要webapp部署到tomcat容器中,Sevlet只会在发布的时候实例化一次,Servlet在其生命周期只有在将项目移除或者停止才会销毁
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->

这两处可以看到service.xml在线程池里面设置线程的数量;当请求到达时,Servlet通过调度线程给请求者;出现不同的线程同一时间访问一个Servlet的时候,其实Servlet的对象只有一个,由于tomcat支持多线程的原因,每个请求执行Servlet都是在自己所支配的那一段线程里面执行。
Windows 系统修复工具主要用于解决 Windows 11/10 系统中的各种常见问题,具有操作简单、功能全面等特点: 文件资源管理器修复:可解决文件资源管理器卡死、崩溃、无响应等问题,能终止崩溃循环。还可修复右键菜单无响应或选项缺失问题,以及重建缩略图缓存,让图片、视频等文件的缩略图正常显示,此外,还能处理桌面缺少回收站图标、回收站损坏等问题。 互联网和连接修复:能够刷新 DNS 缓存,加速网页加载速度,减少访问延迟。可重置 TCP/IP 协议栈,增强网络连接稳定性,减少网络掉线情况,还能还原 Hosts 文件,清除恶意程序对网络设置的篡改,保障网络安全,解决电脑重装系统后网络无法连接、浏览器主页被篡改等问题。 系统修复:集成系统文件检查器(SFC),可自动扫描并修复受损的系统文件。能解决 Windows 激活状态异常的问题,还可重建 DLL 注册库,恢复应用程序兼容性,解决部分软件无法正常运行的问题,同时也能处理如 Windows 沙箱无法启动、Windows 将 JPG 或 JPEG 保存为 JFIF 等系统问题。 系统工具维护:提供启动管理器、服务管理器和进程管理器等工具,用户可控制和管理启动程序、系统服务和当前运行的进程,提高系统的启动和运行速度,防止不必要的程序和服务占用系统资源。还能查看系统规格,如处理器线程数、最大显示分辨率等。 故障排除:集成超过 20 个微软官方诊断工具,可对系统问题进行专业排查,还能生成硬件健康状态报告。能解决搜索和索引故障、邮件和日历应用程序崩溃、设置应用程序无法启动等问题,也可处理打印机、网络适配器、Windows 更新等相关故障。 其他修复功能:可以重置组策略设置、catroot2 文件夹、记事本等多种系统设置和组件,如重置 Windows 应用商店缓存、Windows 防火墙设置等。还能添加重建图标缓存支持,恢复粘滞便笺删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值