JSP表单验证实例

1、表单填写页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form Table</title>
</head>
<body>

<form action="TestServlet" method="post">
<table border="2" cellspaceing="1" cellpadding="3" bgcolor="#F00000">
	<th colspan="2" align="center">
		Sumbmit Table
	</th>
	<tr>
		<td>username</td>
		<td><input type="text" name="username"></td>
	</tr>
	<tr>
		<td>sex</td>
		<td>
			male<input type="radio" name="sex" value="male">
			    
			female<input type="radio" name="sex" value="female">
			</td>
	</tr>
	<tr>
		<td>address</td><td><input type="text" name="address"></td>
	</tr>
	<tr>
		<td>likes</td>
		<td>
			sing<input type="checkbox" name="likes" value="sing">
			dance<input type="checkbox" name="likes" value="dance">
			game<input type="checkbox" name="likes" value="game">	
		</td>
	</tr>
	<tr>
		<td>
		 
		</td>
		<td>
			<input type="submit" value="Submit">
			<input type="reset" value="Reset">
		</td>
	</tr>
</table>
</form>

</body>
</html>

2、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;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet(name="testservlet",urlPatterns={"/TestServlet"})
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("username");
		String sex=request.getParameter("sex");
		String add=request.getParameter("address");
		String[] likes0=request.getParameterValues("likes");
		String likes="";
		for(int i=0;i<likes0.length;i++){
			likes+=likes0[i]+" ";
		}
		request.setAttribute("name", name);
		request.setAttribute("sex", sex);
		request.setAttribute("address", add);
		request.setAttribute("likes", likes);
		request.getRequestDispatcher("result.jsp").forward(request,response);
		
		PrintWriter out=response.getWriter();
		out.println(name);
		out.println(sex);
		out.println(add);
		out.println(likes);
	}

}

3、验证结果输出

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>result</title>
</head>
<body>
<table border="2" cellspaceing="1" cellpadding="3" bgcolor="#00FF00">
	<th colspan="2" align="center">
		Result Table
	</th>
	<tr>
		<td>username</td>
		<td><%=request.getAttribute("name") %></td>
	</tr>
	<tr>
		<td>sex</td>
		<td>
			<%=request.getAttribute("sex") %>
		</td>
	</tr>
	<tr>
		<td>address</td>
		<td><%=request.getAttribute("address") %></td>
	</tr>
	<tr>
		<td>likes</td>
		<td>
			<%=request.getAttribute("likes") %>
		</td>
	</tr>
</table>
</body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值