java getattribute为空_JavaWeb基础 ServletContext getAttribute 读取不存在的数据时返回null...

礼悟:

好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。

虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。

javaEE:7

javaSE:1.8

JSTL:1.2.2

server:tomcat 8.5

explorer:Firefox

os:windows7 x64

ide:MyEclipse 2017

工程目录结构

20180111004918672526.png

web.xml

ReadNotExistDataServletContext

jizuiku.web.servlet.ReadNotExistDataServletContext

ReadNotExistDataServletContext

/ReadNotExistDataServletContext

minNum

10

count

100

maxNum

1000

代码

package jizuiku.web.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* ServletContext 读取不存在的数据

*

* @author 给最苦

* @version V17.10.21

*/

public class ReadNotExistDataServletContext extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = 1L;

@Override

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

// TODO Auto-generated method stub

System.out.println("读取数据");

ServletContext application = this.getServletContext();

String notExistAttributeValue = (String)application.getAttribute("notExistAttribute");

if(notExistAttributeValue == null){

System.out.println("notExistAttribute 未创建");

} else {

System.out.println("如果输出这个,需要检查程序的逻辑");

}

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

// TODO Auto-generated method stub

super.doPost(req, resp);

}

}

效果

20180111004918674479.png

学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。

博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值