实现servlet的转发和读取Web应用中资源文件【持续更新】
2016-05-29 19:15
519 查看
实现Servlet的转发
servletContextDemo2:
package cn.lsh.servlet;
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;
//实现Servlet的转发
public class ServletContextDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "元芳,你怎么看。。。。";
//把数据添加给ServletContext对象
this.getServletContext().setAttribute("data",data);
//转发给1.jsp
this.getServletContext().getRequestDispatcher("/1.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}<strong>
</strong>}
新建1.jsp
在浏览器地址栏输入:http://localhost:8080/Servlet2/ServletContextDemo2
输出结果为:
利用ServletContext对象读取资源文件
新建ServletCtextDemo3:
package cn.lsh.servlet;
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;
//利用ServletContext对象读取资源文件。
public class ServletContextDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// test1();
// test2();
test3();
}
//db.properties文件在src目录下,那么访问路径如下
private void test1() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("WEB-INF/classes/db.properties");
Properties pro = new Properties();
pro.load(in);
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
//db.properties文件在包名内,那么访问路径如下
private void test2() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("WEB-INF/classes/cn/lsh/servlet/db.properties");
Properties pro = new Properties();
pro.load(in);
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
//db.properties文件在WebRoot目录下,那么访问路径如下
private void test3() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("/db.properties");
Properties pro = new Properties();
pro.load(in);
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<strong>在src目录下,包名内,WebRoot目录下分别新建db.properties文件。</strong>
db.properties配置文件内容如下:
在浏览器地址栏输入:http://localhost:8080/Servlet2/ServletContextDemo3
控制台输出的结果为:
jdbc:mysql://localhost:8080/mydb
root
root
servletContextDemo2:
package cn.lsh.servlet;
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;
//实现Servlet的转发
public class ServletContextDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "元芳,你怎么看。。。。";
//把数据添加给ServletContext对象
this.getServletContext().setAttribute("data",data);
//转发给1.jsp
this.getServletContext().getRequestDispatcher("/1.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}<strong>
</strong>}
新建1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <font color="red"> <% String data = (String)application.getAttribute("data"); out.write(data); %> </font> </body> </html>
在浏览器地址栏输入:http://localhost:8080/Servlet2/ServletContextDemo2
输出结果为:
利用ServletContext对象读取资源文件
新建ServletCtextDemo3:
package cn.lsh.servlet;
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;
//利用ServletContext对象读取资源文件。
public class ServletContextDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// test1();
// test2();
test3();
}
//db.properties文件在src目录下,那么访问路径如下
private void test1() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("WEB-INF/classes/db.properties");
Properties pro = new Properties();
pro.load(in);
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
//db.properties文件在包名内,那么访问路径如下
private void test2() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("WEB-INF/classes/cn/lsh/servlet/db.properties");
Properties pro = new Properties();
pro.load(in);
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
//db.properties文件在WebRoot目录下,那么访问路径如下
private void test3() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("/db.properties");
Properties pro = new Properties();
pro.load(in);
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<strong>在src目录下,包名内,WebRoot目录下分别新建db.properties文件。</strong>
db.properties配置文件内容如下:
jdbc:mysql://localhost:8080/mydb root root
在浏览器地址栏输入:http://localhost:8080/Servlet2/ServletContextDemo3
控制台输出的结果为:
jdbc:mysql://localhost:8080/mydb
root
root
相关文章推荐
- Java内部类的使用小结
- BOM和DOM
- 【Java】常用二叉树考点
- Linux系统启动流程 图解
- HBase总结(十一)hbase Java API 介绍及使用示例
- 练习打字第十六天!
- Mac OS X搭建基于hexo的静态博客站点
- 日期计算
- 模拟算法练习
- android安卓Sqlite数据库实现用户登录注册
- android安卓Sqlite数据库实现用户登录注册
- PHP中的表达式简述
- 基于Redis实现分布式锁
- 记Bstar复赛
- overload重载、override重写
- Java类的初始化顺序 (静态变量、静态初始化块、变量、初始化块、构造器)
- NYOJ Distribution---1241第八届
- Leetcode no. 152
- 中断处理程序中输出调试信息导致无法计算机无法启动
- BLE 连接间隔最大值和最小值的问题