实现servlet的转发和读取Web应用中资源文件【持续更新】

实现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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值