servlet upgrade (II)

A, Servlet interface methods defined

public interface Servlet {
    //初始化
    void init(ServletConfig var1) throws ServletException;
    //获取ServletConfig对象
    ServletConfig getServletConfig();

    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
  
    String getServletInfo();
    
    void destroy();
}

the getServletInfo (), this method will return a description Servlet that can return a string.
getServletConfig (), this method returns the object to the Servlet container ServletConfig init () method.

Two, ServletConfig interfaces
when initializing Servlet container Servlet, the Servlet Servlet container will init () passing a manner ServletConfig object
        String getServletName () // Get the name value servlet configuration in web.xml
        String getInitParameter () // Get servlet initialization parameters
        Enumeration getInitParameterNames () // get all servlet initialization parameter name

package com.lixu.servlet;

import java.io.IOException;
import java.rmi.ServerException;
import java.util.Enumeration;

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

/**
 * ServletConfig获取Servlet的初始化参数 
 * @author 
 */
public class ServletConfigDemo extends HttpServlet{

	/**
	 * 标识ID
	 */
	private static final long serialVersionUID = 1L;
	
	private ServletConfig sConfig;

	@Override
    public void init(ServletConfig config) throws ServletException {
        this.sConfig = config;
    }

	public void doGet(HttpServletRequest hServletRequest, HttpServletResponse hServletResponse)
			throws ServerException, IOException{
		
		 //获取web.xml指定的初始化参数
		 String param = this.sConfig.getInitParameter("name");
		 hServletResponse.getWriter().print(param);
		 
		 //换行
		 hServletResponse.getWriter().print("<hr/>");
		 
		 //通过ServletConfig对象获取web.xml初始化的值
		 Enumeration<String> e = sConfig.getInitParameterNames();
		 while (e.hasMoreElements()) {
			String name = e.nextElement();
			String value = sConfig.getInitParameter(name);
			hServletResponse.getWriter().print("name = " + value + "<br/>");
		 }
		 
	}
	
	public void doPost(HttpServletRequest hServletRequest, HttpServletResponse hServletResponse)
			throws ServerException, IOException{
		
		this.doGet(hServletRequest, hServletResponse);
	}
}

Three, ServletContext objects
        ServletContext object represents Servlet application. Each Web application is only one ServletContext object. In an application to deploy multiple containers distributed environment, each Web application on the Java virtual machine will have a ServletContext object.

ServletConfig by calling getServletContext method, you can also get ServletContext object.
ServletContext sContext = this.getServletConfig () getServletContext ( ).;

Four, ServletRequset interfaces
        Servlet container for each received Http request, creates a ServletRequest object, and pass the object to the Servlet Sevice () method.

public interface ServletRequest {
    int getContentLength();//返回请求主体的字节数
 
    String getContentType();//返回主体的MIME类型
 
    String getParameter(String var1);//返回请求参数的值
 
}

Five, ServletResponse interfaces
        javax.servlet.ServletResponse Servlet interface represents a response, before calling the Servlet Service () method, Servlet containers will create a ServletResponse object and put it as the second argument to the Service () method. ServletResponse hides the complexity of the process of sending a response to the browser.

public interface ServletResponse {
    String getCharacterEncoding();
 
    String getContentType();
 
    ServletOutputStream getOutputStream() throws IOException;
 
    PrintWriter getWriter() throws IOException;
 
    void setCharacterEncoding(String var1);
 
    void setContentLength(int var1);
 
    void setContentType(String var1);
}

Six, and the ServletRequest HttpServletRequest
-Service HttpServlet method of the type ServletRequsest converted received object type HttpServletRequest objects, types of objects to ServletResponse converted HttpServletResponse types of objects. It has been able to enforce such a conversion, because when you call Service method of Servlet, Servlet container always passed a HttpServletRequest object and HttpServletResponse objects, ready to use HTTP. Therefore, the conversion course not wrong.

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    HttpServletRequest request;
    HttpServletResponse response;
    try {
        request = (HttpServletRequest)req;
        response = (HttpServletResponse)res;
    } catch (ClassCastException var6) {
        throw new ServletException("non-HTTP request or response");
    }
 
    this.service(request, response);
}

Codec mode used in the service default: ISO-8859-1 encoded
description link: https: //blog.csdn.net/qq_19782019/article/details/80292110

Published 16 original articles · won praise 3 · Views 533

Guess you like

Origin blog.csdn.net/outdata/article/details/102553519
ii