ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Servlet(三)ServletContext

2021-01-23 13:33:49  阅读:187  来源: 互联网

标签:username ServletException resp req IOException ServletContext Servlet void


Servlet(三)ServletContext

ServletContextd对象:

web容器在启动的时候,它会为每个web程序都创建一个对象的ServletContext对象,它代表了当前的web应用;

  • 共享数据

    我在一个Servlet中保存的数据,可以在另一个Servlet中拿到

    实际开发时,少用servletContext,用session

    //放置数据的类
    public class HelloServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
    //        this.getInitParameter(); 初始化参数
    //        this.getServletConfig(); Servlet配置
    //        this.getServletContext(); Servlet上下文
            ServletContext servletContext = this.getServletContext();
            String username = "困了就睡";
            servletContext.setAttribute("username",username);//将一个数据保存到ServletContext中,名字:username,值:username,看成键值对的形式
        }
    }
    
    //读取数据的类
    public class GetServlet extends HttpServlet{
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext servletContext = this.getServletContext();
            String username = (String) servletContext.getAttribute("username");
    
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html");
            resp.getWriter().print(username);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    //配置xml
    <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.godwin.servlet.HelloServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
        
        <servlet>
            <servlet-name>get</servlet-name>
            <servlet-class>com.godwin.servlet.GetServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>get</servlet-name>
            <url-pattern>/get</url-pattern>
        </servlet-mapping>
    

    测试访问时,

    注意:读取数据的类,记得解决乱码问题

    resp.setCharacterEncoding("utf-8");
    resp.setContentType("text/html");
    

    第一次访问http://localhost:8080/s2/get,页面显示为null,

    接着访问http://localhost:8080/s2/hello,页面为空,但是拿了数据

    在访问http://localhost:8080/s2/get,拿到数据,并且页面显示“困了就睡”,即设置好的数据

ServletContextd应用

获取初始化参数

实际开发中,几乎不用这么做

web.xml

<context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>

<servlet>
        <servlet-name>gp</servlet-name>
        <servlet-class>com.godwin.servlet.ServletDemo01</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>gp</servlet-name>
        <url-pattern>/gp</url-pattern>
    </servlet-mapping>
public class ServletDemo01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String url = context.getInitParameter("url");
        resp.getWriter().print(url);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

请求转发

实际开发中,我们用Request去做

请求转发时,路径不会变

web.xml

<servlet>
        <servlet-name>tp</servlet-name>
        <servlet-class>com.godwin.servlet.ServletDemo02</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>tp</servlet-name>
        <url-pattern>/tp</url-pattern>
    </servlet-mapping>
public class ServletDemo02 extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
//        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的请求路径
//        requestDispatcher.forward(req,resp);//调用forward实现请求转发
        context.getRequestDispatcher("/gp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

img

读取资源文件

实际开发中,用类加载或者反射等去做,几乎不用这个方式去做

Properties:

  • 在java目录下新建properties
  • 在resources目录下新建properties

发现都被打包到同一路径下:classes,俗称:类路径 。

思路:需要一个文件流

解决资源导出问题:

<!--在build中配置resources,来防止我们资源导出失败的问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
public class ServletDemo03 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/dp.properties");
        Properties prop = new Properties();
        prop.load(is);
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");
        resp.getWriter().print(username + ":" + password);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
<servlet>
        <servlet-name>sd3</servlet-name>
        <servlet-class>com.godwin.servlet.ServletDemo03</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sd3</servlet-name>
        <url-pattern>/sd3</url-pattern>
    </servlet-mapping>
username=root
password=root

复习笔记资料参考B站UP主狂神说

标签:username,ServletException,resp,req,IOException,ServletContext,Servlet,void
来源: https://www.cnblogs.com/XING-ZHI-JI-DA-XUE/p/14317347.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有