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

javaweb-11:ServletContext应用

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

javaweb-11:ServletContext应用

2、获取初始化参数

web.xml



    
    
        url
        jdbc:mysql://localhost:3306/mybatis
    
    
        hello
        com.gongyi.servlet.HelloServlet
    
    
    
        hello
        /hello
    

    
        getp
        com.gongyi.servlet.ServletDemo03
    

    
        getp
        /getp
    
    



ServletDemo03.java

package com.gongyi.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 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().println(url);
    }

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

3、请求转发

ServletDemo04.java

package com.gongyi.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);//调用forward实现请求转发
        context.getRequestDispatcher("/gp").forward(req,resp);
    }

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

web.xml



    
    
        url
        jdbc:mysql://localhost:3306/mybatis
    
    
        hello
        com.gongyi.servlet.HelloServlet
    
    
    
        hello
        /hello
    

    
        getp
        com.gongyi.servlet.ServletDemo03
    

    
        getp
        /getp
    

    
        sd4
        com.gongyi.servlet.ServletDemo04
    

    
        sd4
        /sd4
    
    



转发与重定向图解

4、读取资源文件

Properties

  • 在java目录下新建properties

  • 在resources目录下新建properties(解决资源文件未打包到目标路径问题)

    在pom.xml中添加如下

    
            
                
                    src/main/java
                    
                        ***.xml
                    
                    false
                
            
        
    

发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath

思路:需要一个文件流

db.properties

username=root
password=123456

ServletDemo05.java

package com.gongyi.servlet;

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 {
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        //InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/gongyi/servlet/db.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);
    }
}

web.xml



    
    
        url
        jdbc:mysql://localhost:3306/mybatis
    
    
        hello
        com.gongyi.servlet.HelloServlet
    
    
    
        hello
        /hello
    

    
        getp
        com.gongyi.servlet.ServletDemo03
    

    
        getp
        /getp
    

    
        sd4
        com.gongyi.servlet.ServletDemo04
    

    
        sd4
        /sd4
    

    
        sd5
        com.gongyi.servlet.ServletDemo05
    

    
        sd5
        /sd5
    
    

访问测试即可

彩蛋

1,在maven web中,classpath包含类文件和资源文件

2.maven web打包后,观察target目录结构

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

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

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