当前位置:首页>开发>正文

Servlet都有哪些方法主要作用是什么 servlet如何使用

2023-06-06 23:02:42 互联网 未知 开发

 Servlet都有哪些方法主要作用是什么 servlet如何使用

Servlet都有哪些方法?主要作用是什么?

HttpServlet 类包含 init() 、 destroy() 、 service() 等方法。其中 init() 和 destroy() 方法是继承的。
(1) init() 方法
在 Servlet 的生命期中,仅执行一次 init() 方法。它是在服务器装入 Servlet 时执行的。 可以配置服务器,以在启动服务器或客户机首次访问 Servlet 时装入 Servlet 。 无论有多少客户机访问 Servlet ,都不会重复执行 init() 。
缺省的 init() 方法通常是符合要求的,但也可以用定制 init() 方法来覆盖它,典型的是管理服务器端资源。 例如,可能编写一个定制 init() 来只用于一次装入 GIF 图像,改进 Servlet 返回 GIF 图像和含有多个客户机请求的性能。另一个示例是初始化数据库连接。缺省的 init() 方法设置了 Servlet 的初始化参数,并用它的 ServletConfig 对象参数来启动配置, 因此所有覆盖 init() 方法的 Servlet 应调用 super.init() 以确保仍然执行这些任务。在调用 service() 方法之前,应确保已完成了 init() 方法。
(2) service() 方法
service() 方法是 Servlet 的核心。每当一个客户请求一个 HttpServlet 对象,该对象的 service() 方法就要被调用,而且传递给这个方法一个“请求”( ServletRequest )对象和一个“响应”( ServletResponse )对象作为参数。 在 HttpServlet 中已存在 service() 方法。缺省的服务功能是调用与 HTTP 请求的方法相应的 do 功能。例如, 如果 HTTP 请求方法为 GET ,则缺省情况下就调用 doGet() 。 Servlet 应该为 Servlet 支持的 HTTP 方法覆盖 do 功能。因为 HttpServlet.service() 方法会检查请求方法是否调用了适当的处理方法,不必要覆盖 service() 方法。只需覆盖相应的 do 方法就可以了。
= 当一个客户通过 HTML 表单发出一个 HTTP POST 请求时, doPost ()方法被调用。 与 POST 请求相关的参数作为一个单独的 HTTP 请求从浏览器发送到服务器。当需要修改服务器端的数据时,应该使用 doPost() 方法。
= 当一个客户通过 HTML 表单发出一个 HTTP GET 请求或直接请求一个 URL 时, doGet() 方法被调用。 与 GET 请求相关的参数添加到 URL 的后面,并与这个请求一起发送。当不会修改服务器端的数据时,应该使用 doGet() 方法。
Servlet 的响应可以是下列几种类型:
一个输出流,浏览器根据它的内容类型(如 text/HTML )进行解释。
一个 HTTP 错误响应 , 重定向到另一个 URL 、 servlet 、 JSP 。
(3) destroy() 方法
destroy() 方法仅执行一次,即在服务器停止且卸装 Servlet 时执行该方法。典型的,将 Servlet 作为服务器进程的一部分来关闭。缺省的 destroy() 方法通常是符合要求的,但也可以覆盖它,典型的是管理服务器端资源。例如,如果 Servlet 在运行时会累计统计数据,则可以编写一个 destroy() 方法,该方法用于在未装入 Servlet 时将统计数字保存在文件中。另一个示例是关闭数据库连接。
当服务器卸装 Servlet 时,将在所有 service() 方法调用完成后,或在指定的时间间隔过后调用 destroy() 方法。一个 Servlet 在运行 service() 方法时可能会产生其它的线程,因此请确认在调用 destroy() 方法时,这些线程已终止或完成。
(4) GetServletConfig()方法
GetServletConfig ()方法返回一个 ServletConfig 对象,该对象用来返回初始化参数和 ServletContext 。 ServletContext 接口提供有关 servlet 的环境信息。
(5) GetServletInfo()方法
GetServletInfo ()方法是一个可选的方法,它提供有关 servlet 的信息,如作者、版本、版权。
当服务器调用 sevlet 的 Service ()、 doGet ()和 doPost ()这三个方法时,均需要 “请求”和“响应”对象作为参数。“请求”对象提供有关请求的信息,而“响应”对象提供了一个将响应信息返回给浏览器的一个通信途径。 javax.servlet 软件包中的相关类为 ServletResponse 和 ServletRequest ,而 javax.servlet.http 软件包中的相关类为 HttpServletRequest 和 HttpServletResponse 。 Servlet 通过这些对象与服务器通信并最终与客户机通信。 Servlet 能通过调用“请求”对象的方法获知客户机环境,服务器环境的信息和所有由客户机提供的信息。 Servlet 可以调用“响应”对象的方法发送响应,该响应是准备发回客户机的。

servlet如何使用?


什么是Servlet?
① Servlet就是JAVA 类
② Servlet是一个继承HttpServlet类的类
③ 这个在服务器端运行,用以处理客户端的请求

Servlet使用
--Servlet生命周期就是指创建Servlet实例后,存在的时间以及何时销毁的整个过程.
--Servlet生命周期有三个方法
  init()方法:
  service()方法:Dispatches client requests to the protected service method 
  destroy()方法:Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
--Servlet生命周期的各个阶段
  ----实例化:Servlet容器创建Servlet实例
  ----初始化:调用init()方法
  ----服务:如果有请求,调用service()方法
  ----销毁:销毁实例前调用destroy()方法
  ----垃圾收集:销毁实例

servlet request请求对象常用方法总结

1. request请求对象常用方法:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html charset = utf-8")
this.response = response
out = this.response.getWriter()
println("

    ")
    //1. 获取请求方式、处理乱码问题
    String method = request.getMethod()

    //servletRequest中的方法
    request.setCharacterEncoding("utf-8")
    //1. 获取请求体的编码方式
    String characterEncoding = request.getCharacterEncoding()
    println("getCharacterEncoding = " characterEncoding)

    //2. get body length
    int contentLength = request.getContentLength()
    println("getContentLength = " contentLength)

    //3. MIME type
    String mimeType = request.getContentType()
    println("getContentType = " mimeType)

    //4. 接收请求的接口的 Internet Protocol (IP) 地址
    String ip = request.getLocalAddr()
    println("getLocalAddr = " ip)

    //5. 基于 Accept-Language 头,返回客户端将用来接受内容的首选 Locale 客户端语言环境
    Locale locale = request.getLocale()
    println("getLocale = " locale)

    //6. 所有的语言环境
    Enumeration locales = request.getLocales()
    while(locales.hasMoreElements()){
    Locale temp = locales.nextElement()
    println(" Locales = " temp)
    }

    servlet之间的通信方法有哪些?如何具体实现?

    以下是几种常调用的方法
    Servlet to Servlet Communication

    Listing 1: ServletBase
    public class ServletBase extends HttpServlet{
    static Connection databaseConnection = null
    public void init(ServletConfig _config) throws
    ServletException{
    super.init(_config)
    if ( databaseConnection == null )
    //- Open up the database connection
    }
    protected boolean isLoggedOn( String _username ){
    return true
    }
    protected boolean logUserOn( String _username ){
    return true
    }
    }
    Listing 2: Using the NewSerletBase Class
    public class logonServlet extends ServletBase{
    public void service(HttpServletRequest _req, HttpServletRe-
    sponse _res) throws ServletException{
    if ( isLoggedOn( _req.getParameter(襏SERNAME? ){
    //- Display a message indicating they are already logged on
    }else{
    logUserOn( _req.getParameter(襏SERNAME? )
    }
    }
    }
    Listing 3: Storing an Object
    public class logonServlet extends HttpServlet{
    public void service(HttpServletRequest _req, HttpServletRe-
    sponse _res) throws ServletException{
    ServletContext thisContext = getServletContext()
    //-- Assume some method creates a new connection class
    Connection newConnection = createConnection()
    thisContext.setAttribute( database.connection? newConnection
    )
    //-- Return some output to the client
    }
    }
    Listing 4: retrieving an Object
    public class logoffServlet extends HttpServlet{
    public void service(HttpServletRequest _req, HttpServletRe-
    sponse _res) throws ServletException{
    ServletContext thisContext = getServletContext()
    //-- Assume some method creates a new connection class
    Connection newConnection = thisContext.getAttribute(
    database.connection?
    if ( newConnection == null )
    //- Database has not been opened yet
    //-- Return some output to the client
    }
    }
    Listing 5: Looking at All the Objects
    public class allServlet extends HttpServlet{
    public void service(HttpServletRequest _req, HttpServletRe-
    sponse _res) throws ServletException{
    ServletContext thisContext = getServletContext()
    //-- Assume some method creates a new Connection class
    Enumeration E = thisContext.getAttributeNames()
    while ( E.hasMoreElements() ){
    String name = (String)E.nextElement()
    System.out.println( "Object: " name )
    }
    }
    }
    Listing 6: Retrieving Remote Contexts
    public class otherServlet extends HttpServlet{
    public void service(HttpServletRequest _req, HttpServletRe-
    sponse _res) throws ServletException{
    ServletContext otherContext =
    getServletContext(http:///servlet/allServlet?
    //-- Assume some method creates a new Connection class
    Enumeration E = otherContext.getAttributeNames()
    while ( E.hasMoreElements() ){
    String name = (String)E.nextElement()
    System.out.println( "Object: " name )
    }
    }
    }
    Listing 7: Forwarding a Request
    public class forwardServlet extends HttpServlet{
    public void service(HttpServletRequest _req, HttpServletRe-
    sponse _res) throws ServletException{
    ServletContext xt = getServletContext()
    RequestDispatcher xyzServlet =
    xt.getRequestDispatcher(http:///servlet/xyzServlet?
    //- Do any preliminary processing
    _req.setAttribute( database.results? new Results() )
    xyzServlet.forward( _req, _res )
    }
    }
    Listing 8: Inserting Content
    public class insertServlet extends HttpServlet{
    public void service(HttpServletRequest _req, HttpServletRe-
    sponse _res) throws ServletException{
    ServletContext xt = getServletContext()
    RequestDispatcher xyzServlet =
    xt.getRequestDispatcher(http:///servlet/xyzServlet?
    PrintWriter Out = _res.getWriter()
    Out.println( this is from the insertServlet ?)
    for(int x=0 x < 10 x )
    xyzServlet.insert( _req, _res )
    Out.println( this is the end of the print servlet ?)
    }
    }

    /////////////////////////////////////////
    forward方法是把请求的内容转发到另外的一个servlet.而include是把另一个servlet处理过后的内容拿过来.
    举例来说比如在servlet1打一句out.print("1111"),servlet2打上out.print("22222"),在servlet1中用forward命令会转到servlet2中,显示22222.
    而在servlet1中使用include方法会依然在servlet1的页面中,但是在1111后打出22222

最新文章