Java EE basis (15): ServletContext object introduction and use of




Foreword

Earlier, we learned:
the Java EE basis (12): Cookie introduction and use of objects , solve data sharing among different request .
Java EE basis (14): Session introduction and use objects to solve the deal with a different requesting user, require the same data problems .

This time we learn ServletContext object. It addresses the different users of data sharing.

A, ServletContext object introduction

1. What is

The official called the ServletContext servlet context. The server creates an object for each project, the object is to ServletContext object. This object globally unique, and all internal engineering servlet share this object. So called global application shared objects.

ServletContext look at the name, no one will think that this object is that each has a Servlet? Actually not, in fact, a web application corresponds to a ServletContext, so the ServletContext scope (scope) should be the entire application.

Now that you know, you create a web application only corresponds to a ServletContext. Words in your web project is only one ServletContext object.

2. Role

Solve the problem of different users to share the same data.

3. Principle

ServletContext is created by the server, a project and only one ServletContext object. No matter which of the Servlet get from the project, obtained are the same ServletContext object. The object is jointly owned by all users, different user-initiated request to get is the same object.

4. Life Cycle

Server starts to shut down the server

5. Scope

The entire project within

6. Features

  • Create a server
  • Users share
  • A project only and only one

7. A method used

Check out my blog post to learn moreupdating

Two, ServletContext use

1. How to get

Baidu entries:

ServletContext Servlet interface is the largest of the interface presents a view Servlet web application. Examples are obtained by the ServletContext GetServletContext () method, the HttpServlet GenericServlet of inheritance, and GenericServlet class HttpServlet class having both the method.

Explanation: We will find a query document ServletContext interface, this is not what we want. What we want is a ServletContext object.

Since the HttpServlet and no way to get ServletContext object, then we look at its parent GenericServlet.

We found that the parent class GenericServlet HttpServlet in a getServletContext () method, which returns a ServletContext object. Type returns ServletContext. At this point we should know how to get the ServletContext object.

ServletContext servletContext = this.getServletContext(); // 获取ServletContext对象

getServletContext () method is actually coming from the interface ServletConfig GenericServlet implemented in, ServletConfig interface has this method.

2. Get three ways

We know getServletContext () method is coming from somewhere, and this time we also know how to get ServletContext object. Then the next will explain three different ways to get ServletContext object

The following are three ways to get ServletContext object:

ServletContext servletContext = this.getServletContext(); // 获取ServletContext对象,方式1
ServletContext servletContext2 = this.getServletConfig().getServletContext(); // 获取ServletContext对象,方式2
ServletContext servletContext3 = req.getSession().getServletContext(); // 获取ServletContext对象,方式3
		

3. Data storage and access

And our previous Session storage and access to the same usage.

ServletContext servletContext = this.getServletContext(); // 获取ServletContext对象,方式1
servletContext.setAttribute("name", "value");
servletContext.getAttribute("name");

4. The current project path to get

ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath(""); // 获取项目下资源的绝对路径

5. Sample Code

Setting values ​​and get the value, get the absolute path of the resource under the project. The remaining can refer to Tomcat documents.

import java.io.IOException;

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;

/**
 * ServletContext简单使用
 * 
 * @author changsheng
 */
@WebServlet("/ServletContextServlet")
public class ServletContextServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.设置请求编码格式
		req.setCharacterEncoding("utf-8");
		// 2.设置响应编码格式
		resp.setHeader("content-type", "text/html;charset=utf-8");
		// 3.获取请求信息
		// 4.处理请求信息
		StringBuilder print = new StringBuilder();
		ServletContext servletContext = this.getServletContext(); // 获取ServletContext对象,方式1
		servletContext.setAttribute("name", "value"); // 设置值
		String name = (String)servletContext.getAttribute("name");
		print.append(name+"\n");
		String realPath = servletContext.getRealPath(""); // 获取项目下资源的绝对路径
		print.append(realPath);
		// 5.响应处理结果
		System.out.println(print); // 打印Session对象id
		resp.getWriter().write(print.toString());
	}

}

Related Links:

Previous: the Java EE basis (14): Session Object introduction and use

Current articles: the Java EE basis (15): ServletContext object introduction and use of

Next:updating

I learn Java EE line: My Java EE Learning Path

Published 86 original articles · won praise 104 · Views 6619

Guess you like

Origin blog.csdn.net/weixin_44034328/article/details/103857469