1 Web应用共享数据区ServletContext接口
ServletContext接口表示一个web应用的上下文。可以想象成一个web一个能用的共享数据区域,该区域保存整个web共享数据。
1.1 Servlet容器何时创建ServeltContext接口实现类
当一个web应用启动的时候,就会创建该ServletContext接口的实现类。我们可以在根据需求,读写ServletContext这个web共享区的数据,那么如何获得ServletContext这个实例呢?下面提供了两种方法
1.2 如何获得ServletContext接口实例
方法1:
可以通过ServletConfig接口的getServletContext()方法获得
方法2:
通过GenericServlet抽象类的getServletContext()方法获得,不过这个方法其实也是调用了ServletConfig的getServletContext()方法
在GenericServlet.java中
Java代码
public ServletConfig getServletConfig() {
return config;
}
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
}
public ServletConfig getServletConfig() {
return config;
}
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
}1.3 一个计数器实例
Java代码
package servletcontext;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CounterServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response){
try
{
response.setCharacterEncoding("gb2312");
//获取ServletContext接口的实现类
ServletContext sc = super.getServletContext();
Integer count = (Integer) sc.getAttribute("count");
if(null == count){
count = new Integer(1);
}else{
count = new Integer(count.intValue()+1);
}
sc.setAttribute("count", count);
PrintWriter out = response.getWriter();
out.print("<body>");
out.print("你登录的次数是:"+count.intValue());
out.print("</body>");
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request,response);
}
}
package servletcontext;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CounterServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response){
try
{
response.setCharacterEncoding("gb2312");
//获取ServletContext接口的实现类
ServletContext sc = super.getServletContext();
Integer count = (Integer) sc.getAttribute("count");
if(null == count){
count = new Integer(1);
}else{
count = new Integer(count.intValue()+1);
}
sc.setAttribute("count", count);
PrintWriter out = response.getWriter();
out.print("<body>");
out.print("你登录的次数是:"+count.intValue());
out.print("</body>");
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request,response);
}
}
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version=[size=x-large][/size]"2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>CountServlet</servlet-name>
<servlet-class>servletcontext.CounterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CountServlet</servlet-name>
<url-pattern>/count</url-pattern>
</servlet-mapping>
</web-app>
ServletContext接口表示一个web应用的上下文。可以想象成一个web一个能用的共享数据区域,该区域保存整个web共享数据。
1.1 Servlet容器何时创建ServeltContext接口实现类
当一个web应用启动的时候,就会创建该ServletContext接口的实现类。我们可以在根据需求,读写ServletContext这个web共享区的数据,那么如何获得ServletContext这个实例呢?下面提供了两种方法
1.2 如何获得ServletContext接口实例
方法1:
可以通过ServletConfig接口的getServletContext()方法获得
方法2:
通过GenericServlet抽象类的getServletContext()方法获得,不过这个方法其实也是调用了ServletConfig的getServletContext()方法
在GenericServlet.java中
Java代码
public ServletConfig getServletConfig() {
return config;
}
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
}
public ServletConfig getServletConfig() {
return config;
}
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
}1.3 一个计数器实例
Java代码
package servletcontext;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CounterServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response){
try
{
response.setCharacterEncoding("gb2312");
//获取ServletContext接口的实现类
ServletContext sc = super.getServletContext();
Integer count = (Integer) sc.getAttribute("count");
if(null == count){
count = new Integer(1);
}else{
count = new Integer(count.intValue()+1);
}
sc.setAttribute("count", count);
PrintWriter out = response.getWriter();
out.print("<body>");
out.print("你登录的次数是:"+count.intValue());
out.print("</body>");
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request,response);
}
}
package servletcontext;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CounterServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response){
try
{
response.setCharacterEncoding("gb2312");
//获取ServletContext接口的实现类
ServletContext sc = super.getServletContext();
Integer count = (Integer) sc.getAttribute("count");
if(null == count){
count = new Integer(1);
}else{
count = new Integer(count.intValue()+1);
}
sc.setAttribute("count", count);
PrintWriter out = response.getWriter();
out.print("<body>");
out.print("你登录的次数是:"+count.intValue());
out.print("</body>");
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request,response);
}
}
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version=[size=x-large][/size]"2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>CountServlet</servlet-name>
<servlet-class>servletcontext.CounterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CountServlet</servlet-name>
<url-pattern>/count</url-pattern>
</servlet-mapping>
</web-app>