--Introduction --Parameter (html) --Servlet side --Method - setCharacterEncoding - getParameter
This time, using Servlet, I will summarize about receiving values from other html files </ b>. The code from the previous "Beginner's Servlet Understanding ①" has been taken over, so if you glance at the code and think "I can't do it." Please go back and check it ~
In addition, this article is ・ "I'm going to do Java now" ・ "I'm doing Java, but I want to review it." It is a rough content for people. I am also studying hard, so I do not guarantee the accuracy of the content. .. .. I hope it will help you in your learning (notepad for your own learning).
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Example 3</title>
</head>
<body>
<div align="center">
<!--At the destination URL "sample", sample<form>Submit the value of the input form in the tag-->
<!--When sending with "post" specified instead of "get" in method-->
<!--POST transmission is performed instead of GET transmission-->
<form action="sample" method="post" >
<!--The character string described in the text box is set to the value of the parameter name "requestParam"-->
<input type="text" name="requestParam" value="" >
<input type="submit" name="button" value="Send" >
</form>
</div>
</body>
</html>
When you have an html file like this A parameter is a name and value combination </ b> in the input tag inside the form tag. The parameter value "" (value entered in the text box) "" can be obtained with the parameter name "requestParam".
The code for receiving the value of the parameter [requestParam] in the html file 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 {
//To handle GET transmission, doGet method,
//Override of doPost method required to handle POST transmission
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//Character string in HTTP request, character code "UTF-Read with "8" (encode)
request.setCharacterEncoding("UTF-8");
//Get the parameter value of "requestParam" from 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>");
}
}
●setCharacterEncoding()
request.setCharacterEncoding("UTF-8");
A method that encodes (reads) the parameter value in the HTTP request with the character code specified in the argument. When the parameter value is specified as Japanese, it seems that it cannot be read correctly. (Because Servlet only interprets parameters as ASCII code by default), you need to convert the values with this method.
●getParameter()
String value = request.getParameter("requestParam");
A method for reading 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.
When I start to look up this kind of knowledge, the phenomenon of "explaining unknown words with unknown words" usually occurs, which is quite annoying. "Accuracy knowledge is important, but at first it is important to get an image." He said he was thinking as a beginner.
Then.
Recommended Posts