Servlet

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

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都是在自己所支配的那一段线程里面执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值