ServletContext对象(4)、web项目中路径使用、ServletConfig,ServletContext方法总结

来源:互联网 发布:软件设计师成绩查询 编辑:程序博客网 时间:2024/06/04 17:44

一、ServletContext对象:
1、web项目中路径使用:

package sram.path;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class PathDemo extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html;charset=utf-8");        //目标资源: target.html        /**         * 思考: 目标资源是给谁使用的。         *      给服务器使用的:   / 表示在当前web应用的根目录(webRoot下)         *      给浏览器使用的: /  表示在webapps的根目录下         */        /**         * 1.转发         */        request.getRequestDispatcher("/target.html").forward(request, response);                /**         * 2.请求重定向         */        response.sendRedirect("/servlet_test2/target.html");        /**         * 3.html页面的超连接href         */        response.getWriter().write("<html><body><a href='/servlet_test2/target.html'>超链接</a></body></html>");        /**         * 4.html页面中的form提交地址         */        response.getWriter().write("<html><body><form action='/servlet_test2/target.html'><input type='submit'/></form></body></html>");    }}

2、ServletContext对象的核心API(作用)
6)java.lang.String getRealPath(java.lang.String path) –得到web应用的资源文件
7)java.io.InputStream getResourceAsStream(java.lang.String path)

package sram.resource;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.Properties;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ResourceDemo extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        /**         *  . 表示相对路径中的当前路径代表,相对于java命令运行目录。java运行命令在哪里??         *   结论:  1、在java项目中,. 代表在java项目的根目录下开始         *        2、在web项目中, . 代表在tomcat/bin目录下开始,所以不能使用这种相对路径。         */        //读取文件。在web项目下不要这样读取。因为.表示在tomcat/bin目录下        /*File file = new File("./src/db.properties");        FileInputStream in = new FileInputStream(file);*/        /**         * 使用web应用下加载资源文件的方法         */        /**         * 1. getRealPath读取,返回资源文件的绝对路径         */        /*String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");        System.out.println(path);        File file = new File(path);        FileInputStream in = new FileInputStream(file);*/        /**         * 2. getResourceAsStream() 得到资源文件,返回的是输入流         */        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");        Properties prop = new Properties();        //读取资源文件        prop.load(in);        String user = prop.getProperty("user");        String password = prop.getProperty("password");        System.out.println("user="+user);        System.out.println("password="+password);    }}

在web项目中路径不要再使用”.“了,”.“表示tomcat下的bin目录,而web项目是在webapps目录下的。

二、ServletConfig,ServletContext方法总结
1、ServletConfig对象
获取servlet的初始化参数:
1)getInitParameter(“name”);
2)getInitParameterNames();

2、ServletContext对象
1)得到web应用路径:
a)context.getContextPath();
b)request.getContextPath(); 等价于上面的代码
2)得到web应用参数:
a)context.getInitParameter(“name”);
b)context.getInitParameterNames();
3)域对象:
a)context.setAttribute(“name”,Object): 保存数据
b)context.getAttribute(“name”) 得到数据
c)context.removeAttribue(“name”) 清除数据
4)转发
a)context.getRequestDispatcher(“路径”).forward(request,response);
b)request.getRequestDispacher(“路径”).forward(request,response); 等价于上面的代码
5)得到web应用中的资源文件
a)context.getRealPath(“路径”)
b)context.getResourceAsStream(“路径”);

ServletContext对象终于学完了,累死宝宝了!!!

0 0
原创粉丝点击