JavaWeb入门day04--ServletContext

JavaWeb入门day04--ServletContext,第1张

一、ServletContext

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

  • 共享数据:在Servlet1中保存的内容可以到Servlet2中可以拿到;
二、共享数据 

1、创建一个父子Maven工程,子工程命名为Servlet02。可参考JavaWeb入门day03--Servlet_冷冷冷冷冷冷冷啊的博客-CSDN博客

2、先编写一个Helloservlet.class,用于存放context中的内容

package com.liyu.Servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String username="张三";
        context.setAttribute("username",username);
       // System.out.println("Hello");
    }

}

3、 在写一个getServelt类,来获取context中存的内容。

package com.liyu.Servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class getServlet extends HelloServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();

        String username=(String) context.getAttribute("username");

        //添加编码方式
        //修改返回页面的编码方式
        resp.setContentType("text/html");
        //修改页面中的编码方式
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字"+username);
    }

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

4、在修改配置文件,添加映射。




    
        hello
        com.liyu.Servlet.HelloServlet
    

    
        hello
        /hello
    

    
        getname
        com.liyu.Servlet.getServlet
    
    
    
        getname
        /getname
    

 三、获取初始化参数

1、新建一个ServletDemo03.class类,获取初始化参数在输出。

public class ServletDemo03 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);
    }
}

2、在web.xml中配置所需的参数内容。

    
        url
        jdbc:mysql://localhost:3306/mybatis
    
    
        Demo03
        com.liyu.Servlet.ServletDemo03
    
    
        Demo03
        /Demo03
    
 四、转发--浏览器地址不发生变化
public class ServletDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("进入的是Demo04");
        ServletContext context = this.getServletContext();
        //转发的请求路径RequestDispatcher requestDispatcher=context.getRequestDispatcher("/Demo03");
       //进行转发requestDispatcher.forward(req,resp);
        context.getRequestDispatcher("/Demo03").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
五、读取资源文件

1、Properties

  • 在Java目录下新建properties
  • 在resourc目录下新建properties

发现:都被打包在了同一个路径下:classes,称为类路径:classpath。

2、修改resource目录下的db.properties,

username="root"
password="123456"

3、新建一个ServletDemo05类

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建一个流,通过getServletContext方法去获取目录下的文件
        InputStream is = this.getServletContext().getResourceAsStream("WEB-INF/classes/db.properties");
        //创建一个properties类
        Properties properties = new Properties();
        properties.load(is);
        //获取properties中的内容
        String user=properties.getProperty("username");
        String password=properties.getProperty("password");
        //输出到页面
        response.getWriter().print("名字"+user+":"+password);
        System.out.println("123456");
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/923402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存