当您使用搜索引擎搜索内容、从网上购买商品或者购票时,都需要输入一些个人信息或购买信息,这些信息就是表单数据,表单数据会随客户端的请求一起提交到服务器端。
使用Servlet处理客户端请求时,Servlet需要将这些表单数据提取出来并进行处理。HttpServletRequest的getParameter方法可以通过表单的参数名称获取该参数的值。
案例1:编写一个Servlet程序,读取名称为name、age、work的表单数据,并将它们的值输出到客户端。
程序清单 ThreeParams.java
@WebServlet(name="ThreeParamsServlet",urlPatterns= {"/params"})
public class ThreeParams extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String paramName = request.getParameter("name");
String paramAge = request.getParameter("age");
String paramWork = request.getParameter("work");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintStream out = new PrintStream(response.getOutputStream());
//输出html标签
out.println("<html>");
out.println("<head>");
out.println("<title>first Servlet</title>");
out.println("</head>");
out.println("<body>");
String body = "<ul>";
body += "<li><B>param1:<B>";
body += paramName;
body += "<li><B>param2:<B>";
body += paramAge;
body += "<li><B>param3:<B>";
body += paramWork;
body += "</ul>";
out.println(body);
out.println("</body>");
out.println("</html>");
}
}客户端向Servlet发送请求时,需要传入name,age,work三个表单数据,Servlet通过request对象的getParameter方法获取客户端传递的表单数据。
程序清单 ThreeParamsForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/bcxly_war_exploded/params" method="GET"> 第一个表单参数: <input type="text" name="name" /> 第二个表单参数: <input type="text" name="age" /> 第三个表单参数: <input type="text" name="work" /> <input type="submit" value="提交" /> </form> </body> </html>
案例1根据预先指定的参数名从表单数据中读取参数值,还假定每个参数都有值。但在实际应用中,服务端对客户端提交的表单数据并不清楚有多少个参数,也不能确定每个参数都会有值,在这种情况下,就需要遍历所有参数,并对参数是否有值进行判断。
案例2:编写一个Servlet程序,读取客户端提交的所有表单数据,并判断每个表单数据是否有值,若有值将它们的值输出到客户端。
程序清单 ProcessParams.java
@WebServlet(name="ProcessParams",urlPatterns= {"/processparams"})
public class ProcessParams extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintStream out = new PrintStream(response.getOutputStream());
//输出html标签
out.println("<html>");
out.println("<head>");
out.println("<title>读取所有表单参数</title>");
out.println("</head>");
out.println("<body>");
String body = "<ul>";
// 获取全部参数名称
Enumeration parames = request.getParameterNames();
// 遍历所有参数
while (parames.hasMoreElements())
{
String paramName =(String)parames.nextElement();
String value = request.getParameter(paramName);
if( null != value && !value.isEmpty() )
{
body += "<li><B>" + paramName + ":<B>";
body += value;
}
}
out.println(body);
out.println("</body>");
out.println("</html>");
}
}程序清单 ProcessParams.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/bcxly_war_exploded/processparams" method="GET"> 姓名: <input type="text" name="name" /> 年龄: <input type="text" name="age" /> 职业: <input type="text" name="work" /> <input type="submit" value="提交" /> </form> </body> </html>