JSP簡單練習-用Servlet獲取表單數據
// javaBean代碼
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AcceptUserRegist extends HttpServlet {
public String codeToString(String str) // 處理中文字符串的函數
{
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}catch(Exception e)
{
return s;
}
}
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
// 設置mime
response.setContentType("text/html;charset=GB2312");
PrintWriter out=response.getWriter();
out.println("接收新用戶注冊 ");
out.println("這是新用戶注冊所提交的數據:
");
out.println("用戶名是:"+codeToString(request.getParameter("username"))+"
");
out.println("密碼是:"+codeToString(request.getParameter("userpassword"))+"
");
out.println("性別是:"+codeToString(request.getParameter("sex"))+"
");
out.println("出生年月是:"+request.getParameter("year")+request.getParameter("month")+request.getParameter("day")+"
");
out.println("電子郵箱是:"+request.getParameter("E-mail")+"
");
out.println("家庭住址是:"+codeToString(request.getParameter("address"))+"
");
out.print(" ");
}
}
<%@ page contentType="text/html;charset=gb2312" %>
<script language="javascript">
function on_submit() // 驗證數據的合法性
{
if(form1.username.value=="")
{
alert("用戶名不能為空,請輸入用戶名!");
form1.username.focus();
return false;
}
if(form1.userpassword.value=="")
{
alert("用戶密碼不能為空,請輸入密碼!");
form1.userpassword.focus();
return false;
}
if(form1.reuserpassword.value=="")
{
alert("用戶確認密碼不能為空,請輸入密碼!");
form1.reuserpassword.focus();
return false;
}
if(form1.userpassword.value!=form1.reuserpassword.value)
{
alert("密碼與確認密碼不同");
form1.userpassword.focus();
return false;
}
if(form1.email.value.length!=0)
{
for(i=0;i