ServletConfig和ServletContext
2016-11-19 20:23
399 查看
ServletConfig对象
## 使用getServletConfig方法创建对象,并在配置文件进行初始化配置 ## //ServletConfig程序,读取配置文件 `package com.heima.test; import java.io.IOException; 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 ServletConfig01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletConfig config = getServletConfig(); String username = config.getInitParameter("username"); String password = config.getInitParameter("password"); System.out.println(username + " " + password); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }` //配置文件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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletConfig</servlet-name> <servlet-class>com.heima.test.ServletConfig01</servlet-class> <init-param> <param-name>username</param-name><param-value>root</param-value> </init-param> <init-param> <param-name>password</param-name><param-value>123</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletConfig</servlet-name> <url-pattern>/demo1</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>`
ServletContext对象
## 使用getServletContext方法创建对象,配置文件xml ## # 代码实现统计访问网站次数 # //配置文件 `<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <display-name></display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletCount</servlet-name> <servlet-class>com.heima.test.ServletCount</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletShow</servlet-name> <servlet-class>com.heima.test.ServletShow</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletCount</servlet-name> <url-pattern>/count</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ServletShow</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ` //统计次数代码 `package com.heima.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletCount extends HttpServlet { @Override public void init() throws ServletException { ServletContext context = getServletContext(); context.setAttribute("num", 0); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); int num = (Integer) context.getAttribute("num"); context.setAttribute("num", num+1); response.getWriter().write("welcome to website"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ` //显示次数代码 `package com.heima.test; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletShow extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); int num = (Integer) context.getAttribute("num"); response.getWriter().write("times:" + num); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } `
相关文章推荐
- (转)ServletConfig与ServletContext对象详解
- Servlet技术浅析(五)之-----ServletConfig接口和ServletContext接口
- getServletConfig() 与getServletContext() 的区别
- JavaWeb 初步学习一(ServletConfig参数获取,ServletContext共享数据与请求转发)
- 【JavaEE】Servlet接口、ServletConfig接口、GenericServlet抽象类、ServletContext接口、HttpServlet类源码及方法
- Servlet简介 以及servlet一些细节 servletconfig servletcontext 的几个应用
- ServletConfig获取配置信息、ServletContext的应用
- request,session,servletContext,servletConfig等分析
- ServletConfig与ServletContext
- ServletContext和ServletConfig
- ServletConfig与ServletContext对象(接口)
- Servlet--ServletConfig和ServletContext
- ServletContext与ServletConfig的深度分析
- ServletConfig和ServletContext
- ServletContext,ServletConfig和Sesion
- Servlet知识详解(一)ServletContext对象 和 ServletConfig对象学习笔记
- ServletConfig和ServletContext
- 关于ServletConfig与ServletContext的访问
- ServletContext与ServletConfig的深度分析
- ServletConfig与ServletContext的区别与联系