Servlet初始化研究

instantiate   实例化 instantiate an object实例化对象;
demonstrate 示范,证明,论证;
retrieve 重新得到,找回;
ex: This example demonstrates a servlet that retrieves some initialization parameters in its init() method;
以前一直用框架开发,回过头来又看了一下Servlet,感觉收获挺多的。
Servlet:

import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class MainServlet extends HttpServlet {

                     
public MainServlet() {
        
super();
    }

    
public void init() throws ServletException {
        System.out.println(
".........initiating the MainServlet............");
        System.out.println(
"The servlet context is [" + getServletContext() + "]root is[" + getServletContext().getRealPath("/"+"]");
        
        
//getServletConfig();
        System.out.println("context[" + this.getServletContext().getServletContextName() +  "]");
        
        System.out.println(
"Application Name[" + getServletConfig().getServletContext().getServletContextName() + "=====" + getServletConfig().getServletName() + "]");
        System.out.println(
"init parameter param1[" + getServletConfig().getInitParameter("param1"+ "]");
        System.out.println(
"init parameter param2[" + getServletConfig().getInitParameter("param2"+ "]");
        
        String resource 
= "/WEB-INF/web.xml";
        
try{
            URL url 
= getServletContext().getResource(resource);
            System.out.println(
"===========WEBINF/web.xml路径=============");
            System.out.println(url.toString());
        }
catch (Exception e) {
            e.printStackTrace();
        }

     }

}

这是我写的一个Servlet,此Servlet中没有doGet,doPost方法,只用init方法去初始化servlet。
web.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns
="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
  
<servlet>
    
<servlet-name>MainServlet</servlet-name>
    
<servlet-class>com.wes.controller.MainServlet</servlet-class>
    
<init-param>
       
<param-name>param1</param-name>
       
<param-value>11111111</param-value>
    
</init-param>
    
<init-param>
       
<param-name>param2</param-name>
       
<param-value>sssss</param-value>
    
</init-param>
    
<load-on-startup>0</load-on-startup>
 
</servlet>
  
<servlet-mapping>
    
<servlet-name>MainServlet</servlet-name>
    
<url-pattern>*.do</url-pattern>
  
</servlet-mapping>
</web-app>

在这个文件中配置了servlet,指定了两个初始化参数,并且配置了load-on-startup,使其在服务器启动时初始化。
这样在服务器启动时输出如下:

.........initiating the MainServlet............
The servlet context is [org
.apache.catalina.core.ApplicationContextFacade@9fa8f]root is[D:Tomcat5webappsfeiliu]
context[null]
Application Name[null
=====MainServlet]
init parameter param1[
11111111]
init parameter param2[sssss]
===========WEBINF/web.xml路径=============

在这里有几点没闹明白:
1.getServletContext().getServletContextName()此方法按说应该输出应用的名字,但在这里输出的是null。
2.getServletContext().getInitParameter("param1")按说这个方法应该能得到param1的初始值,但我试了试得到null。
不知道为什么会出这种问题,希望有看到的人能帮忙解答一下。

 

 

 

 

 


 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值