Servlets - 表单数据( Form Data)
当您需要将某些信息从浏览器传递到Web服务器并最终传递到后端程序时,您必须遇到很多情况。 浏览器使用两种方法将此信息传递给Web服务器。 这些方法是GET方法和POST方法。
GET方法
GET方法发送附加到页面请求的编码用户信息。 页面和编码信息由?分隔? (问号)符号如下 -
http://www.test.com/hello?key1 = value1&key2 = value2
GET方法是将信息从浏览器传递到Web服务器的默认方法,它会生成一个长字符串,显示在浏览器的Location:框中。 如果您要将密码或其他敏感信息传递给服务器,请勿使用GET方法。 GET方法有大小限制:请求字符串中只能使用1024个字符。
此信息使用QUERY_STRING标头传递,可通过QUERY_STRING环境变量访问,Servlet使用doGet()方法处理此类请求。
POST Method
将信息传递给后端程序的一般更可靠的方法是POST方法。 这以与GET方法完全相同的方式打包信息,而不是在一个?之后将其作为文本字符串发送? (问号)在URL中将其作为单独的消息发送。 此消息以标准输入的形式提供给后端程序,您可以解析并用于处理。 Servlet使用doPost()方法处理这种类型的请求。
使用Servlet读取表单数据
Servlet根据具体情况使用以下方法自动处理表单数据解析 -
getParameter() - 您调用request.getParameter()方法来获取表单参数的值。
getParameterValues() - 如果参数出现多次并返回多个值(例如复选框getParameterValues() ,则调用此方法。
getParameterNames() - 如果需要当前请求中所有参数的完整列表,请调用此方法。
使用URL获取GET方法示例
这是一个简单的URL,它将使用GET方法将两个值传递给HelloForm程序。
http://localhost:8080/HelloForm?first_name = ZARA&last_name = ALI下面给出了HelloForm.java servlet程序来处理Web浏览器给出的输入。 我们将使用getParameter()方法,这使得访问传递的信息变得非常容易 -
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloForm extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body>" +
"</html>"
);
}
}
假设您的环境设置正确,请按如下方式编译HelloForm.java -
$ javac HelloForm.java
如果一切顺利,上面的编译将产生HelloForm.class文件。 接下来,您必须在“Tomcat-installationdirectory”/ webapps/ROOT/WEB-INF/classes中复制此类文件,并在“Tomcat-installation-directory”/ webapps/ROOT/WEB-中的web.xml文件中创建以下条目: INF/
<servlet>
<servlet-name>HelloForm</servlet-name>
<servlet-class>HelloForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloForm</servlet-name>
<url-pattern>/HelloForm</url-pattern>
</servlet-mapping>
现在在浏览器的Location:框中键入http://localhost:8080/HelloForm?first_name=ZARA&last_name=ALI ,并确保在浏览器中触发上述命令之前已经启动了tomcat服务器。 这将产生以下结果 -
<h1 align="center">Using GET Method to Read Form Data</h1>
- First Name :ZARA
- Last Name :ALI
使用表格获取方法示例
这是一个使用HTML FORM和提交按钮传递两个值的简单示例。 我们将使用相同的Servlet HelloForm来处理此输入。
<html>
<body>
<form action = "HelloForm" method = "GET">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
将此HTML保存在文件Hello.htm中,并将其放在
尝试输入名字和姓氏,然后单击“提交”按钮以在运行tomcat的本地计算机上查看结果。 根据提供的输入,它将生成与上例中提到的类似的结果。
使用表单的POST方法示例
让我们在上面的servlet中做一点修改,这样它就可以处理GET和POST方法。 下面是HelloForm.java servlet程序,用于处理Web浏览器使用GET或POST方法给出的输入。
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloForm extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body>"
"</html>"
);
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
现在编译和部署上面的Servlet并使用Hello方法使用POST方法测试它,如下所示 -
<html>
<body>
<form action = "HelloForm" method = "POST">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
以下是上述表单的实际输出,尝试输入名字和姓氏,然后单击“提交”按钮以在运行tomcat的本地计算机上查看结果。
基于所提供的输入,它将产生与上述示例中提到的类似的结果。
将Checkbox数据传递给Servlet程序
如果需要选择多个选项,则使用复选框。
下面是示例HTML代码CheckBox.htm,用于包含两个复选框的表单
<html>
<body>
<form action = "CheckBox" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" checked = "checked" /> Maths
<input type = "checkbox" name = "physics" /> Physics
<input type = "checkbox" name = "chemistry" checked = "checked" />
Chemistry
<input type = "submit" value = "Select Subject" />
</form>
</body>
</html>
此代码的结果如下所示
下面给出了CheckBox.java servlet程序,用于处理Web浏览器为复选框按钮提供的输入。
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class CheckBox extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Checkbox Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>Maths Flag : </b>: "
+ request.getParameter("maths") + "\n" +
" <li><b>Physics Flag: </b>: "
+ request.getParameter("physics") + "\n" +
" <li><b>Chemistry Flag: </b>: "
+ request.getParameter("chemistry") + "\n" +
"</ul>\n" +
"</body>"
"</html>"
);
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
对于上面的例子,它会显示以下结果 -
<h1 align="center">Reading Checkbox Data</h1>
- Maths Flag : ::开
- Physics Flag: :: null
- Chemistry Flag: :上
阅读所有表格参数
以下是使用HttpServletRequest的getParameterNames()方法读取所有可用表单参数的通用示例。 此方法返回一个Enumeration,其中包含未指定顺序的参数名称
一旦我们有了Enumeration,我们就可以通过标准方式循环Enumeration,使用hasMoreElements()方法确定何时停止并使用nextElement()方法获取每个参数名称。
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Extend HttpServlet class
public class ReadParams extends HttpServlet {
// Method to handle GET method request.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading All Form Parameters";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<table width = \"100%\" border = \"1\" align = \"center\">\n" +
"<tr bgcolor = \"#949494\">\n" +
"<th>Param Name</th>"
"<th>Param Value(s)</th>\n"+
"</tr>\n"
);
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n<td>");
String[] paramValues = request.getParameterValues(paramName);
// Read single valued data
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<i>No Value</i>");
else
out.println(paramValue);
} else {
// Read multiple valued data
out.println("<ul>");
for(int i = 0; i < paramValues.length; i++) {
out.println("<li>" + paramValues[i]);
}
out.println("</ul>");
}
}
out.println("</tr>\n</table>\n</body></html>");
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
现在,使用以下表单尝试上面的servlet -
<html>
<body>
<form action = "ReadParams" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" checked = "checked" /> Maths
<input type = "checkbox" name = "physics" /> Physics
<input type = "checkbox" name = "chemistry" checked = "checked" /> Chem
<input type = "submit" value = "Select Subject" />
</form>
</body>
</html>
现在使用上面的表单调用servlet会产生以下结果 -
<h1 align="center">Reading All Form Parameters</h1>
参数名称 | 参数价值 |
---|---|
maths | on |
chemistry | on |
您可以尝试使用上面的servlet来读取任何其他表单的数据,这些数据包含其他对象,如文本框,单选按钮或下拉框等。