Servlet笔记——(2.5)Servlet方法以及Servlet域小结

来源:互联网 发布:培训机构网站源码 编辑:程序博客网 时间:2024/06/08 02:43

2.5.1 Servlet方法

void init(ServletConfig)

Servlet生命周期相关

void service(ServletRequest, ServletResponse)

void destroy()

ServletConfig getServletConfig()

获取ServletConfig对象

String getServletInfo();

 

2.5.2 ServletConfig方法

String getInitParameter(String)

获取Servlet初始化参数,和<servlet><init-param>XXX</init-param></servlet>对应

Enumeration getInitParameterNames()

 

String getServletName();

 

ServletContext getServletContext()

获取ServletContext对象

2.5.3 GenericServlet方法

实现了Servlet、ServletConfig接口,提供了无参init()、抽象的service()方法

private transient ServletConfig config

属性

void init(ServletConifg)

{ this.config=config; init(); }

void init()

 

abstract void service(ServletRequest, ServletResponse)

 

ServletConfig getServletConfig()

{ return this.config; }

void destroy()

 

String getServletInfo()

 

String getInitParameter(String name)

{ return config.getInitParameter(name); }

Enumeration getInitParameterNames()

{ return config.getInitParameterNames(); }

ServletContext getServletContext()

{ return config.getServletContext();  }

String getServletName()

{ return config.getServletName();  }

2.5.4 ServletContext方法

String getInitParameter(String)

对应<web-app><init-param></init-param></web-app>参数

Enumeration getInitParameterNames()

void setAttribute(String, Object);

域参数

Object getAttribute(String);

Enumeration getAttributeNames()

void removeAttribute(String)

String getResource(String)

服务器资源路径相关

String getRealPath(String)

Set getResourcePaths(String)

InputStream getResourceAsStream(String)

String getContext()

RequestDispatcher getRequestDispatcher(String)

服务器请求转发

ServletContext getServletContext()

获取ServletContext对象

获取web项目在服务器上部署的名称

获取web.xml中<display-name>的值

String getContextPath()

String getContextPathName

Servlet getServlet(String)

Servlet相关

 

Enumeration getServlets()

Enumeration getServletName()

示例代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {ServletContext application = this.getServletContext();  // GenericServlet// http://localhost:8080/servlet/AServlet// 获取 在服务器部署时的项目名称**System.out.println(application.getContextPath());   // /servlet     ******System.out.println(application.getMajorVersion());System.out.println(application.getMinorVersion());System.out.println(application.getMimeType("a.html"));// 服务器端的根"/": 《==》contextPath 《==》/servlet  《==》WebRoot// [/index.jsp, /WEB-INF/, /META-INF/]System.out.println(application.getResourcePaths("/"));/* * Returns a URL to the resource that is mapped to a specified     * path. The path must begin with a "/" and is interpreted     * as relative to the current context root. */// jndi:/localhost/servlet/index.jspSystem.out.println(application.getResource("/index.jsp"));/* * The path must be specified according * to the rules given in <code>getResource</code> */// java.io.ByteArrayInputStream@14028a1dSystem.out.println(application.getResourceAsStream("/index.jsp"));/* * The pathname must begin with a "/" and is interpreted as relative * to the current context root. */// org.apache.catalina.core.ApplicationDispatcher@27a762bcSystem.out.println(application.getRequestDispatcher("/BServlet"));   // ******// D:\workspace\servlet\apache-tomcat-7.0.54\webapps\servlet\index.jsp// D:\workspace\servlet\apache-tomcat-7.0.54\webapps\servlet\web.xml// D:\workspace\servlet\apache-tomcat-7.0.54\webapps\servlet\WEB-INF\web.xml// getRealPath(path:没有强制要求使用/开头,但是建议以/开头,因为/代表服务器的根WebRoot)System.out.println(application.getRealPath("/WEB-INF/web.xml"));// Apache Tomcat/7.0.54System.out.println(application.getServerInfo());/* * <context-param><param-name>pwd</param-name><param-value>123456</param-value></context-param> */// 123456System.out.println(application.getInitParameter("pwd"));/* * 域参数相关 */application.setAttribute("aa", "AA");System.out.println(application.getAttribute("aa"));application.removeAttribute("aa");/* * 1.域参数***** * 2.初始化参数***** * 3.转向***** * 4.资源路径*** * 5.contextPath***** * 6.版本号、服务器名称…… */}

2.5.5 获取参数方法总结

2.5.5.1 请求参数

HttpServletRequest提供的4个方法

String getParameter(String name);

Enumeration getParameterNames();

String[] getParameterValues(String name);

Map<String, String[]> getParameterMap();

2.5.5.2 web.xml参数

<web-app>

<servlet>

<servlet-name></servlet-name>

<servlet-class></servlet-class>

<init-param> --- >>>>Servlet中ServletConfig提供的String getInitParameter(String name);Enumeration getInitParameterNames()

<param-name>username</param-name>

<param-value>admin</param-value>

</init-param>

</servlet>

<context-param> --- >>>>Web项目对应ServletContext对象提供的String getInitParameter(String);

EnumerationgetInitParameterNames();

<param-name>xx</param-name>

<param-value>xxx</param-value>

</context-param>

</web-app>

2.5.5.3 域参数

void setAttribute(String, Object);          ObjectgetAttribute(String)

Enumeration getAttributeNames(); void remove(String name);

2.5.6 ServletConfig和ServletContext




依次访问AServlet和BServlet,运行结果:


原创粉丝点击