JSP/Servlet | Servlets and JSP/HTML | Sending a Form from HTML to a Servlet
The basic mechanism for exchanging data between pages is still the form. Preparing a form on an HTML or JSP page and sending it to a servlet is one of the basics of using servlets.
To obtain the contents of a form sent to a servlet, use the HttpServletRequest instance prepared as an argument to doGet or doPost. You can extract the passed value with its getParameter method.
String variable = request.getParameter(name);
For example, for an input element such as <input type="text" name="txt1">, you can obtain the value by specifying getParameter("txt1"). All obtained values are String values, so convert numeric values and similar data as needed before processing.
You have already seen the getParameter method elsewhere. It is the getParameter method of the JSP implicit object request. This is not a coincidence. In fact, the implicit object request is the same kind of HttpServletRequest that is passed as an argument to doGet.
As explained earlier, JSP is actually a servlet. JSP scripts are also converted entirely into servlets. A servlet basically runs in the doGet method. From this, you can understand that an “implicit object” is a variable prepared in advance when JSP is converted into a servlet.
The implicit objects request and response are the doGet arguments themselves. The implicit object out can also be thought of as a variable that holds the PrintWriter obtained from response. Thinking this way shows that the knowledge from JSP can be used directly in servlets as well.
Here is a simple usage example.
index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Hello App Engine</title>
<style>
h1 {
font-size: 16pt;
background: #AAFFAA;
padding: 5px;
}
</style>
</head>
<body>
<h1>Hello App Engine!</h1>
<p id="msg">Write something and send it.</p>
<form method="post" action="/mygaeapp">
<table>
<tr>
<td>Input</td>
<td><input type="text" id="input" name="text1"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Send"></td>
</tr>
</table>
</form>
</body>
</html>
MyGaeAppServlet.java
package com.devkuma.mygaeapp;
import java.io.*;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class MyGaeAppServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
PrintWriter out = response.getWriter();
out.println("Hello, world!");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
String param = request.getParameter("text1");
PrintWriter out = response.getWriter();
out.println("<html><head></head><body>");
out.println("<h1>result</h1>");
out.println("<p>you typed: " + param + ".</p>");
out.println("</body></html>");
}
}
Place a simple form in index.html and display the received result in the MyGaeAppServlet servlet created earlier. You can see that the value is passed to the servlet very simply.