zoukankan      html  css  js  c++  java
  • context的简单应用

    数据上传

    context.setAttribute

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String username="吴梓超";
            context.setAttribute("username",username);
        }
    

    数据请求

    context.getAttribute

    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 {
            doGet(req, resp);
        }
    

    参数获取

    context.getInitParameter

    context-param>
    	<param-name>url</param-name>
    	<param-value>jdbc:mysql://localhost:3306</param-value>
    </context-param>
    
     @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);
        }
    

    请求转发

    context.getRequestDispatcher("/s3").forward(req,resp);

     @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            context.getRequestDispatcher("/s3").forward(req,resp);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    

    读取资源文件

    需要一个文件流

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop=new Properties();
        prop.load(is);
        String name = prop.getProperty("username");
        String password = prop.getProperty("password");
        resp.getWriter().print(name+"+"+password);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    
  • 相关阅读:
    upstream sent unsupported FastCGI protocol version: 72 while reading response header from upstream
    当代免疫学小史-第一章(根据讲谈社Blue Backs系列2009年第一版第三次印刷版本翻译)
    微信小程序自定义tabbar解决方案(可用于解决tabbar跳转至分包页面问题)
    celery的使用
    分布式之数据库和缓存双写一致性方案解析
    opencv实战-全景图像拼接
    matplotlib+seaborn样式管理-学习整理
    matplotlib绘制3D图形
    matplotlib+seaborn图形绘制-学习整理
    opencv实战-文档扫描
  • 原文地址:https://www.cnblogs.com/chaostudy/p/12921343.html
Copyright © 2011-2022 走看看