栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

P11:ServletContext 应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

P11:ServletContext 应用

创建ServletDemo03

package com.Li.servlet;
//P11的学习,测试获取web.xml中context-param的内容
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 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);
    }

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

web.xml :context-param与注册





   
      url
      jdbc:mysql://localhost:3306/mybatis
   


   
      hello
      com.Li.servlet.HelloServlet
   
   
      hello
      /hello
   
   

   
      getc
      com.Li.servlet.GetServlet
   
   
      getc
      /getc
   


   
      gp
      com.Li.servlet.ServletDemo03
   
   
      gp
      /gp
   
   
   

Demo03效果:

  

ServletDemo04:转发效果

package com.Li.servlet;

import javax.servlet.RequestDispatcher;
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 ServletDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        System.out.println("进入了ServletDemo04");
        //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");
        //requestDispatcher.forward(req, resp);     这两句等同于下面这句
        context.getRequestDispatcher("/gp").forward(req, resp);

    }

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

 

Demo04的注册:


   
      sd4
      com.Li.servlet.ServletDemo04
   
   
      sd4
      /sd4
   

效果:实现了转发。但是url路径还是用自己的。

 关系:

A想要C的资源,但是自己只能和B交互。于是转发过程为:ABCBA 。

 *读取资源文件(重点)*:

建立:resources下建立文件。======CSDN也太垃圾了。去不了水印,很影响效果。。。。

 

文件里面的内容为:

 

 Demo05

package com.Li.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;
import java.io.InputStream;
import java.util.Properties;

public class ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取web文件中properties的内容。并将它输出为一个流
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

        //读取流中的内容
        Properties properties = new Properties();
        properties.load(is);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");

        //返回前端打印结果。
        resp.getWriter().print(username+":"+password);
    }

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

配置web.xml

 
   
      sd5
      com.Li.servlet.ServletDemo05
   
   
      sd5
      /sd5
   

 

效果:

 

 

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号