首页 > 代码库 > JSP表单验证实例
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=http://www.mamicode.com/"male">>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>
JSP表单验证实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。