WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP Form processing


When we browse the web, we often need to submit information to the server and let the background program handle it. The browser uses the GET and POST methods to submit data to the server.


GET method

The GET method adds the requested encoding information to the URL. The URL and encoding information are separated by a "?". It looks like this:

http://www.welookups.com/hello?key1=value1&key2=value2

The GET method is a method for the browser to pass parameters by default. Some sensitive information, such as passwords, is recommended not to use the GET method.

When using get, the size of the transmitted data is limited (note that the number of parameters is not limited), and the maximum is 1024 bytes.


POST method

Some sensitive information, such as passwords, can be passed through the POST method. POST submission data is implicit.

POST submission data is not visible, GET is passed in the url (you can look at the address bar of your browser).

JSP uses getParameter () to get the passed parameters. The getInputStream () method is used to process the client's binary data stream request.


JSP read form data

  • getParameter (): Use the request.getParameter () method to get the value of the form parameter.

  • getParameterValues (): Get data like the checkbox class (same name, but multiple values). Receive array variables, such as checkbox type

  • getParameterNames (): This method can get the names of all variables, and this method returns an Enumeration.

  • getInputStream (): Call this method to read the binary data stream from the client.


Example of GET method using URL

The following is a simple URL, and use the GET method to pass the parameters in the URL:

http://localhost:8080/testjsp/main.jsp?name=welookups tutorial&url=http://ww.welookups.com

testjsp Is the project address。

The following is the JSP program of the main.jsp file used to process the form data submitted by the client, we use the getParameter () method to obtain the submitted data:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>
<h1>Use the GET method to read data</h1>
<ul>
<li><p><b>Site name:</b>
   <%= request.getParameter("name")%>
</p></li>
<li><p><b>URL:</b>
   <%= request.getParameter("url")%>
</p></li>
</ul>
</body>
</html>

Next we visit through the browser http://localhost:8080/testjsp/main.jsp?name=welookups tutorial&url=http://ww.welookups.com The output results are as follows:/p>


Example of GET method using form

The following is a simple HTML form that submits client data through the GET method Go to the main.jsp file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>

<form action="main.jsp" method="GET">
Site name: <input type="text" name="name">
<br />
URL: <input type="text" name="url" />
<input type="submit" value="submit" />
</form>

</body>
</html>

Save the above HTML code in the test.htm file. Place the file in the WebContent directory of the current jsp project (the same directory as main.jsp).

By visiting http: //localhost: 8080 /testjsp /test.html and submitting the form data to the main.jsp file, the demo Gif diagram is shown below:

Fill in the information in the two forms "Site Name" and "URL", and click the "Submit" button, it will output the results.


Example of POST method using form

Next let us use the POST method to pass the form data, modify the main.jsp and Hello.htm file code, as follows:

Code of main.jsp file:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>
<h1>Use POST method to read data</h1>
<ul>
<li><p><b>Site name:</b>
<%
//Solve the problem of code
String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8");
%>
   <%=name%>
</p></li>
<li><p><b>URL:</b>
   <%= request.getParameter("url")%>
</p></li>
</ul>
</body>
</html>

In the code, we use new String ((request.getParameter ("name")). getBytes ("ISO-8859-1"), "UTF-8") to convert the encoding to prevent The occurrence of code.

The following is the modified code of test.htm:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>

<form action="main.jsp" method="POST">
Site name: <input type="text" name="name">
<br />
URL: <input type="text" name="url" />
<input type="submit" value="submit" />
</form>

</body>
</html>

By visiting http://localhost:8080/testjsp/test.html Submit the form data to the main.jsp file, the demo Gif picture is shown below:


Pass Checkbox data to JSP program

Checkbox checkbox can pass one or more data.

The following is a simple HTML code, and save the code in the test.htm file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>

<form action="main.jsp" method="POST" target="_blank">
<input type="checkbox" name="google" checked="checked" /> Google
<input type="checkbox" name="welookups"  /> welookups tutorial
<input type="checkbox" name="welookups" checked="checked" /> 
                                                welookups
<input type="submit" value="Select website" />
</form>

</body>
</html>

The above code can be accessed in the browser as follows:

The following is the main.jsp file code, used to process checkbox data:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>
<h1>Read data from checkbox</h1>
<ul>
<li><p><b>Google Whether selected:</b>
   <%= request.getParameter("google")%>
</p></li>
<li><p><b>Whether the welookups tutorial is selected:</b>
   <%= request.getParameter("welookups")%>
</p></li>
<li><p><b>Whether welookups is selected:</b>
   <%= request.getParameter("welookups")%>
</p></li>
</ul>
</body>
</html>

By visiting http://localhost:8080/testjsp/test.html Submit the form data to the main.jsp file, the demo Gif diagram is as follows:


Read all form parameters

Here we will use getParameterNames () of HttpServletRequest to read all form parameters. This method can get the names of all variables, and this method returns an enumeration.

Once we have an Enumeration, we can call the hasMoreElements () method to determine whether there are still elements, and use the nextElement () method to get the name of each parameter.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>
<h1>Read all form parameters</h1>
<table width="100%" border="1" align="center">
<tr bgcolor="#949494">
<th>parameter name</th><th>Parameter value</th>
</tr>
<%
   Enumeration paramNames = request.getParameterNames();

   while(paramNames.hasMoreElements()) {
      String paramName = (String)paramNames.nextElement();
      out.print("<tr><td>" + paramName + "</td>\n");
      String paramValue = request.getParameter(paramName);
      out.println("<td> " + paramValue + "</td></tr>\n");
   }
%>
</table>
</body>
</html>

The following istest.Content of htm file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups tutorial(welookups.com)</title>
</head>
<body>

<form action="main.jsp" method="POST" target="_blank">
<input type="checkbox" name="google" checked="checked" /> Google
<input type="checkbox" name="welookups"  /> welookups tutorial
<input type="checkbox" name="welookups" checked="checked" /> 
                                                welookups
<input type="submit" value="Select website" />
</form>

</body>
</html>

Now we access the test.htm file through a browser to submit data, and the output result is as follows:

By visiting http: //localhost: 8080 /testjsp /test.html and submitting the form data to the main.jsp file, the demo Gif diagram is shown below:

You can try to use the above JSP code to read other objects, such as text boxes, radio buttons or drop-down boxes and other forms of data.