程序员人生 网站导航

JSP表单验证实例

栏目:互联网时间:2014-12-24 08:27:07

1、表单填写页面

<%@ page language="java" contentType="text/html; charset=ISO⑻859⑴" pageEncoding="ISO⑻859⑴"%> <!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⑻859⑴"> <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⑻859⑴" pageEncoding="ISO⑻859⑴"%> <!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⑻859⑴"> <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>



------分隔线----------------------------
------分隔线----------------------------

最新技术推荐