servlet上下文对象,每一个web工程都有一个servletContext对象。不管在哪个servlet里面,获取到的这个类的对象都是同一个
获取对象:
ServletContext context=getServletContext();
<context-param>
<param-name>nameparam-name>
<param-value>张三param-value>
context-param>
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 Test01 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.如何获取servletContext对象
ServletContext context=getServletContext();
//获取参数对应的值
String name = context.getInitParameter("name");
System.out.println("Test01获取name="+name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
如获取WebContent下的文件夹file里面的配置文件config.properties(三种方式)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
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 Test02 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//test01(); //
//test02(); //直接给相对路径,获取流对象
//test03(); //通过类加载器获取web工程下的资源
}
private void test01() throws FileNotFoundException, IOException {
//创建servletContext对象
ServletContext context=getServletContext();
//获取项目在Tomcat里面的路径
//String path = context.getRealPath("");//获取项目发布到服务器上的根路径
String path = context.getRealPath("file\\config.properties");
//创建properties对象
Properties properties=new Properties();
//创建输入流对象
InputStream in=new FileInputStream(path);
//加载输入流
properties.load(in);
//根据key获取value值
String value=properties.getProperty("name");
System.out.println(value);
}
private void test02() throws FileNotFoundException, IOException{
ServletContext context=getServletContext();
InputStream in = context.getResourceAsStream("file/config.properties");
Properties pro=new Properties();
pro.load(in);
String name=pro.getProperty("name");
System.out.println("Test02获取name="+name);
}
private void test03() throws FileNotFoundException, IOException{
Properties pro=new Properties();
InputStream in=this.getClass().getClassLoader().getResourceAsStream("../../file/config.properties");
pro.load(in);
String value = pro.getProperty("name");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<h2>欢迎登陆h2>
<form action="loginServlet" method="get">
账号:<input type="text" name="username"/><br>
密码:<input type="text" name="password"/><br>
<input type="submit" value="提交"/>
form>
body>
html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class loginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//通过response获取字符输入流
PrintWriter pw=resp.getWriter();
//1、获取页面提交过来的数据:用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
//2、判断账户信息是否正确:用户名和密码
if("admin".equals(username) && "123".equals(password)) {//3、登陆成功
//获取以前的count值加一
Object obj = getServletContext().getAttribute("count");
int totalCount=0;
if(obj!=null) {
totalCount=(int)obj;
}
System.out.println("已知当前登录总用户数:"+totalCount);
getServletContext().setAttribute("count", totalCount+1);
//设置状态码
resp.setStatus(302);
//定位跳转的资源所在位置
resp.setHeader("Location", "login_success.html");
}else {//登陆失败
pw.write("login error!");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<h1>登陆成功h1>
<a href="countServlet">获取网站登陆成功总人数a>
body>
html>
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 countServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = getServletContext();
//取出登录成功的总人数
int count = (int)context.getAttribute("count");
//返回给页面
resp.getWriter().write("login success count="+count);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
服务器启动的时候就会创建,Tomcat会为托管的每一个web应用程序,创建一个ServletContext对象,从服务器移除托管,或者服务器关闭ServletContext对象就会被销毁
ServletContext作用范围是整个项目,都可以获取。但不能跨项目获取。