使用jsp+servlet完成一个表单验证,通过表单提交信息给Servlet,Servlet获取到信息后,处理,把信息放到request对象中,如果用户提交的姓名为空,不能跳转,否则,将信息全部显示出来。
1.form,jsp
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'form.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="servlet/CheckTest" method="post">
姓名:<input type="text" name="username" value=""/><br/>
性别:<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女"/>女<br/>
地址:<input type="text" name="address" value=""/><br/>
爱好:<input type="checkbox" name="likes" value="篮球"/>篮球
<input type="checkbox" name="likes" value="唱歌"/>唱歌
<input type="checkbox" name="likes" value="看书"/>看书<br/>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</body>
</html>
2.CheckTest.java(servlet)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CheckTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String name =request.getParameter("username");
String sex =request.getParameter("sex");
String address =request.getParameter("address");
String[] likes = request.getParameterValues("likes");
String likes2="";
if(likes!=null){
for(String like:likes){
likes2+=like+" ";
}
}
String url="/form.jsp";
if(name!=null && !name.equals("")){
request.setAttribute("username", name);
request.setAttribute("sex",sex);
request.setAttribute("address", address);
request.setAttribute("likes", likes2);
url="/result2.jsp";
}
request.getRequestDispatcher(url).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
3.result2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
姓名:<%=request.getAttribute("username") %><br/>
性别:<%=request.getAttribute("sex") %><br/>
住址:<%=request.getAttribute("address") %><br/>
爱好:<%=request.getAttribute("likes") %><br/>
</body>
</html>