Chapter 4 request and response

Keywords: Java Network Protocol http

Browser access Servlet process:

HttpServletResponse object:
It inherits from the ServletResponse interface and is specially used to encapsulate HTTP response messages. It defines the method of sending response status code, response message header and response message body to the client.

Methods related to sending status codes include:
1. setStatus(int status) method
This method is used to set the status code of the HTTP response message and generate a response status line.
(note that the default status code of the Web server is 200)
2. sendError(int sc) method
This method is used to send a status code indicating error information. For example, 404 status code indicates that the resource requested by the client cannot be found. In the response object, two overloaded sendError(int sc) methods are provided, as follows.

public void sendError(int code) throws java.io.IOException
public void sendError(int code, String message) throws java.io.IOException

In the above overloaded two methods, the first method only sends the status code of error information, while the second method can add a text message for prompt description in addition to sending the status code, which will appear in the body content sent to the client.

Methods related to sending response message headers:

Method of sending response message body:
1. getOutputStream() method
The byte output stream object obtained by this method is of type ServletOutputStream. Since ServletOutputStream is a subclass of OutputStream, it can directly output binary data in byte array. Therefore, to output the response body in binary format, you need to use the getOutputStream() method.
2. getWriter() method
The character output stream object obtained by this method is of type PrintWriter. Because PrintWriter type objects can directly output character text content, to output web documents with all character text, you need to use the getWriter() method.

package cn.itcast.servlet;

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

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

public class servlet02 {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ServletConfig config=this.getServletconfig();
		String name=config.getServletName();
		System.out.println("servlet name:"+name);
		
		Enumeration<String> er =config.getInitParameterNames();
		while(er.hasMoreElements()) {
			String name1=er.nextElement();
			String valule2=config.getInitParameter(name1);
			
			System.out.println("Global initialization parameter name:"+name1);
			System.out.println("Value of global initialization parameter:"+valule2);
		}
		System.out.println("----+-------");
		
		ServletContext context=this.getServletContext();
		
		Enumeration<String> er1=context.getInitParameterNames();
		while(er.hasMoreElements()) {
			String name1=er1.nextElement();
			String valule2=context.getInitParameter(name1);
			
			System.out.println("Global initialization parameter name:"+name1);
			System.out.println("Value of global initialization parameter:"+valule2);
	}
}

	private ServletConfig getServletconfig() {
		// TODO Auto-generated method stub
		return null;
	}

	private ServletContext getServletContext() {
		// TODO Auto-generated method stub
		return null;
	}

	
}

HttpServletRequest object:
It inherits from the ServletRequest interface and is specially used to encapsulate HTTP request messages. It defines the relevant methods to obtain the request line, request header and request message body.

Methods to get the request line:

package cn.itcast.servlet;

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

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet03 extends httpservlet{
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ServletContext context=this.getServletContext();
		Enumeration<String> er =context.getInitParameterNames();
		while(er.hasMoreElements()) {
			String name1=er.nextElement();
			String valule2=context.getInitParameter(name1);
			
			System.out.println("Global initialization parameter name:"+name1);
			System.out.println("Value of global initialization parameter:"+valule2);
		}
	}

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


Posted by freynolds on Tue, 16 Nov 2021 01:11:34 -0800