Java web learning notes (request and response)

HttpServletResponse object

In the Servlet API, a GttpServletResponse interface is defined, which inherits the word ServletResponse and is used to encapsulate HTTP response messages.

Method for transmitting status code

When the Servlet sends back the response message to the client, it is necessary to set the status code in the response message. Two methods of sending the status code are defined in HttpServletResponse.

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. Ziyao can send the status line by setting the status code through the setStatus(int status) method. By default, the Web server sends a status line with a status code of 200.

2.sendError(int sc) method

This method is used to send a status code indicating error information. In the response object, two overloaded sendError() methods are provided.

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

The first one only sends the error message status code, and the second one can add a text message for prompt description in addition to sending the error message status code, which will appear in the body content sent to the client.

Send response header correlation

In the HttpServletResponse interface, a series of fields for setting the HTTP response header are defined.

Method declarationFunction description
void addHeader(String name,String value)Used to set the response header field of HTTP. The parameter name is used to specify the response header field, and the parameter value is used to specify the value of the response header field. The addHeader() method can add a response header field with the same name.
void setHeader(String name,String value)Used to set the response header field of HTTP. The parameter name is used to specify the response header field, and the parameter value is used to specify the value of the response header field. The setHeader() method overwrites the header field with the same name.
void addIntHeader(String name,int value)It is used to set the response header containing integer value, avoiding the trouble of converting int type to String type when using addHeader() and setHeader() methods.
void setIntHeader(String name,int value)It is used to set the response header containing integer value, avoiding the trouble of converting int type to String type when using addHeader() and setHeader() methods.
void setContentLength(int len)This method is used to set the size of the response message entity content, in bytes.
void setContentType()This method is used to set the MINE type of Servlet output content. For HTTP, it is to set the value of the content type response header field. If the response header is text, you can also set the character encoding.
void setLocale(Local loc)This method is used to set the localized message of the response message.
void setCharacterEncoding(String charset)This method is used to set the character encoding used for the output content. For HTTP, it is the part that sets the character set encoding in the content type header field.

Methods related to 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 the binary data in the byte array, so you want 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 the opposite direction of PrintWriter type can directly output character text content, you need to use the getWriter() method to output web documents with all character text.
Example: getOutputStream() method


getWriter method


Note: both getOutputStream() and getWriter() methods of the response object can send the response message body, but they cannot be used at the same time.

HttpServletResponse application

Chinese output garbled

Computer data is stored in binary form, so when transmitting text, character and byte conversion will occur. If the code table used for encoding and decoding is inconsistent, it will lead to garbled code.
Example: the code is as follows:

package cn.itcast.chapter04.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class EX05
 */
@WebServlet("/EX05")
public class EX05 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String data="China";
		PrintWriter print=response.getWriter();
		print.write(data);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


Start Tomcat server

All of them??, The character output stream of the response object is encoded using the character table of ISO 8859-1, which is not compatible with Chinese.
At this point, you need to add a code between lines 24 and 25:

response.setCharacterEncoding("UTF-8");



It can be seen that it is still garbled, which is caused by the decoding error of the browser, because the encoding format of the character output stream of the response object is UTF-8 and the decoding format of the browser is GB2312.
(1) We can solve this problem by modifying the decoding method of the browser. The decoded format of the browser.
Google browser needs to download a Charset plug-in and change the page decoding format.

(2) We can make changes to our code

Page refresh and jump regularly

In Web development, we sometimes encounter the need to jump to the page regularly. A Refresh header field is defined in HTTP, which can notify the browser to automatically Refresh and jump to other pages within a specified time.
Example:


We can also make the current page refresh automatically.

request redirections

In some cases, according to the requirements of the client, a Servlet class may not be able to do all the work. Request redirection means that after the Web server receives the request from the client, it may not be able to access the Web resource pointed to by the URL of the current request due to some conditions, but points to a new path to let the client resend the request.
In order to realize request redirection, a sendredirect () is defined in the HttpServletResponse interface, which is used to generate 302 response code and Kocation response header, so as to notify the URL specified in the Location response header. The complete syntax of sendRedirect() is:

public void sendRedirect(java.lang.String location) throws java.io.IOException)

Example: the code is as follows:
E05:

package cn.itcast.chapter04.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class EX05
 */
@WebServlet("/EX05")
public class EX05 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		if(("root").equals(username)&&("123456").equals(password)){
			response.sendRedirect("/chapter04/welcome.html");
		}else{
			response.sendRedirect("/chapter04/login.htm	l");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

login.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/chapter04/EX05" method="post">
	user name:<input type="text" name="username" />
	password:<input type="password" name="password" />
	<input type="submit" value="Sign in"/>
</form>
</body>
</html>

welcome.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Welcome, login succeeded
</body>
</html>

result:

HttpServletRequest object

Related methods of obtaining request line information

When accessing the Servlet, the request line will not contain the request method, request resource name, request path and other messages. In order to obtain these messages, a series of methods for obtaining the request line information are defined in the HttpservletRequest interface.

Method declarationFunction description
String getMethod()This method is used to obtain the request mode in the HTTP request message
String getRequestURL()This method is used to obtain the resource name part in the request line, that is, the part after the host and port of the URL and before the parameter
String getQueryString()This method is used to obtain the parameter part in the request line, that is, the content after the question mark after the resource path.
String getProtocol()This method is used to obtain the protocol name and version in the request line.
String getContextPath()This method is used to obtain the path belonging to the We application in the requested URL. This path starts with "/", which means that it is equivalent to the following directory of the whole Web site, and the end of the path does not contain "/.
String getPathInfo()This method is used to get the extra path message in the URL. The extra path is the content in the request URL after the path to the Servlet and before the query parameters.
String getPathTranslated()This method is used to obtain the real path corresponding to the additional path information in the URL.
String getServletPath()This method is used to obtain the name of the Servlet or the path of the Servlet mapping.

Method of getting request message header

Some methods to get the message header.

Method declarationFunction description
String getHeader(String name)This method is used to obtain the value of a specified header field. If the request message does not contain the specified header field, if the request message contains multiple header fields, return the value of the first header field, and if not, return null.
Enumeration getHeaders(String name)This method returns an Enumeration collection object, which is composed of all header field values of a specified name in the request message. In most cases, a header field name appears only once in the request, but it may appear multiple times.
Enumeration getHeaderNames()This method is used to get all Enumeration objects that contain the request header field.
int getIntHeader(String name)This method is used to get the header field of the specified name and convert its value to int type. If it does not exist, the return value is - 1
long getDateHeader(String name)This method is used to obtain the value of the specified header field and convert it into a long integer representing date / time in GMT time format.
String getContentType()This method is used to obtain the value of the content type header field, and the result is String
int getContentLength()This method is used to obtain the value of the content length header field, and the result is of type int
String getCharacterEncoding()This method is used to return the character set code of the test question part of the request message. It is usually extracted from the content type header field, and the result is String.

Example:

package cn.itcast.chapter04.servlet;

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

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

/**
 * Servlet implementation class EX05
 */
@WebServlet("/EX05")
public class EX05 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		Enumeration headerNames=request.getHeaderNames();
		while(headerNames.hasMoreElements()){
			String headername=(String)headerNames.nextElement();
			out.println(headername+":"+request.getHeader(headername)+"<br>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Method for obtaining request message body

(1) getInputStream() method
This method is used to get the ServletInputStream object representing the content of the entity. If the object is fat text, the message body can only be obtained through the getInputStream() method.
(2) getReader() method
This method is used to obtain the BufferedReader object representing the entity content. The object will convert the byte data of the entity content into a text string according to the specified character set encoding in the request message. When calling getReader() method, you can use setCharcterEncoding() method to specify the character encoding of the BufferedReader object. If the character encoding of the entity content is not specified, The default is ISO8859-1.
Example:
(1) Write a user login form login html

(2) Write a file for receiving the request message body. The code is as follows

package cn.itcast.chapter04.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;

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

/**
 * Servlet implementation class EX05
 */
@WebServlet("/EX05")
public class EX05 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		InputStream in=request.getInputStream();
		byte[] buffer=new byte[1024];
		StringBuilder sb=new StringBuilder();
		int len;
		while((len=in.read(buffer))!=-1){
			sb.append(new String(buffer,0,len));
		}
		System.out.print(sb);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Results enter username itcast password 123

HttpServletRequest application

Get request parameters

In the HttpSdervletRequest interface, a series of methods to obtain request parameters are defined.

Method declarationFunction description
String getParameter(String name)This method is used to obtain the parameter value of a specified name. If the request message does not specify a parameter value, null is returned. If no value is set, a specified empty string is returned. If multiple parameters are included, the first parameter is returned.
String[] getParameterValues(String name)There can be multiple parameters with the same name in the HTTP request message. If you want to obtain all parameter values in the HTTP request message, you need to use getParameterValues(String name). This method returns a String type array.
Enumeration getParameterName()The getParameterNames() method is used to return an Enumeration object containing all the parameter names in the request message. Once again, all parameters in the request message can be traversed.
Map getParameterMap()getParameterMap() is used to transfer all parameter names and values in the request message into a Map object for return.

Example:
(1) Modify the previous login Add 3 check boxes to HTML file

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/chapter04/EX05" method="post">
	user name:<input type="text" name="username" />
	password:<input type="password" name="password" />
	Hobbies:
		<input type="checkbox" name="hobby" value="sing">sing
		<input type="checkbox" name="hobby" value="dance">dance
		<input type="checkbox" name="hobby" value="football">Football<br>
		<input type="submit" value="Submit"/>
</form>
</body>
</html>

Write EX05 code to obtain request parameters.

package cn.itcast.chapter04.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;

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

/**
 * Servlet implementation class EX05
 */
@WebServlet("/EX05")
public class EX05 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("username");
		String password=request.getParameter("password");
		System.out.println("user name:"+name);
		System.out.println("password:"+password);
		String[] hobby=request.getParameterValues("hobby");
		System.out.println("Hobbies:");
		for(int i=0;i<hobby.length;i++ ){
			System.out.println(hobby[i]+" ");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Run Tomcat

Chinese garbled code of request parameters

There will be garbled code when filling in but entering Chinese.


When the browser transmits the request parameters, the default encoding format is GBK, but the default ISO8859-1 is used when decoding, so the parameter information printed on the console will be garbled.
A setC act Encoding() method is provided in the HttpServletResponse interface, which is used to set the decoding method of the request object. Add this code to EX05:

request.setCharacterEncoding("utf-8");

This method can only be used in POST mode. This code should be added in GET mode.

name=new String(name.getBytes("iso8859-1"),"utf-8");


Get network connection information

In the HttpServletRequset interface, a series of methods for obtaining client network connection information are defined.

Method declarationFunction description
String getRemoteAddr()This method is used to obtain the IP address of the client.
String getRemooteHost()This method is used to obtain the complete host name of the requested client. If there is no way to resolve the host name of the client, the IP address of the client will be returned.
int getRemotePort()This method is used to obtain the port number of the network connection of the requesting client.
String getLocalAddr()This method is used to obtain the IP address on the Web server that receives the current network request.
String getLocalName()This method is used to obtain the host name corresponding to the current network connection IP received on the Web server.
int getLocalPort()This method is used to obtain the port number on the Web server to receive the current network connection
String getServerName()This method is used to obtain the Host name pointed to by the current request, that is, the Host name part corresponding to the Host header field in the HTTP request message.
int getServerPort()This method is used to obtain the server port number of the current request connection, that is, the port number corresponding to the Host header field in the HTTP request message.
String getScheme()This method is used to get the requested protocol name.
StringBuffer getRequestURL()This method is used to obtain the complete URL when the client sends a request, excluding the subsequent query parameter information.

Pass data through the Request object

The Request object can not only obtain data, but also transfer data through a book. A series of methods for operating attributes are defined in the servlet Request interface.
(1) setAttribute() method
This method is used to associate an object with a name and store it in the ServletRequest object.
The complete syntax is defined as:

public void setAttribute(java.lang.String name,java.lang.Object o);

If an attribute with the specified name already exists in the ServletRequest object, setattribute() method will delete the original attribute first, and then add a new attribute. If the attribute passed to setattribute() method is null, the attribute with the specified name will be deleted.
(2) getAttribute() method
This method is used to return the property object of the specified name from the ServletRequest object. Its complete syntax is defined as:

public java.lang.String getAttribute(java.lang.String name);

(3) removeAttribute() method
This method deletes the property with the specified name from the ServletRequest object.

public voidremoveAttribute(java.lang.String name)

(4) getAttributeNames() method
This method is used to return an Enumeration object containing all attribute names in the ServletRequest object. On this basis, all attributes of ServletRequest can be traversed.

public java.util.Enumration getAttributeNames();

Application of RequestDispatcher object

RequestDispatcher interface

When a Web resource receives a client request and wants the server to notify another resource to process the request, in addition to using the sendRedirect() method to redirect the request, it can also be implemented through the instance object of the RequestDispatcher interface.
Method to get RequestDispatcher object:
RequestDispatcher(String path): returns the RequestDispatcher object that encapsulates the resources specified by a path, where the parameter path must start with "/", which is used for the root directory of the current Web application when the table.
RequestDispatcher the method to process Servlet requests.

Method declarationFunction description
forward(ServletRequest request,ServletResponse reponse)This method is used to transfer requests from one Servlet to another Web resource. In the Servlet, you can initially process the request, and then pass the request to other resources for response by calling this method. This method must be called before the response is provided to the client.
include(ServletRequest request,ServletResponse response)This method is used to include other resources as the content of the current response.

The include() method can include requests, and forward() can forward requests.

Request forwarding

In a Servlet, if the current Web resource does not want to process its access request, it can pass the current request to other Web resources for processing through the forward() method, which is called request forwarding.
Example:
Use the forward() method in Ex05 to forward requests.

package cn.itcast.chapter04.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;

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

/**
 * Servlet implementation class EX05
 */
@WebServlet("/EX05")
public class EX05 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setAttribute("name", "word");
		RequestDispatcher dispatcher=request.getRequestDispatcher("/Ex03");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Ex03 Code:

package cn.itcast.chapter04.servlet;

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

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;

/**
 * Servlet implementation class Ex03
 */
@WebServlet("/Ex03")
public class Ex03 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		String name=(String)request.getAttribute("name");
		if(name!=null){
			out.println("Hello"+name+"<br/>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}

The result is

It can be seen that the forward() method performs request conversion, and the request in EX05 is converted to Ex03.

Request contains

Request inclusion refers to forwarding the Servlet request to other Web resources for processing by using the include() method. Different from request forwarding, the response message returned in the request inclusion includes both the response message of the current Servlet and the response message made by other Web resources.
The code is as follows:
EX05:

package cn.itcast.chapter04.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;

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

/**
 * Servlet implementation class EX05
 */
@WebServlet("/EX05")
public class EX05 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		RequestDispatcher rd=request.getRequestDispatcher("/Ex03?p1=abc");
		out.println("before includeing"+"<br>");
		rd.include(request, response);
		out.println("after including"+"<br>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Ex03:

package cn.itcast.chapter04.servlet;

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

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;

/**
 * Servlet implementation class Ex03
 */
@WebServlet("/Ex03")
public class Ex03 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		out.println("China"+"<br>");
		out.println("URI:"+request.getRequestURI());
		out.println("QueryString:"+request.getQueryString()+"<br>");
		out.println("Parameter:"+request.getParameter("p1"));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}

As a result, there was garbled code.

We need to add it directly on lines 27 and 28 of EX05

response.setContentType("text/html;charset=utf-8");

The result is:

Keywords: Java Front-end server

Added by smacpettit on Thu, 13 Jan 2022 13:17:17 +0200