JavaWeb-ServletContext (file read in two ways resources)

What is ServletContext

ServletContext is representative of a context object (web application objects) of a web application, the web application are packaged inside information, a corresponding one of ServletContext application.

ServletContext lifecycle

Will be created when a server is started, destroyed when the server shuts down.

How to get context

  • Which is acquired by a parameter init method ServletConfig
  • Direct access to the HttpServlet them:
this.getServletContext()

This approach was essentially to come and go through the config acquired

To get global initialization parameters

Initialization parameters which can not come one Servlet configuration. In the outermost come and configuration

<?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">
    <context-param>
        <param-name>name</param-name>
        <param-value>youyuan</param-value>
    </context-param>

</web-app>

Obtaining global initialization parameters:

package org.youyuan.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/myservlet")

public class myServlet extends HttpServlet{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("servlet");
        ServletContext servletContext = this.getServletContext();
        System.out.println(servletContext.getInitParameter("name"));      
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

Print out the console:
Here Insert Picture Description

Web applications get the absolute path to the resource of a particular resource, read the contents of the file

The first:
Here Insert Picture Description
db.properties:

name = zs
age = 23
@WebServlet("/getPropertiesServlet")
public class getPropertiesServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        /*获取相对路径的输入流对象*/
        InputStream resourceAsStream = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        System.out.println("name="+properties.getProperty("name"));
        System.out.println("age="+properties.getProperty("age"));

    }

}

Console print the results:
Here Insert Picture Description

The second:


@WebServlet("/getPropertiesServlet2")
public class servlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //获取文件绝对路径
        String realPath = servletContext.getRealPath("/WEB-INF/classes/db.properties");
        FileInputStream inputStream = new FileInputStream(realPath);
        Properties properties = new Properties();
        properties.load(inputStream);
        System.out.println("name="+properties.getProperty("name"));
        System.out.println("age="+properties.getProperty("age"));
    }

}


Console print the results:
Here Insert Picture Description

ServletContext is a domain object

  • What is a domain? Capable of storing data.

  • Domain object? It is possible to access the data object.

  • ServletContext scope domain objects?
    1. The entire web application.
    2. All web resources can be accessed data
    3. Data can be shared.

  • After completion of writing data to get ServletContext inside
    context.setAttribute (String name, Object value) ;

  • After completion of acquisition ServletContext, extracted data is stored by name
    context.getAttribute (String name);

  • After completion of acquisition ServletContext, delete the specified name value
    Context.removeAttribute (String name);

  • Long as it is a domain object, there are several methods substantially

Published 25 original articles · won praise 0 · Views 280

Guess you like

Origin blog.csdn.net/qq_42219004/article/details/105281716