Servlet init

我们从常用的HttpServlet来看。


HttpServlet
extends: GenericServlet
All Implemented Interfaces: java.io.Serializable, Servlet, ServletConfig
接口Servlet中定义了init(ServletConfig)方法

init

public void init(ServletConfig config)
          throws ServletException
Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.

The servlet container cannot place the servlet into service if the init method

  1. Throws a ServletException
  2. Does not return within a time period defined by the Web server
Parameters:
config - a ServletConfig object containing the servlet‘s configuration and initialization parameters
Throws:
ServletException - if an exception has occurred that interferes with the servlet‘s normal operation
See Also:
UnavailableException, getServletConfig()

###init(ServletConfig config)方法是由Servlet容器(like Tomcat...)在实例化Servlet之后立马调用的。执行一些初始化的工作。

查看GenericServlet的时候发现会有两个init方法:public void init(ServletConfig config);public void init();

init

public void init(ServletConfig config)
          throws ServletException
Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(javax.servlet.ServletConfig).

This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).

Specified by:
init in interface Servlet
Parameters:
config - the ServletConfig object that contains configutation information for this servlet
Throws:
ServletException - if an exception occurs that interrupts the servlet‘s normal operation
See Also:
UnavailableException

init

public void init()
          throws ServletException
A convenience method which can be overridden so that there‘s no need to call super.init(config).

Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().

Throws:
ServletException - if an exception occurs that interrupts the servlet‘s normal operation

看一眼两个方法的实现

    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        init();
    }

    public void init() throws ServletException {}        

GenericServlet 添加了一个init无参的方法,子类可以重写init()方法,这样就不用执行super(config)方法了,执行该方法的目的就像有参方法中写的那样为this.config赋值,后面调用的时候使用

时间: 2024-12-14 00:43:23

Servlet init的相关文章

javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception

e'cipse中,项目没报错,正常启动,访问控制器时报错 javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invok

项目中访问controller报错:HTTP Status 500 - Servlet.init() for servlet spring threw exception

直接访问controller路径http://localhost:8080/index报错: HTTP Status 500 - Servlet.init() for servlet spring threw exception type Exception report message Servlet.init() for servlet spring threw exception description The server encountered an internal error th

Servlet.init() for servlet [springmvc] threw exception

项目还没开始做,就碰到那么多问题.. 报错一:/oa/news/%E6%A0%8F%E7%9B%AE%E7%AE%A1%E7%90%86.jsp 1.一开始是jsp的页面名称为中文,改了 2.接着仍然这样错误,我想可能没有重新加载,把target文件删掉解决了. 报错二:Artifact tlks: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: E:\

关于springmvc的context:component-scan 标签使用时HTTP Status 500 - Servlet.init() for servlet springmvc threw exception的问题

首先是控制器的配置: 1 package cn.cuibusi.ssm.controller; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework

HTTP Status 500 - Servlet.init() for servlet springmvc threw exception

自己的当前环境是ubuntu14.04 64位,tomtcat7和tomcat8,eclipse luna,然后跑了一直spring的demo项目爆了上述的错误,由于这个项目用的是spring3.2在多次尝试之后都不能跑起来我就猜测这个spring版本太低了,然后就把自己手头的srping4.0的jar包放进去然后项目就跑起来了. 在eclipse下面还报了一个 Failed to read candidate component class 的错误,当更换jar包之后消失了

HTTP Status 500 - Servlet.init() for servlet CXFServlet threw exception

     root cause java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cxf.wsdl.WSDLManager' defined in URL [jar:file:/D:/Program%20Files/apache-tomcat-8.0.21/webapps/PCServer/WE

Servlet.init() for servlet [spring-dispatcher] threw exception

在ssm商铺开发1.0中遇到该问题,之前项目运行正常,在电脑安装jdk1.6之后出现该问题,具体报错如下 解决办法: eclipse中将jdk切换回1.8,在window配置中将相关jdk都切换.是jdk与其他之间的兼容问题.我在切换回之后问题消失,但是隔天再次出现该问题.最后将eclipse重安装,将环境重新搭建(eclipse的环境),最后解决该问题. 很可能是之前eclipse环境搭建混乱,卸载有残留,导致不兼容. 这是本人本次问题解决办法. 原文地址:https://www.cnblog

Servlet中的init()方法如何才会在服务器启动时执行

如果要想让 servlet 的 init () 方法在服务器启动 时就被执行,则需要在 web.xml 中相应的 servlet 下配置 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.

servlet的init()方法启动加载

<!DOCTYPE html> <html> <head> <title>about servlet init</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> /* GitHub stylesheet for