您的位置:首页 > 其它

实现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
<%@ 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: