07 JavaWeb ServletContext的一些应用场景

2.获取初始化参数

package com.testweb;

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 Demo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext sc = this.getServletContext();
        String url = sc.getInitParameter("url");
        resp.getWriter().print(url);
    }

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

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
<!--    配置web应用初始化参数-->
    <context-param>
        <param-name>url</param-name>
        <param-value>http://www.zcv.net.cn</param-value>
    </context-param>
  
    <!--    servlet.context.getInitParamete获取初始化变量-->
    <servlet>
        <servlet-name>sd02</servlet-name>
        <servlet-class>com.testweb.Demo02</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sd02</servlet-name>
        <url-pattern>/sd02</url-pattern>
    </servlet-mapping>
    
</web-app>

3.请求转发

package com.testweb;

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 Demo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext sc = this.getServletContext();
        RequestDispatcher rdp= sc.getRequestDispatcher("/sd02");//转发请求
        rdp.forward(req,resp);
    }

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

注意:重定向必须以/开头,所以不能转发跨域请求,也不能跨项目转发

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3vLTsjSB-1614157725599)(https://tva1.sinaimg.cn/large/008eGmZEly1gnyqio2dloj30m80ci76f.jpg)]

4.读取资源

image-20210224155825243

package com.testweb;

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 Demo04 extends HttpServlet {

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

        InputStream is =this.getServletContext().getResourceAsStream("/WEB-INF/classes/test01.properties");

        Properties prop = new Properties();
        prop.load(is);

        String user =prop.getProperty("username");
        resp.getWriter().print(user);
    }

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

Properties

username=root
password=123456

Web.xml

<servlet>
    <servlet-name>sd04</servlet-name>
    <servlet-class>com.testweb.Demo04</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>sd04</servlet-name>
    <url-pattern>/sd04</url-pattern>
</servlet-mapping>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值