---
title: "jsp作业（1）--使用jsp+servlet完成一个表单验证"
author: "mywebcode"
date: "2012-11-07T21:46:00Z"
tags: []
slug: "jsp-1-jsp-servlet"
draft: false
lastmod: "2013-05-08T10:12:50Z"
id: 1862415
url: "https://mywebcode.iteye.com/blog/1862415"
---

要求：使用jsp+servlet完成一个表单验证，通过表单提交信息给Servlet，Servlet获取到信息后，处理，把信息放到request对象中，如果用户提交的姓名为空，不能跳转，否则，将信息全部显示出来。

## //index2.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%&gt;  
&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme() + "://"  
\+ request.getServerName() + ":" + request.getServerPort()  
\+ path + "/";  
%&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;base href="&lt;%=basePath%&gt;"&gt;

&lt;title&gt;My JSP 'index2.jsp' starting page&lt;/title&gt;

&lt;meta http-equiv="pragma" content="no-cache"&gt;  
&lt;meta http-equiv="cache-control" content="no-cache"&gt;  
&lt;meta http-equiv="expires" content="0"&gt;  
&lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;  
&lt;meta http-equiv="description" content="This is my page"&gt;  
&lt;!--  
&lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;  
\--&gt;

&lt;/head&gt;

&lt;body bgcolor="#7FFFD4"&gt;  
&lt;center&gt;  
&lt;h3&gt;  
表单提交  
&lt;/h3&gt;  
&lt;form action="servlet/TableSubmitServlet" method="post"&gt;  
&lt;table border="2px" width="260" height="192"&gt;  
&lt;tr&gt;  
&lt;td&gt;  
username  
&lt;/td&gt;  
&lt;td&gt;  
&lt;input type="text" name="username" /&gt;  
&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;  
sex  
&lt;/td&gt;  
&lt;td&gt;  
&lt;input type="radio" name="sex" value="boy" /&gt;  
boy  
&lt;input type="radio" name="sex" value="girl" /&gt;  
girl  
&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;  
address  
&lt;/td&gt;  
&lt;td&gt;  
&lt;input type="text" name="address" /&gt;  
&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;  
likes  
&lt;/td&gt;  
&lt;td&gt;  
&lt;input type="checkbox" name="likes" value="sing" /&gt;  
sing  
&lt;input type="checkbox" name="likes" value="dance" /&gt;  
dance  
&lt;input type="checkbox" name="likes" value="game" /&gt;  
game  
&lt;/td&gt;  
&lt;/tr&gt;  
&lt;tr&gt;  
&lt;td&gt;  
&lt;/td&gt;  
&lt;td&gt;  
&lt;center&gt;  
&lt;input type="submit" value="submit" /&gt;  
&lt;input type="reset" value="reset" /&gt;  
&lt;/td&gt;  
&lt;/tr&gt;

&lt;/table&gt;

&lt;/form&gt;  
&lt;/center&gt;  
&lt;/body&gt;  
&lt;/html&gt;

![](http://img.my.csdn.net/uploads/201211/07/1352295809_4406.png)

## //result2.jsp

&lt;%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%&gt;  
&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;base href="&lt;%=basePath%&gt;"&gt;

&lt;title&gt;My JSP 'result2.jsp' starting page&lt;/title&gt;

&lt;meta http-equiv="pragma" content="no-cache"&gt;  
&lt;meta http-equiv="cache-control" content="no-cache"&gt;  
&lt;meta http-equiv="expires" content="0"&gt;  
&lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;  
&lt;meta http-equiv="description" content="This is my page"&gt;  
&lt;!--  
&lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;  
\--&gt;

&lt;/head&gt;

&lt;body bgcolor="#ADD8E6"&gt;  
&lt;%="用户名："+request.getAttribute("username") %&gt;&lt;br/&gt;  
&lt;%="性别："+request.getAttribute("sex") %&gt;&lt;br/&gt;  
&lt;%="地址："+request.getAttribute("address") %&gt;&lt;br/&gt;  
&lt;%="爱好："+request.getAttribute("like") %&gt;  
&lt;/body&gt;  
&lt;/html&gt;

## //TableSubmitServlet.java

package com.hbsi.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 TableSubmitServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)  
throws ServletException, IOException {  
request.setCharacterEncoding("UTF-8");  
response.setContentType("text/html;charset=UTF-8");  
PrintWriter out = response.getWriter();

String username = request.getParameter("username");  
String sex = request.getParameter("sex");  
String address = request.getParameter("address");  
String\[] likes = request.getParameterValues("likes");  
String like=" ";  
for(int i=0;likes!=null && i&lt;likes.length;i++){  
like = like +"&nbsp;"+likes\[i];  
}  
request.setAttribute("username", username);  
request.setAttribute("sex", sex);  
request.setAttribute("address", address);  
request.setAttribute("like", like);  
if(username.equals("")){  
out.print("&lt;h1&gt;用户名为空，不能跳转，请输入用户名！！&lt;/h1&gt;");

}else{  
request.getRequestDispatcher("/result2.jsp").forward(request,response);  
}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)  
throws ServletException, IOException {

doGet(request, response);  
}

}