首页 /  技术专区  /  Servlet 宽屏模式 >

ServletContext对象

ServletContext对象

web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象, 它代表了当前的web应用; 


共享数据

我在这个Servlet中保存的数据,可以在另外一个servlet中拿到;

1.新建一个javaweb的maven项目:

image.png

2.新建两个servlet:

HelloServlet:

package com.allen.servlet;

import javax.servlet.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 HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //this.getInitParameter(); 初始化参数
        //this.getServletConfig(); servlet配置
        //this.getServletContext(); servlet上下文

        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("userName", "allen"); //将一个数据保存到servlet中,键为userName,值为allen


    }

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

GetServlet:

package com.allen.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 GetServlet extends HttpServlet {

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

        ServletContext servletContext = this.getServletContext();
        String userName = (String) servletContext.getAttribute("userName");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字:" + userName);
    }

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

web.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.allen.servlet.HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>getc</servlet-name>
    <servlet-class>com.allen.servlet.GetServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>getc</servlet-name>
    <url-pattern>/getc</url-pattern>
  </servlet-mapping>

</web-app>

3.配置tomcat,启动测试:

第一次进入getc:

image.png

调用helloservlet之后:

image.png

结果:

image.png

获得初始化参数:

web.xml

<!-- 配置一些web应用的初始化参数 -->
<context-param>
  <param-name>url</param-name>
  <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>

<servlet>
  <servlet-name>ServletDemo03</servlet-name>
  <servlet-class>com.allen.servlet.ServletDemo03</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>ServletDemo03</servlet-name>
  <url-pattern>/ServletDemo03</url-pattern>
</servlet-mapping>

ServletDemo03

package com.allen.servlet;

import javax.servlet.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 servletContext = this.getServletContext();
        String url = servletContext.getInitParameter("url");
        resp.getWriter().print(url);

    }

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

访问ServletDemo03测试:

image.png

请求转发:

image.png

web.xml

<servlet>
  <servlet-name>ServletDemo04</servlet-name>
  <servlet-class>com.allen.servlet.ServletDemo04</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>ServletDemo04</servlet-name>
  <url-pattern>/ServletDemo04</url-pattern>
</servlet-mapping>

ServletDemo04

package com.allen.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 servletContext = this.getServletContext();
        //RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/ServletDemo03"); //转发的请求路径
        //requestDispatcher.forward(req, resp); //调用forward实现转发

        servletContext.getRequestDispatcher("/ServletDemo03").forward(req, resp);
    }

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

访问ServletDemo04测试是否被转发到ServletDemo03:

image.png

读取资源文件:

Properties

在java目录下新建properties

●在resources目录下新建properties

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

思路:需要一个文件流;


web.xml

<servlet>
  <servlet-name>ServletDemo05</servlet-name>
  <servlet-class>com.allen.servlet.ServletDemo05</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>ServletDemo05</servlet-name>
  <url-pattern>/ServletDemo05</url-pattern>
</servlet-mapping>

ServletDemo05

package com.allen.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 in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

        Properties properties = new Properties();
        properties.load(in);
        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 {
        this.doGet(req, resp);
    }
}

测试:

image.png


上一篇: Servlet原理
头像
0/200
图片验证码