package cn.et;
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 ExampleServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public ExampleServlet1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*ServletContext代表当前Web应用,作用整个应用程序*/
//ServletConfig 维护了ServletContext的引用
ServletContext servletContext1 = this.getServletConfig().getServletContext();
ServletContext servletContext2 = this.getServletContext();
//servletContext1与servletContext2是同一个对象
System.out.println(servletContext1==servletContext2);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package cn.et;
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 ExampleServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
public ExampleServlet2() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String data = "数据信息";
//将data数据设入Context作用域中
servletContext.setAttribute("data", data);
//设入Context中的数据,其他Servlet也能通过ServletContext对象获取数据,可用于数据交流
String value = (String)servletContext.getAttribute("data");
response.getOutputStream().write(("Hello Servlet "+value).getBytes());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package cn.et;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
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 ExampleServlet3 extends HttpServlet {
private static final long serialVersionUID = 1L;
public ExampleServlet3() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//读取资源文件(.properties)
//“/”代表当前Web应用(服务器中的应用目录)
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/jdbc.properties");
Properties props = new Properties();
props.load(in);
/*driverClass=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
usernames=scott
password=tiger*/
String driverClass = props.getProperty("driverClass");
String url = props.getProperty("url");
String usernames = props.getProperty("usernames");
String password = props.getProperty("password");
System.out.println(driverClass);
System.out.println(url);
System.out.println(usernames);
System.out.println(password);
//请求转发
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/index.html");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletExample3_ServletContext</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ExampleServlet1</display-name>
<servlet-name>ExampleServlet1</servlet-name>
<servlet-class>cn.et.ExampleServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExampleServlet1</servlet-name>
<url-pattern>/ExampleServlet1</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ExampleServlet2</display-name>
<servlet-name>ExampleServlet2</servlet-name>
<servlet-class>cn.et.ExampleServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExampleServlet2</servlet-name>
<url-pattern>/ExampleServlet2</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ExampleServlet3</display-name>
<servlet-name>ExampleServlet3</servlet-name>
<servlet-class>cn.et.ExampleServlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExampleServlet3</servlet-name>
<url-pattern>/ExampleServlet3</url-pattern>
</servlet-mapping>
</web-app>
index.html<html>
<body>
HTML页面
</body>
</html>
jdbc.properties文件,放在src目录下
#driverClass=com.mysql.jdbc.Driver
#url=jdbc:mysql://192.168.14.103:3306/myself
#usernames=root
#password=123456
driverClass=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
usernames=scott
password=tiger