servlet的生命周期,通配符,servletconfigue,servletcontext

(1)




@WebServlet("/MyFirstServlet")
public class MyFirstServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
         /**
          * 处理post请求
          */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用doGet
doGet(request, response);

}
     /**
      * 初始化      (只执行一次)               
      */
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init();



}

  /**
   * 实例化  (只执行一次)         
   */
public MyFirstServlet() {

}

/**
* 对外服务,也就是浏览器
* 注意,是在service中进行判断分发get请求还是post请求
* 一般情况下是不会重写service的,一般情况下只会重写doget与dopost方法,destroy,service,MyFirstServlet,init一般不会重写,
* 这里写出来只是进行了解用
*/
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(arg0, arg1);

}

/**
* 销毁
*/
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}

}


(2)通配符


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Servlet</display-name>
  
  <servlet>
       <servlet-name>MySecondServlet</servlet-name>
       <servlet-class>com.example.test.controller.MySecondServlet</servlet-class>
       
  </servlet>
  
    
  
  <servlet-mapping>
       <servlet-name>MySecondServlet</servlet-name>
       <url-pattern>/servlet/MySecondServlet</url-pattern>
       
  </servlet-mapping>
  <!-- 第一种通配符:/*,配置一个通配符 ,/servlet/*,只要前面一样,那么*部分无论是什么都可以访问MySecondServlet-->
  <servlet-mapping>
       <servlet-name>MySecondServlet</servlet-name>
       <url-pattern>/servlet/*</url-pattern>
       
  </servlet-mapping>
  <!-- 第二种通配符形式 .action表示最后以这个为结尾的都会找到这个 MySecondServlet-->
   <servlet-mapping>
       <servlet-name>MySecondServlet</servlet-name>
       <url-pattern>*.action</url-pattern>
       
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


(3)servletconfigue


示例:

在web.xml中的配置信息

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Servlet</display-name>
  
  <servlet>
       <servlet-name>MyFirstServlet</servlet-name>
       <servlet-class>com.example.test.controller.MyFirstServlet</servlet-class>
       <!-- 初始化参数,初始化编码为utf-8 -->
       <init-param>
         <param-name>encode</param-name>
         <param-value>utf-8</param-value>
       </init-param>
       
         <!-- 初始化参数,初始化name为张三 -->
        <init-param>
         <param-name>name</param-name>
         <param-value>张三</param-value>
       </init-param>
       
  </servlet>
  
  <servlet-mapping>
       <servlet-name>MyFirstServlet</servlet-name>
       <url-pattern>/servlet/MyFirstServlet</url-pattern>
       
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在创建的servlet中的代码如下:



@WebServlet("/MyFirstServlet")
public class MyFirstServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
//取出web.xml中的初始化的信息
//获取servletConfig对象
ServletConfig servletConfig=getServletConfig();
String encode = servletConfig.getInitParameter("encode");
System.out.println(encode);
String name = servletConfig.getInitParameter("name");
System.out.println(name);
//下面是一下子都取出来--
Enumeration<String> initParametersNames=servletConfig.getInitParameterNames();
//如果有多个元素,就取出来
while(initParametersNames.hasMoreElements()) {
String key=initParametersNames.nextElement();
String value=servletConfig.getInitParameter(key);
System.out.println(key+"---"+value);

 

}

}
         /**
          * 处理post请求
          */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用doGet
doGet(request, response);

}
}

结果:

utf-8
张三
encode---utf-8
name---张三

注意:但是这样一个一个在web中配置有点麻烦,可以用servletcontext获取配置参数

(3)servletcontext获取配置参数



在web.xml中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Servlet</display-name>
   <context-param>
     <param-name>sex</param-name>
     <param-value>nan</param-value>
   </context-param>
   <context-param>
     <param-name>hobby</param-name>
     <param-value>nv</param-value>
   </context-param>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


在servlet中的代码:



@WebServlet("/MyFirstServlet")
public class MyFirstServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
//取出web.xml中的初始化的信息
//获取servlet的上下文
ServletContext servletContext=getServletContext();
String sex = servletContext.getInitParameter("sex");

String hobby = servletContext.getInitParameter("hobby");
System.out.println(sex+"----"+hobby);
//下面是一下子都取出来--
Enumeration<String> initParametersNames=servletContext.getInitParameterNames();
//如果有多个元素,就取出来
while(initParametersNames.hasMoreElements()) {
String key=initParametersNames.nextElement();
String value=servletContext.getInitParameter(key);
System.out.println(key+"---"+value);

 

}

}
         /**
          * 处理post请求
          */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用doGet
doGet(request, response);

}
}




阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页