IDEA中ServletContext傳遞值爲Null解決方法

ServletContext傳遞值爲Null解決方法

Web容器在啓動的時候,它會爲每一個Web程序都創建一個對應的ServletContext對象,它代表了當前的Web應用。

作用:

1.共享數據。

我在此Servlet中保存的數據,在其他Servlet中也可以使用。

在這裏插入圖片描述

存放數據文件:

package com.edwin.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;
/**
 * @author Edwin D
 * @date 2020.6.2 上午 7:59
 */
public class HelloServlet_2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        this.getInitParameter();初始化參數
//        this.getServletContext();Servlet配置
//        this.getServletContext();Servlet上下文
        ServletContext i = this.getServletContext();

        String username = "Edwin";//數據
        i.setAttribute("Username",username);
//        將一個值存在了ServletContext中,名字爲:Username,值爲username(Edwin)。
    }

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

讀取數據文件:

package com.edwin.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;
/**
 * @author Edwin D
 * @date 2020.6.2 上午 8:56
 */
public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext i = this.getServletContext();
        String username = (String) i.getAttribute("Username");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().println("姓名:"+username);
    }

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

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>com.edwin.servlet.HelloServlet_2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/Hello</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>Get</servlet-name>
    <servlet-class>com.edwin.servlet.GetServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Get</servlet-name>
    <url-pattern>/Get</url-pattern>
  </servlet-mapping>

</web-app>

測試結果:

注意訪問頁面的順序:點擊運行,先訪問Hello(此頁面爲空),但是必須要先訪問,確保:

String username = "Edwin";//數據

數據傳入ServletContext,纔可以被後面的GetServlet訪問到。

結果:
主界面:
在這裏插入圖片描述
直接訪問GetServlet:Null值
在這裏插入圖片描述

轉到Hello:空值

在這裏插入圖片描述

再訪問GetServlet:

在這裏插入圖片描述

參考文獻

《【狂神說Java】JavaWeb入門到實戰》

視頻連接

2020.06.02

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章