JSP简单练习-用Servlet获取表单数据
程序员文章站
2023-11-10 14:24:58
// javaBean代码
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.se...
// 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(" "); } }
jsp --> <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 新用户注册 新用户注册另外,在web.xml文件的
用户名(*):
密 码(*):
再输一次密码(*):
性别:男女
出生年月:年 1 2 3 4 5 6 7 8 9 10 11 12 月 日
电子邮箱(*):
家庭住址:
acceptUserRegist servlet.AcceptUserRegist acceptUserRegist /acceptUserRegist输入网址:localhost:8080/jspdemo/servletform.jsp
填写信息~ 如下:
提交后,显示页面如下: