首先是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去了。