java_ServletContext 应用源码
2012-10-22 16:25
344 查看
目录概要:
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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
//嵌入java代码
<%=application.getAttribute("username") %>
</body>
</html>
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读取配置文件 方法1
public 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(){
}
}
相关文章推荐
- Java_常瑞鹏 java_ServletContext 应用源码
- java_ServletContext 应用源码
- Portal-Basic Java Web 应用开发框架 v2.6.1(源码、示例及文档)
- Tomcat与JavaWeb 1.3 ServletContext、Web应用范围与Serlvet的服务方法抛出异常
- (转)JavaWeb学习之Servlet(四)----ServletConfig获取配置信息、ServletContext的应用
- Portal-Basic Java Web 应用开发框架 v3.0.1 正式发布(源码、示例及文档)
- java_web之ServletContext应用
- Java FastJson 源码分析(一) 之 简单应用
- Portal-Basic Java Web 应用开发框架(v2.5.4 - 源码、示例及文档可在 Google Code 下载)
- 深入Java基础(四)--哈希表(2)HashTable与HashSet应用及源码详解
- JDK源码分析Java.lang.Boolean的浅析——单例模式的应用(使我们更省内存)
- Portal-Basic Java Web 应用开发框架(v2.5.4 - 源码、示例及文档可在 Google Code 下载)
- JAVA Base64编码-源码及应用
- Portal-Basic Java Web 应用开发框架(v2.5.4 - 源码、示例及文档可在 Google Code 下载)
- java迭代器应用 源码探究
- JDK源码-java.lang.instrument-第二部分-应用示例
- Java源码——一个简单的余额查询应用(Credit Inquiry Program)
- GDAL从源码到JAVA应用(Windows环境)
- [Java][Web]ServletContext 方法的应用
- 【Android应用源码分析】Java多线程:线程本地变量ThreadLocal源码分析