在本 Java Servlet 教程中,我将指导您如何使用Java Servlet从服务器端的 HTML 表单中读取常用输入字段的值。
您知道,处理以 HTML 页面表示的表单数据是 Web 开发中非常常见的任务。一个典型的场景是用户填写表单的字段并提交。服务器将根据提交的数据处理请求,并将响应发送回客户端。下图描述了在服务器端使用 Java servlet 的工作流:
要在 HTML 中创建表单,我们需要使用以下标签:
为了使表单与 Java servlet 一起工作,我们需要为标记指定以下属性:
例如,以下是登录表单的 HTML 代码:
1
2
3
4
5
|
< form name = "loginForm" method = "post" action = "loginServlet" >
Username: < input type = "text" name = "username" /> < br />
Password: < input type = "password" name = "password" /> < br />
< input type = "submit" value = "Login" />
form >
|
此表单在浏览器中如下所示:
在服务器端,我们需要创建一个映射到 URL 的 Java servlet:loginServlet,在表单的 action 属性中指定。以下是 servlet 的代码:
1
2
3
4
5
6
7
8
9
10
11
|
@WebServlet ( "/loginServlet" )
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// code to process the form...
}
}
|
请注意,servlet 的 URL 由servlet 类之前的@WebServlet注释指定。当用户提交上面的登录表单时,servlet 容器会调用servlet 的doPost()方法。通常,我们将在doPost()方法中执行以下任务:
为了读取表单字段的值,HttpServletRequest接口提供了以下方法:
请注意,上述方法还可以处理 URL 查询字符串中的参数,因此名称为 getParameter。
例如,我们可以在doPost()方法中编写以下代码来读取表单字段的值:
1
2
|
String username = request.getParameter( "username" );
String password = request.getParameter( "password" );
|
要将响应发送回客户端,我们需要通过调用HttpServletResponse接口的getWriter()方法从响应对象中获取写入器:
1
|
PrintWriter writer = response.getWriter();
|
然后使用print()或println()方法传递响应(以 HTML 代码的形式)。例如:
1
2
3
4
5
|
String htmlRespone = "" ;
htmlRespone += " + username + "" ;
htmlRespone += "" ;
writer.println(htmlRespone);
|
这是处理登录表单的 servlet 类的完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package net.codejava.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet ( "/loginServlet" )
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// read form fields
String username = request.getParameter( "username" );
String password = request.getParameter( "password" );
System.out.println( "username: " + username);
System.out.println( "password: " + password);
// do some processing here...
// get response writer
PrintWriter writer = response.getWriter();
// build HTML code
String htmlRespone = "" ;
htmlRespone += " + username + " ;
htmlRespone += "Your password is: " + password + "" ;
htmlRespone += "" ;
// return response
writer.println(htmlRespone);
}
}
|
这是在浏览器中提交上述登录表单时的示例输出:
到目前为止,您已经了解了使用 Java servlet 处理 HTML 表单数据的来龙去脉。供您参考,我们提供了处理常见 HTML 表单字段的示例列表,如下所示。请注意,我们使用 servlet 中的System.out.println()语句来演示输出。
1
2
|
Username: < input type = "text" name = "username" />
Password: < input type = "password" name = "password" />
|
1
2
3
4
5
|
String username = request.getParameter( "username" );
String password = request.getParameter( "password" );
System.out.println( "username is: " + username);
System.out.println( "password is: " + password);
|
1
2
|
username is: admin
password is: nimda
|
1
2
3
|
Speaking language:
< input type = "checkbox" name = "language" value = "english" />English
< input type = "checkbox" name = "language" value = "french" />French
|
1
2
3
4
5
6
7
8
|
String languages[] = request.getParameterValues( "language" );
if (languages != null ) {
System.out.println( "Languages are: " );
for (String lang : languages) {
System.out.println( "\t" + lang);
}
}
|
1
2
3
|
Languages are:
english
french
|
1
2
3
|
Gender:
< input type = "radio" name = "gender" value = "male" />Male
< input type = "radio" name = "gender" value = "female" />Female
|
1
2
|
String gender = request.getParameter( "gender" );
System.out.println( "Gender is: " + gender);
|
1
|
Gender is: male
|
1
2
|
Feedback:< br />
< textarea rows = "5" cols = "30" name = "feedback" > textarea >
|
1
2
|
String feedback = request.getParameter( "feedback" );
System.out.println( "Feed back is: " + feedback);
|
1
|
Feed back is: This tutorial is very helpful. Thanks a lot!
|
1
2
3
4
5
6
7
|
Job Category:
< select name = "jobCat" >
< option value = "tech" >Technology option >
< option value = "admin" >Administration option >
< option value = "biology" >Biology option >
< option value = "science" >Science option >
select >
|
1
2
|
String jobCategory = request.getParameter( "jobCat" );
System.out.println( "Job category is: " + jobCategory);
|
1
|
Job category is: science
|
要创建一个表单来上传文件,我们需要指定enctype标签的属性如下:
1
2
3
4
5
6
|
< form method = "post" action = "uploadServlet" enctype = "multipart/form-data" >
Select file to upload: < input type = "file" name = "uploadFile" />
< input type = "submit" value = "Upload" />
form >
|
对于使用 Java servlet 在服务器端处理文件上传,我们推荐这些教程: