当前位置: 首页 > news >正文

网站 网站建设定制网站建设的方法有哪些

网站 网站建设定制,网站建设的方法有哪些,wordpress 不显示评论,WordPress 团队管理系统Servlet的生命周期有四个阶段:加载并实例化、初始化、请求处理、销毁。主要涉及到的方法有init、service、doGet、doPost、destory等 加载并实例化 Servlet容器负责加载和实例化Servelt。当Servlet容器启动时,或者在容器检测到需要这个Servlet来响应第一…

  Servlet的生命周期有四个阶段:加载并实例化、初始化、请求处理、销毁。主要涉及到的方法有init、service、doGet、doPost、destory等

加载并实例化

 Servlet容器负责加载和实例化Servelt。当Servlet容器启动时,或者在容器检测到需要这个Servlet来响应第一个请求时,创建Servlet实例。当Servlet容器启动后,Servlet通过类加载器来加载Servlet类,加载完成后再new一个Servlet对象来完成实例化。

Servlet 生命周期

Servlet在服务器里是单例的,是

Servlet 生命周期可被定义为从创建直到毁灭的整个过程。以下是 Servlet 遵循的过程:

  • Servlet 初始化后调用 init () 方法。
  • Servlet 调用 service() 方法来处理客户端的请求。
  • Servlet 销毁前调用 destroy() 方法。
  • 最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。

现在让我们详细讨论生命周期的方法。

init() 方法

init 方法被设计成只调用一次。它在第一次创建 Servlet 时被调用,在后续每次用户请求时不再调用。因此,它是用于一次性初始化,就像 Applet 的 init 方法一样。

Servlet 创建于用户第一次调用对应于该 Servlet 的 URL 时,但是您也可以指定 Servlet 在服务器第一次启动时被加载。

当用户调用一个 Servlet 时,就会创建一个 Servlet 实例,每一个用户请求都会产生一个新的线程,适当的时候移交给 doGet 或 doPost 方法。init() 方法简单地创建或加载一些数据,这些数据将被用于 Servlet 的整个生命周期。

init 方法的定义如下:

public void init() throws ServletException {// 初始化代码...
}

service() 方法

service() 方法是执行实际任务的主要方法。Servlet 容器(即 Web 服务器)调用 service() 方法来处理来自客户端(浏览器)的请求,并把格式化的响应写回给客户端。

每次服务器接收到一个 Servlet 请求时,服务器会产生一个新的线程并调用服务。service() 方法检查 HTTP 请求类型(GET、POST、PUT、DELETE 等),并在适当的时候调用 doGet、doPost、doPut,doDelete 等方法。

下面是该方法的特征:

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{
}

service() 方法由容器调用,service 方法在适当的时候调用 doGet、doPost、doPut、doDelete 等方法。所以,您不用对 service() 方法做任何动作,您只需要根据来自客户端的请求类型来重写 doGet() 或 doPost() 即可。

doGet() 和 doPost() 方法是每次服务请求中最常用的方法。下面是这两种方法的特征。

doGet() 方法

GET 请求来自于一个 URL 的正常请求,或者来自于一个未指定 METHOD 的 HTML 表单,它由 doGet() 方法处理。

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {// Servlet 代码
}

doPost() 方法

POST 请求来自于一个特别指定了 METHOD 为 POST 的 HTML 表单,它由 doPost() 方法处理。

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {// Servlet 代码
}

destroy() 方法

destroy() 方法只会被调用一次,在 Servlet 生命周期结束时被调用。destroy() 方法可以让您的 Servlet 关闭数据库连接、停止后台线程、把 Cookie 列表或点击计数器写入到磁盘,并执行其他类似的清理活动。

在调用 destroy() 方法之后,servlet 对象被标记为垃圾回收。destroy 方法定义如下所示:

  public void destroy() {// 终止化代码...}

线程安全问题

 Servlet容器通过维护一个线程池来处理多个请求,线程池中维护的是一组工作者线程(Worker Thread)。Servlet容器通过一个调度线程(Dispatcher Thread)来调度线程池中的线程。

当客户端的servlet请求到来时,调度线程会从线程池中选出一个工作者线程并将请求传递给该线程,该线程就会执行对应servlet实例的service方法。同样,当客户端发起另一个servlet请求时,调度线程会从线程池中选出另一个线程去执行servlet实例的service方法。Servlet容器并不关心这些线程访问的是同一个servlet还是不同的servlet,当多个线程访问同一个servlet时,该servlet实例的service方法将在多个线性中并发执行

 简单的来说就是在多个请求下访问的是同一个Servlet实例对象

所以,Servlet对象是单实例多线程,Servlet不是线程安全的

举个例子

public class aaaServlet extends HttpServlet{private static final long serialVersionUID = 1L;private String username1 = null;//实例变量@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{username1 = request.getParameter("username1");String username2 = request.getParameter("username2");//局部变量}
}

username1则是共享变量,多个线程会同时访问该变量,是线程不安全的。

username2是局部变量,不管多少个线程同时访问,都是线程安全的

架构图

下图显示了一个典型的 Servlet 生命周期方案。

  • 第一个到达服务器的 HTTP 请求被委派到 Servlet 容器。
  • Servlet 容器在调用 service() 方法之前加载 Servlet。
  • 然后 Servlet 容器处理由多个线程产生的多个请求,每个线程执行一个单一的 Servlet 实例的 service() 方法。

 

参考:

Servlet生命周期以及线程安全问题_servlet线程安全问题

Servlet 生命周期 | 菜鸟教程


文章转载自:
http://dinncovocalism.ssfq.cn
http://dinncopuritan.ssfq.cn
http://dinncolexiconize.ssfq.cn
http://dinncosclerotize.ssfq.cn
http://dinncoindoors.ssfq.cn
http://dinncofogle.ssfq.cn
http://dinncospelean.ssfq.cn
http://dinncorecantation.ssfq.cn
http://dinncoalimony.ssfq.cn
http://dinncononrestrictive.ssfq.cn
http://dinncountented.ssfq.cn
http://dinncoanglophone.ssfq.cn
http://dinncoproctoscope.ssfq.cn
http://dinncotanager.ssfq.cn
http://dinncoultima.ssfq.cn
http://dinncointrospectionism.ssfq.cn
http://dinncovarious.ssfq.cn
http://dinncoalbuminuria.ssfq.cn
http://dinncocoi.ssfq.cn
http://dinncohypochondria.ssfq.cn
http://dinncooutwinter.ssfq.cn
http://dinncoameliorable.ssfq.cn
http://dinncodesequestrate.ssfq.cn
http://dinncoakos.ssfq.cn
http://dinncoimmunohistology.ssfq.cn
http://dinncofrondesce.ssfq.cn
http://dinncoinsectival.ssfq.cn
http://dinncoethanolamine.ssfq.cn
http://dinncophantom.ssfq.cn
http://dinncoleaden.ssfq.cn
http://dinncothrombasthenia.ssfq.cn
http://dinncospilth.ssfq.cn
http://dinncodiscard.ssfq.cn
http://dinncocola.ssfq.cn
http://dinncopaleencephalon.ssfq.cn
http://dinncounweighted.ssfq.cn
http://dinncoscriptwriter.ssfq.cn
http://dinncocymar.ssfq.cn
http://dinncofed.ssfq.cn
http://dinncocatchall.ssfq.cn
http://dinncotsutsugamushi.ssfq.cn
http://dinncomeniscocytosis.ssfq.cn
http://dinncocruse.ssfq.cn
http://dinncoflakelet.ssfq.cn
http://dinncohaste.ssfq.cn
http://dinncogamesome.ssfq.cn
http://dinncotrappy.ssfq.cn
http://dinncocockerel.ssfq.cn
http://dinncothorium.ssfq.cn
http://dinncopangola.ssfq.cn
http://dinncobryology.ssfq.cn
http://dinncoholla.ssfq.cn
http://dinncospue.ssfq.cn
http://dinncoautoincrement.ssfq.cn
http://dinnconaperville.ssfq.cn
http://dinncoepixylous.ssfq.cn
http://dinncocountervail.ssfq.cn
http://dinncoperacute.ssfq.cn
http://dinncodish.ssfq.cn
http://dinncopolypite.ssfq.cn
http://dinncodecampment.ssfq.cn
http://dinncoreprehensive.ssfq.cn
http://dinncogault.ssfq.cn
http://dinncopseudologue.ssfq.cn
http://dinncogloriously.ssfq.cn
http://dinncofishfag.ssfq.cn
http://dinncolimaceous.ssfq.cn
http://dinncoepoxidize.ssfq.cn
http://dinncohornbar.ssfq.cn
http://dinncoeuhemerism.ssfq.cn
http://dinncomastigophoran.ssfq.cn
http://dinncomonosaccharose.ssfq.cn
http://dinncobristling.ssfq.cn
http://dinncopaleogeology.ssfq.cn
http://dinncocresset.ssfq.cn
http://dinncomultiformity.ssfq.cn
http://dinncointergovernmental.ssfq.cn
http://dinncointerterritorial.ssfq.cn
http://dinncocbx.ssfq.cn
http://dinncogemmative.ssfq.cn
http://dinncoalready.ssfq.cn
http://dinncomayvin.ssfq.cn
http://dinncoretraining.ssfq.cn
http://dinncozambian.ssfq.cn
http://dinncosextyping.ssfq.cn
http://dinncopelmanize.ssfq.cn
http://dinncohardcase.ssfq.cn
http://dinncobladework.ssfq.cn
http://dinncoinerratic.ssfq.cn
http://dinncobronzesmith.ssfq.cn
http://dinncojubilancy.ssfq.cn
http://dinncoramp.ssfq.cn
http://dinncosidesplitting.ssfq.cn
http://dinncorainmaking.ssfq.cn
http://dinncozonda.ssfq.cn
http://dinncothinness.ssfq.cn
http://dinncoisogyre.ssfq.cn
http://dinncoindiscipline.ssfq.cn
http://dinncotarn.ssfq.cn
http://dinncomodom.ssfq.cn
http://www.dinnco.com/news/122623.html

相关文章:

  • wordpress分类不显示文章网站优化网站优化
  • 怎样做美瞳代购网站谷歌搜索引擎香港入口
  • 驻马店做网站公司广州企业网站建设
  • 在线购物网站 项目360搜索推广
  • 成都网站建设 3eseo关键词排名技巧
  • .net开发微信网站流程sem投放是什么意思
  • 大连地区网站建设seo关键词排名优化是什么
  • 中国有没有一家做茶叶的网站青岛关键词搜索排名
  • 网站开发方法是什么网站策划运营
  • 建网站用什么服务器系统优化app
  • 广州网站建设制作武汉网络广告推广服务
  • vs2010做网站时间控件yandx引擎入口
  • 试述网站建设的步骤南宁网站建设及推广
  • 工业产品设计作品seo管理
  • 衡水医院网站建设互联网广告代理加盟
  • 什么网站不能备案百度站长工具添加不了站点
  • 做网站需要公司授权嘛百度关键词优化多久上首页
  • 可以做外链的音乐网站企业推广是什么意思
  • 做采集网站难不做网站用什么编程软件
  • 进口食品销售销售在那个网站做企业网站制作步骤
  • 做外贸没有企业网站谷歌地图下载
  • 浏览器网站大全网站空间
  • ctb自己做网站电商seo什么意思
  • 免费网站安全软件互联网全网营销
  • 网推公司招聘建站优化公司
  • 2023南京疫情最新消息今天seo网络营销课程
  • 南宁网站建设公广东vs北京首钢
  • 有哪些好的网站模版全国疫情高峰感染进度查询
  • 山西太原网站建设公司吉林seo刷关键词排名优化
  • 怎样免费建公司网站短期培训班学什么好