JavaWeb 初步学习一(ServletConfig参数获取,ServletContext共享数据与请求转发)

首先是ServletConfig的学习

用了下ServletConfig下的参数获取

先在web.xml中对某个Servlet配置下相应的参数

<servlet>
		<description>This is the description of my J2EE component</description>
		<display-name>This is the display name of my J2EE component</display-name>
		<servlet-name>one</servlet-name>
		<servlet-class>com.baoxiu123.one</servlet-class>
		<init-param>
			<param-name>paramone</param-name>
			<param-value>xixihaha</param-value>
		</init-param>
		<init-param>
			<param-name>paramtwo</param-name>
			<param-value>fffff</param-value>
		</init-param>
		<init-param>
			<param-name>paramthree</param-name>
			<param-value>wwww</param-value>
		</init-param>
	</servlet>
再在这个servlet中用config获取出来

ServletConfig config = this.getServletConfig();
		String ev = config.getInitParameter("paramone");
		System.out.println(ev);
		System.out.println("用Enumeration遍历参数");
		Enumeration en = config.getInitParameterNames();
		while(en.hasMoreElements()){
			String tempaa = (String) en.nextElement();
			System.out.println(tempaa);
			System.out.println(config.getInitParameter(tempaa));
		}

输出的结果为:

xixihaha
用Enumeration遍历参数
paramtwo
fffff
paramthree
wwww
paramone
xixihaha


其次是对ServletContext 的学习

首先也是对web.xml是参数的共享

在web.xml中配置如下参数:

<context-param>
		<param-name>contextone</param-name>
		<param-value>password</param-value>
	</context-param>

在一个Servlet中写出如下代码,获取web.xml中的参数信息

System.out.println("输出Context里的参数");
		System.out.println(this.getServletContext().getInitParameter("contextone"));
结果为:

输出Context里的参数
password

再用下不同的Servlet中传递参数

在一个servlet中在context对象中设置如下参数:

this.getServletContext().setAttribute("xixi", "haipi");
在另一个servlet中获取此参数代码:

String tempstr = (String) this.getServletContext().getAttribute("xixi");
		System.out.println(tempstr);
然后分别先后运行那两个servlet

在运行第二个servlet后,在控制台中输出如下内容:

haipi


再用Servlet中的context对象读取其配置文件。在WebRoot 目录下建立一个config.properties的配置文件

在一个servlet中读取

public class democontext extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Properties pro = new Properties();
		String pathstr = this.getServletContext().getRealPath("config.properties");
		System.out.println("pathstr"+pathstr);
		//在我电脑上输出结果为 pathstrC:\apache-tomcat-6.0.14\webapps\testone\config.properties
		pro.load(new FileReader(pathstr));
		System.out.println(pro.getProperty("username"));
		System.out.println(pro.getProperty("password"));
		mydao dao = new mydao();
		dao.addUser();
		
	}

由此可知,this.getServletContext().getRealPath()这个方法会获取tomcat目录上的webapps里对应现在工程的文件

我这里的工程名为testone ,然后获取config.properties文件,就是获取了testone\config.properties这个文件

然后在mydao 中获取(常常在数据库连接里这样获取)同样的配置信息

public class mydao {
	public void addUser(){
		//String tempStr = mydao.class.getClassLoader().getResource("config.properties").getPath();
		//这个会加载src目录下的config.properties文件   即我电脑上的这个 C:\apache-tomcat-6.0.14\webapps\testone\WEB-INF\classes 位置
		String tempStr = mydao.class.getClassLoader().getResource("../../config.properties").getPath();
		//这个会加载C:\apache-tomcat-6.0.14\webapps\testone  这个目录下的方件
		Properties pro = new Properties();
		try {
			pro.load(new FileReader(tempStr));
		System.out.println(pro.getProperty("username"));
		System.out.println(pro.getProperty("password"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
在里由于没有传context对象过来,所以用了类加载的方法获取了配置文件。这种方法最常用

mydao.class.getClassLoader().getResourde().getPath()这个方法得到文件的绝对路径

mydao.class.getClassLoader().getResourde()获得一个URL

.getPath()获得一个String

然后再按常规方法输出就可以了。


最后是Context的转发功能 小试一下:

在一个Servlet中这样写:

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//请求转发-
		this.getServletContext().getRequestDispatcher("/servlet/fourth").forward(request, response);
		
	}

这样就会转发到/servlet/fourth这个url去了。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水中加点糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值