目录概要:
1. 写出获取ServletContext的两种方式
2.使用ServletContext实现两个Servlet数据共享
3.设置ServletContext初始化参数,然后对其之。
4. 编写一个转发
5.通过ServletContext读取配置文件的内容。(使用两种方式)
6.通过一般的java类读取配置文件的内容。
原码:----------------------------------------------------------------------------------------
一、写出获取ServletContext的两种方式
package com.hbsi.context;
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 Context1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ServletContext方法1
ServletContext context1 = this.getServletConfig().getServletContext();
//获取ServletContext方法2
ServletContext context2 = this.getServletContext();}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {doGet(request, response);
}}
二、使用ServletContext实现两个Servlet数据共享
package com.hbsi.context;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Context2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//多个Servlet实现数据共享
String data="abcd";
this.getServletContext().setAttribute("data1", data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {doGet(request, response);
}}
package com.hbsi.context;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Context3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//配合Context2使用
String value = (String) this.getServletContext().getAttribute("data1");
System.out.println(value);}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {doGet(request, response);
}}
三、设置ServletContext初始化参数,然后对其之。
package com.hbsi.context;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Context4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {System.out.println(this.getServletContext().getInitParameter("data"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}四、 编写一个转发
package com.hbsi.context;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Context5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//实现转发
this.getServletContext().setAttribute("username","zhangsan");
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
-------------------------------------------------------
//链接数据库driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
----------------------------------------------------------//JSP
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
//嵌入java代码
<%=application.getAttribute("username") %>
5.通过ServletContext读取配置文件的内容。(使用两种方式)package com.hbsi.context;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hbsi.dao.StudentDao;
public class Context6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {// 传统的方式错误了
/*
* FileInputStream fis = new FileInputStream("src/db.properties");
*
* Properties prop = new Properties(); prop.load(fis);
*
* String driver = prop.getProperty("driver");
* System.out.println(driver);
*/
StudentDao dao = new StudentDao();
dao.delete();
}
// 通过ServletContext读取配置文件 方法1public void test1() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream(
"/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
}
// 通过ServletContext读取配置文件 方法2,获取了文件的绝对路径
public void test2() throws IOException {
String path = this.getServletContext().getRealPath(
"/WEB-INF/classes/db.properties");
int i = path.lastIndexOf("\\");
System.out.println(path.substring(i + 1));
FileInputStream fis = new FileInputStream(path);
Properties prop = new Properties();
prop.load(fis);
String driver = prop.getProperty("driver");
System.out.println(driver);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
6.通过一般的java类读取配置文件的内容。
package com.hbsi.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
public class StudentDao {
private static String driver;
static{//如果读取配置文件不是Servlet,而是一般的类,使用类加载器。
InputStream in = StudentDao.class.getClassLoader().getResourceAsStream("db.properties");
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
}
public static Connection getConnection(){
return null;
}public void insert(){
}
public void update(){
}
public void delete(){
}
}