[Java] [Java] Servlet understanding for beginners-②

2 minute read

table of contents

  • Introduction
  • Parameter (html)
  • Servlet side
  • Method -setCharacterEncoding -getParameter
  • in conclusion


This time, I will summarize the value reception from other html files using Servlet. I have taken over the code from the previous [Understanding Servlet for beginners ①], so if you take a quick look at the code and think “it’s impossible”, Please go back and check~

In addition, this article · “I’m going to do Java right now” ―― “I’m doing Java, but I want to review it a lot~” It is a miscellaneous content for people. The author is also studying hard, so I cannot guarantee the accuracy of the content. .. .. I hope it will help everyone’s learning (memo pad for their own learning).

Parameter (html)

<!DOCTYPE html>
<meta charset="UTF-8"/>
<title>Example 3</title>
    <div align="center">
        <!-- Send the value of the input form in the sample<form> tag with the destination URL "sample" -->
        <!-- If you specify "post" instead of "get" for method and send -->
        <!-- POST is sent instead of GET -->
        <form action="sample" method="post">
            <!-- The character string described in the text box is set as the value of the parameter name "requestParam" -->
            <input type="text" name="requestParam" value="">
            <input type="submit" name="button" value="Send">

For an html file like this, A parameter is a name and value combination in the input tag in the form tag. Parameter value “requestParam” and parameter value ““(value entered in text box)”” can be obtained.

Servlet side

The code to receive the value of the parameter [requestParam] in the html file of ↑ is ↓.

package parameterPackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ParameterServlet extends HttpServlet {

    // doGet method to handle GET submission,
    // doPost method must be overridden to handle POST submission
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        // Read the character string in the HTTP request with the character code "UTF-8" (encoding)
        // Get the parameter value of "requestParam" from the HTTP request
        String value = request.getParameter("requestParam");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        // output the value of value
        out.println("<body>" + value + "</body>");





Method that encodes (reads) the parameter value in HTTP request with the character code specified in the argument. It seems that if you specify the parameter value as Japanese, it cannot be read correctly. (By default, Servlet interprets parameters as ASCII code only), so it is necessary to convert the value with this method.

● getParameter()

String value = request.getParameter("requestParam");

Method to read the parameter value sent (requested) by GET/POST. Since the first argument “HttpServletRequest” of the doGet()/doPost() method of the servlet corresponds to the Http request, the object request is created and read.

in conclusion

With this system knowledge, when I start to look into it, the phenomenon that “unknown words are explained by unknown words” usually occurs, which is quite troublesome. “It’s important to have accurate knowledge, but it’s important to get an image at first.” I was thinking that I was a beginner.