张志宇 SERVELET JSP学习讲义-07_SERVLET为什么有2个init方法

package com.zzk.cn;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestInitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=GBK");     
		PrintWriter out = resp.getWriter();
		
		out.println(this.getServletConfig().getServletName());

		// out.println(this.getServletConfig().getInitParameter("name"));

//		java.util.Enumeration params = this.getServletConfig()
//				.getInitParameterNames();   
//		for (; params.hasMoreElements();) {
//			String name = params.nextElement().toString();
//			String value = this.getServletConfig().getInitParameter(name);
//			value = new String(value.getBytes("iso8859-1"),"GBK");
//			out.println(value);
//		}
		out.close();  
	}
}

然后web.xml这么配置:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  


    <servlet>
      <servlet-name>Myfirstservlet</servlet-name>
      <servlet-class>com.zzk.cn.HelloWorldServlet</servlet-class>
    </servlet>
    

    <servlet-mapping>
        <servlet-name>Myfirstservlet</servlet-name>
        <url-pattern>/HelloWorldServlet</url-pattern>
    </servlet-mapping>
    
	
	    <servlet>
      <servlet-name>TestLifeCycleServlet</servlet-name>
      <servlet-class>com.zzk.cn.TestLifeCycleServlet</servlet-class>
    </servlet>
    

    <servlet-mapping>
        <servlet-name>TestLifeCycleServlet</servlet-name>
        <url-pattern>/TestLifeCycleServlet</url-pattern>
    </servlet-mapping>
	
	
		    <servlet>
      <servlet-name>TestInitServlet</servlet-name>
      <servlet-class>com.zzk.cn.TestInitServlet</servlet-class>
    </servlet>
    

    <servlet-mapping>
        <servlet-name>TestInitServlet</servlet-name>
        <url-pattern>/TestInitServlet</url-pattern>
    </servlet-mapping>

</web-app>


运行tomcat,在tomcat页面中显示的是

TestInitServlet 

这里输出的是
      <servlet-name>TestInitServlet</servlet-name>



继续编辑

package com.zzk.cn;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestInitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=GBK");     
		PrintWriter out = resp.getWriter();
		
		out.println(this.getServletConfig().getServletName());

		out.println(this.getServletConfig().getInitParameter("name"));
		out.println(this.getServletConfig().getInitParameter("gender"));

//		java.util.Enumeration params = this.getServletConfig()
//				.getInitParameterNames();   
//		for (; params.hasMoreElements();) {
//			String name = params.nextElement().toString();
//			String value = this.getServletConfig().getInitParameter(name);
//			value = new String(value.getBytes("iso8859-1"),"GBK");
//			out.println(value);
//		}
		out.close();  
	}
}

web.xml修改为英文(中文有乱码问题)

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  


    <servlet>
      <servlet-name>Myfirstservlet</servlet-name>
      <servlet-class>com.zzk.cn.HelloWorldServlet</servlet-class>
    </servlet>
    

    <servlet-mapping>
        <servlet-name>Myfirstservlet</servlet-name>
        <url-pattern>/HelloWorldServlet</url-pattern>
    </servlet-mapping>
    
	
	    <servlet>
      <servlet-name>TestLifeCycleServlet</servlet-name>
      <servlet-class>com.zzk.cn.TestLifeCycleServlet</servlet-class>
    </servlet>
    

    <servlet-mapping>
        <servlet-name>TestLifeCycleServlet</servlet-name>
        <url-pattern>/TestLifeCycleServlet</url-pattern>
    </servlet-mapping>
	
	
		    <servlet>
      <servlet-name>TestInitServlet</servlet-name>
      <servlet-class>com.zzk.cn.TestInitServlet</servlet-class>
      <init-param>
	      <param-name>name</param-name>
		  <param-value>Tom</param-value>
	  </init-param>
	  
	       <init-param>
	      <param-name>gender</param-name>
		  <param-value>man</param-value>
	  </init-param>
	</servlet>
    

    <servlet-mapping>
        <servlet-name>TestInitServlet</servlet-name>
        <url-pattern>/TestInitServlet</url-pattern>
    </servlet-mapping>

</web-app>

输出:

TestInitServlet Tom man 


继续修改:循环输出:

package com.zzk.cn;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestInitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=GBK");     
		PrintWriter out = resp.getWriter();
		
		out.println(this.getServletConfig().getServletName());

		out.println(this.getServletConfig().getInitParameter("name"));
		out.println(this.getServletConfig().getInitParameter("gender"));

		java.util.Enumeration params = this.getServletConfig()
				.getInitParameterNames();   
		for (; params.hasMoreElements();) {
			String name = params.nextElement().toString();
			String value = this.getServletConfig().getInitParameter(name);
			value = new String(value.getBytes("iso8859-1"),"GBK");
			out.println(value);
		}
		out.close();  
	}
}

输出结果:

TestInitServlet Tom man Tom man 


为什么拥有两个init()

³init(servletconfig config)
²this.config = config
²init();
²重写必须得super.init(config)
±不然不能使用this.getServletConfig
±当然也就不能用getInitParameter();
³我们只需要重写init();


防止程序员调用第一个,而忘记使用super




你可能感兴趣的:(张志宇 SERVELET JSP学习讲义-07_SERVLET为什么有2个init方法)