[Jsp] lesson 3 common objects of Servlet

Posted by thna_brianb on Sun, 10 Oct 2021 05:24:48 +0200

In the process of learning Servlet Technology, many common objects need to be used, and their usage is also very important. Here we first introduce the first common object, ServletConfig.

summary

1. In the Servlet configuration file, you can use one or more < init param > tags to configure some initialization parameters for the Servlet.
2. It is configured in the Servlet tag of the registration information
3. When the servlet is configured with initialization parameters, the web container will automatically encapsulate these initialization parameters into the ServletConfig object when creating the servlet instance object
4. When calling the init method of the servlet, the ServletConfig object is passed to the servlet, and then the programmer can get the initialization parameter information of the current servlet through the ServletConfig object.

Code step implementation

1. Add static data to the configuration file

  The process of creating a project will not be repeated here. After that, create a new package file and create a Servlet through class

The init method with parameters is rewritten here

package com.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class Servlet1 extends HttpServlet {
	
	public Servlet1() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

}

  Then register the Servlet in the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Day03Jsp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>Servlet1</servlet-name>
  	<servlet-class>com.servlet.Servlet1</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>Servlet1</servlet-name>
  	<url-pattern>/Servlet1</url-pattern>
  </servlet-mapping>
  
</web-app>

Add static data in the < servlet > tag. The format of static data must be:

<init-param>
  		<param-name>hote</param-name>
  		<param-value>hotel</param-value>
  	</init-param>

  Add the above format code to the < servlet > tag

<servlet>
  	<servlet-name>Servlet1</servlet-name>
  	<servlet-class>com.servlet.Servlet1</servlet-class>
  	<init-param>
  		<param-name>hote</param-name>
  		<param-value>hotel</param-value>
  	</init-param>
  </servlet>

  If you need to add multiple static data, just continue to add in this format:

<servlet>
  	<servlet-name>Servlet1</servlet-name>
  	<servlet-class>com.servlet.Servlet1</servlet-class>
  	<init-param>
  		<param-name>hote</param-name>
  		<param-value>hotel</param-value>
  	</init-param>
  	<init-param>
  		<param-name>money</param-name>
  		<param-value>1000 ten thousand</param-value>
  	</init-param>
  </servlet>

Then, in Servlet1, the static data in Web.xml is obtained through the ServletConfig object

@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		String a=config.getInitParameter("hote");
		String b=config.getInitParameter("money");
		System.out.println(a+"---"+b);
	}

Run the project on the tomcat server and access Servlet1 according to the virtual address of the Servlet. You can see the static data obtained from the web.xml file on the console. The effect is not shown here. Beginners please run according to the code to see the effect.

Then, when there is a large amount of static data, it is obviously too slow to use the getInitParameter(name) method to obtain it one by one. Next, let's look at another method to obtain the names of all static data, and then obtain all static data according to the loop traversal and name. The code is as follows:

//When there are a lot of static data, you can't use the above methods to obtain one by one
		//Circular acquisition should be performed
		//Get the names of all static data, and store the names of all static data in the enumeration type
		Enumeration<String> e= config.getInitParameterNames();
		while (e.hasMoreElements()) {
			String name=e.nextElement();//hotel
			String value=config.getInitParameter(name);
			System.out.println(name+"####"+value);
		}

The running results will not be repeated here. Beginners can run by themselves, view the results, and also obtain all static data.

2.Java annotation creates Servlet class and adds static data

After Servlet 3.0, annotation is provided to create and register Servlet classes. Adding static data also corresponds to Java annotation. Its writing form is as follows:

package com.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Servlet2
 */
@WebServlet(urlPatterns = "/Servlet2",initParams = {
		@WebInitParam(name = "name",value = "Li Si"),
		@WebInitParam(name = "age",value = "20"),
		@WebInitParam(name = "sex",value = "male")
})
public class Servlet2 extends HttpServlet {
	

}

Registering servlets and adding static data by annotation will be simpler than configuring in Web.xml file. Beginners can master and choose to use them by themselves. Then the data obtained by the ServletConfig object is the same. Two init initialization methods are provided in Servlet. This time, we rewrite the init method without ServletConfig parameter. The code is as follows:

@Override
    public void init() throws ServletException {
    	super.init();
    	ServletConfig config=getServletConfig();
    	String a=config.getInitParameter("name");
    	String b=config.getInitParameter("age");
    	String c=config.getInitParameter("sex");
    	System.out.println(a+"---"+b+"---"+c);
    	
    	String d=config.getInitParameter("money");
    	System.out.println(d);
    	//Summary: the ServletConfig object will only get the static data of each Servlet itself, not the static data defined by other servlets
    }

Because the init method does not take the ServletConfig parameter, we can obtain the ServletConfig object through the getServletConfig() method defined in the Servlet interface, and then obtain the corresponding static data according to the name. In addition, we also conducted a test and tried to obtain the static data defined in Servlet1 in Servlet2 class. According to the results of running and viewing, it is impossible to obtain it.

Therefore, we get the following summary:

The ServletConfig object will only get the static data of each Servlet itself, not the static data defined by other servlets

This article is described here. I hope beginners can better master this part of knowledge.

 

Topics: Java servlet