import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloServlet implements Servlet {
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy()");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
System.out.println("getServletConfig()");
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
System.out.println("getServletInfo()");
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init(ServletConfig arg0)");
String servletName=arg0.getServletName();
System.out.println(servletName);
// 获取<servlet>的属性
System.out.println("获取<servlet>的属性user:"+arg0.getInitParameter("user"));
System.out.println("获取<servlet>的属性password:"+arg0.getInitParameter("password"));
Enumeration<String> names= arg0.getInitParameterNames();
while(names.hasMoreElements()){
String name=names.nextElement();
String value=arg0.getInitParameter(name);
System.out.println(name+"="+value);
}
ServletContext srvContext=arg0.getServletContext();
// 获取应用的名称:
System.out.println("获取应用的名称:"+srvContext.getServletContextName());
// 获取应用的路径。
System.out.println("获取应用的路径"+srvContext.getContextPath());
Enumeration con= srvContext.getAttributeNames();
while(con.hasMoreElements()){
Object name=con.nextElement();
System.out.println("getAttributeName:"+name);
}
// 获取<context-param>的属性 ,应用的全局属性。 相对于<servlet>里面的<init-param>是全局属性。
Enumeration<String> paraname=srvContext.getInitParameterNames();
while(paraname.hasMoreElements()){
String pname=paraname.nextElement();
String value=srvContext.getInitParameter(pname);
System.out.println("获取<context-param>的属性pname="+pname+" 获取<context-param>的属性value="+value);
}
String contextPath=srvContext.getContextPath();
System.out.println("contextPath:"+contextPath);
// getRealPath,获取文件的相对发布路径。
String realPath=srvContext.getRealPath("/hello.jsp");
System.out.println(realPath);
// F:\apache-tomcat-9\webapps\tomcatDemo2\hello.jsp 这个实际显示的PATH是,这个WEB应用发布时的PATH。
// 物理的原PATH为:D:\workspace\tomcatDemo2\WebContent\hello.jsp.
// 默认的Class path下,得到IS流
getIS("jdbc.properties");
// 使用ServletContext得到IS,必须要注意,这个PATH必须是对发布路径的路径。
contextGetIS(srvContext,"/WEB-INF/classes/jdbc.properties");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
System.out.println("service(ServletRequest arg0, ServletResponse arg1)");
// TODO Auto-generated method stub
}
public HelloServlet(){
System.out.println("HelloServlet()");
}
public InputStream getIS(String path){
InputStream is=this.getClass().getClassLoader().getResourceAsStream(path);
System.out.println(is);
return is;
}
public InputStream contextGetIS(ServletContext srvContext,String path){
InputStream is=srvContext.getResourceAsStream(path);
System.out.println("contextGetIS:"+is);
return is;
}
}