The use of servlet

Servlet is a java code run in the server such as Tomcat.

How to create a servlet?

  1.Create a class extends HttpServlet (or implements the Servlet interface or extends the GenericServlet)

  2.Override the doPost and doGet (if implements the Servlet interface , you should override all the methods : init() service() getServletConfig() getServletInfo() destroy())

  3.Configure the Servlet in the web.xml.

The structure of Servlet:

  javax.servlet.Servlet interface

    ----->  javax.servlet.Genericservlet abstract class

      ------> javax.servlet.http.HttpServlet

        ------> the class extends or implements xxx you defined by yourself

The life-cricle of servlet:

  1.create the servlet and call the init() to initialize the servlet.

  2.process the request via service().

  3.destroy the servlet via destroy() and the garbage collect the servlet.

When we create the serlet , the first access to the servlet will create the object of servlet constructor , then call the init() method . But the init() will be called once.

Importantly , there will start a thread to call the service() . Because the web apps are multi-thread application.

When we access to the server, there will open a new thread to call the service.

The servlet object usually created once and it stays at the memory for a long time.

When the server stop or the servlet object was destroyed the destroy() method will be called.

Notice:

  if you create the member variables in servlet , may lead to the secure problems.

  So, we should avoid to create member variables in servlet. If must, we should synchronize it.

About doGet() and doPost():

  According to the url, we can find the servlet from the web.xml to call the service(). The service() in HttpServlet is extended from GenericServlet and includes doGet() and doPost().

  Tell from the method name, the service could tell which method to use. And here involves a design pattern: Template method model.

                    

时间: 2024-08-10 01:59:51

The use of servlet的相关文章

Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not foun

一段时间没亲自建新项目玩乐,今天建立了一Maven project的时候发现了以下异常,Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path index.jsp /easyBuy/src/main/webapp line 1 JSP Problem 经过查找原因,原来是因为忘记设置server

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --

Java web之servlet

入坑必备之servlet(O(∩_∩)O哈!) 两个问题:是什么?怎么用? the first question:what?       Servlet是sun公司提供的一门用于开发动态web资源的技术,sun公司在其API中提供了一个servlet接口.由此可以理解为原生的servlet是一个接口,提到接口,我们应该想道我们必须去实现它才能被我们使用,servlet这个接口当然也不例外,从概念上讲,servlet是指sun公司提供的这个API接口,约定俗称,现在我们说的servlet是指实现这

JavaWeb之Java Servlet完全教程(转)

Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servlet必须部署在Java servlet容器才能使用.虽然很多开发者都使用Java Server Pages(JSP)和Java Server Faces(JSF)等Servlet框架,但是这些技术都要在幕后通过Servlet容器把页面编译为Java Servlet.也就是说,了解Java Servle

servlet理解

一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向浏览器输出数据),需要完成以下2个步骤: 1.编写一个Java类,实现servlet接口. 2.把开发好的Java类部署到web服务器中. 按照一种约定俗成的称呼习惯,通常我们也把实现了servlet接口的java程序,称之为Servlet 二.Servlet的运行过程 Servlet程序是由WEB

web.xml 中的listener、filter、servlet加载及一些配置

在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> filter -> servlet 同时还存在着这样一种配置节:context-param,它用于向 Servlet

Servlet简介与生命周期

转载请注明原文地址: 一:Servlet是什么 Servlet是运行在Web服务器上的Java程序,作为处理来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层.JSP在web服务器上要先转换成servlet,然后才能在JVM运行,并把结果拼接成浏览器可识别的文件(如html)传回浏览器显示. 二:Servlet的应用场景 单纯地对客户端的请求做处理时,如果我们用纯JSP文件(即:只有Java语句)来处理的话,还需要先转换为servlet才能运行

Java—Servlet开发

掌握Servlet API是Java Web的基础. 首先新建一个Web类型的工程,然后再创建Servlet类.而且Servlet类名,要以Servlet作为后缀.--Servlet技术,就在Servlet类中. 然后要运行Servlet,就需要一个容器(JSP也需要),如开源的Tomcat. Tomcat的配置: 下载:

Servlet的生命周期

Servlet的生命周期是由Servlet的容器来控制的,它可以分为三个阶段:初始化.运行.销毁1.初始化阶段:(1)Servlet容器加载Servlet类,把Servlet类的.class文件中数据读到内存中:(2)然后Servlet容器创建一个ServletConfig对象.ServletConfig对象包含了Servlet的初始化配置信息:(3)Servlet容器创建一个Servlet对象:(4)Servlet容器调用Servlet对象的init方法进行初始化.2.运行阶段当Servlet

Servlet入门

1.在tomcat中新建一个day01web应用,然后在web应用中新建一个web-inf/classes目录:2.在classes目录中新建一个FirstServlet.java文件:package cn.itcastimport java.io.*;import javax.servlet.*;public class Firstservlet extends GenericServlet{ public void service(ServletRequest req, ServletRes