1、获得整个web应用初始化参数
类中:
public class ContextServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过config获得context
ServletContext context=getServletConfig().getServletContext();
//从而获得全局的参数
System.out.println(context.getInitParameter("hobby"));//一定不能忘记用context
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
web.xml中:
<context-param>
<param-name>hobby</param-name>
<param-value>唱儿歌</param-value>
</context-param>
2、实现全局数据共享
以记录网站的访问次数为例
在countServlet 类中
public class countServlet extends HttpServlet {
@Override
public void init() throws ServletException {
//获得servletcontext对象
ServletContext context=getServletContext();
//保存访问次数0
context.setAttribute("visitimes",0);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//每次访问都回执行doget方法,因此把递增次数的访问次数放在doget中。
//从servletcontext中获得访问次数
ServletContext context=getServletContext();
int times=(Integer)context.getAttribute("visitimes");
//访问次数递增
times++;
//更新访问次数到servletcontext中
context.setAttribute("visitimes", times);
response.getWriter().print("网站被访问了"+times+"次");//输出到浏览器!
//System.out.println("网站被访问了"+times+"次");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
在CountShowServlet类中(代码和上个类中的代码一致!)
public class CountShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*ServletContext context=getServletContext();
int times =(Integer)context.getAttribute("visitimes");*/
ServletContext context=getServletContext();
int time=(Integer)context.getAttribute("visitimes");
time++;
context.setAttribute("visitimes", time);
response.getWriter().print(time);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
此例说明了在countServlet中用context记录的访问次数,在CountShowServlet 也能访问,即全局共享!
3、读取we工程资源文件
package aweiyo.io;
public class ReadFileServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//因为不在wenroot的路径下,而在工程路径下,所以要加上前面的路径。又因为工程的存放文件的路径是在webroot的目录下,所以可以read,但是
//web-inf目录下的文件就不可以读取了,因为其不在webroot的目录下
String filename2="/WEB-INF/classes/a.txt";
filename2=getServletContext().getRealPath(filename2);
readfile(filename2);
// 路径开头必须写/,因为webroot就是servlet重的根目录,所以在webroot下的文件不用加其他的
String filename="/a2.txt";
//获得该路径的绝对路径,即全部路径
filename=getServletContext().getRealPath(filename);
readfile(filename);
//用Class的路径,也可用于Java中
Class c=ReadFileServlet.class;
String filename3=c.getResource("/a.txt").getFile();
readfile(filename3);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public static void readfile(String filename) throws FileNotFoundException,
IOException {
BufferedReader in =new BufferedReader(new FileReader(filename));
String line;
while((line=in.readLine())!=null){
System.out.println(line);
}
in.close();
}
}
注:java读取文件
/**
* 通过Java程序读取文件
* @author aweiyoo
*
*/
public class FileReaderTest {
public static void main(String[] args) throws IOException {
String filename="src/a.txt";
readfile(filename);
String filename2="WebRoot/a2.txt";
readfile(filename2);
String filename3=("a3.txt");//位于根目录下的文件可以直接写
readfile(filename3);
}
public static void readfile(String filename) throws FileNotFoundException,
IOException {
BufferedReader in =new BufferedReader(new FileReader(filename));
String line;
while((line=in.readLine())!=null){
System.out.println(line);
}
in.close();
}
}