Servlet series learning (2)

One, ServletContext class

(1) What is the ServletConfig object

  • ServletContext is an interface
  • ServletContext is a domain object
  • For a web project, Tomcat will only create a ServletContext object.

(2) What is a domain object

**A domain object is an object that can be stored like a Map collection (domain is the scope) **The domain in the domain object here refers to the effective operation range of the data stored in the object, the ServletContext and the object's data The effective scope of operation is the entire Web project.

Features Map collection Domain object
save data put() setAttribute()
retrieve data get() getAttribute()

(Three), the four functions of the ServletContext class

  1. Get the context parameters configured in web.xml
  2. Get the project path of the current project
  3. Get the sentence-pair path of the resource or directory on the server disk after the project is published on the server.
  4. You can store data like a map.

(Four), example demonstration

1. Get the ServletContext class object

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
//        获取ServletContext对像
//        方法一
        ServletConfig config = getServletConfig();
        ServletContext context = config.getServletContext();
//        方法二  通常都会用这种
        ServletContext context1 = getServletContext();
        
    }

2. Realization of specific functions

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
//        创建一个ServletContext对象
        ServletContext context = getServletContext();
//        获取web.xml中的配置参数<context-param>
        System.out.println("上下文参数url的值:"+context.getInitParameter("url"));
//        获取当前工程的工程路径
        System.out.println("工程路径:"+context.getContextPath());
/*
 *         获取工程发布到服务器上之后,资源或目录在服务器磁盘上的句对路径。
 *         在getRealPath()中的参数"/"就是把路径映射到工程的WebContent文件夹下
 *         */
        System.out.println("服务器磁盘上的绝对路径:"+context.getRealPath("/"));
//        获取img中的图片
        System.out.println("获取img中的图片路径:"+context.getRealPath("/img/servlet.PNG"));
    }

Insert picture description here

4. ServletContext object stores data


protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
    
    
        ServletContext context = getServletContext();
//        用setAttribute()存储数据
        context.setAttribute("abc","abcValue");
//      用getAttribute()取出数据
        System.out.println("从ServletContext域对象中获取数据abc的值:"+context.getAttribute("abc"));
    }

2. HTTP protocol related content

(1) GET request protocol

1. Request line information
Insert picture description here
2. Request body
Insert picture description here

(2) POST request protocol

1. Request line information:
Insert picture description here
2. Request header information
Insert picture description here
3. Request body information
Insert picture description here

(2) HTTP protocol format of the response

Insert picture description here
Insert picture description here

(3) Those in the page are GET requests and those are POST requests (3)

1. GET request
  • Enter the requested address in the browser address bar, and then hit enter
  • a tag Script, link, img, iframe introduce form tag method=GET
2. POST request is only in the state of method="post" under the form tag

(4) Common response codes

Response code meaning
200 Indicates that the server successfully processed the client request
302 Indicates a request to request a jump
304 Indicates that the client cache version is the latest
404 Indicates that the server has received the request, but your request has insufficient resources
500 Indicates that the server has received the request, but the server has an internal error (code)

Guess you like

Origin blog.csdn.net/weixin_44676935/article/details/104803066