程序代码:
<%@ page language="java" import="java.util.*,java.sql.*,javax.sql.DataSource" pageEncoding="GB2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% request.setCharacterEncoding("GB2312"); if(request.getParameter("username")!=null) { Connection conn = null; Statement smt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e) { out.print(e.getMessage()); } try{ System.out.println("iiii"); conn = DriverManager.getConnection("jdbc:MySql://localhost:3306/web?user=root&password=123&useUnicode=true&characterEncoding=gb2312"); smt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql = "insert into user values('"+(String)request.getParameter("username")+"','"+ (String)request.getParameter("password") +"','"+(String)request.getParameter("email")+"','')"; smt.executeUpdate(sql); }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally{ try{ if(rs!=null) { rs.close(); } if(smt!=null) { smt.close(); } if(conn!=null) { conn.close(); } }catch(Exception e) { e.printStackTrace(); } } } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'regist.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <form method="post" name="f1" action="regist.jsp"> <h1 align="center">注册</h1><div align="center"><br> </div><div align="center"><div align="center">用户名: <input type="text" name="username" size="20" maxlength="20"><br><br>密码: <input type="password" name="password" size="20" maxlength="20"><br><br>邮箱: <input type="text" name="email" size="20" maxlength="20"><br></div> <input type="submit" name="Regist" value="注册 " onClick="validateLogin()";> </div> </form> <script language="javaScript"> function validateLogin() { var sUserName = document.f1.username.value; var sPassword = document.f1.password.value; var sEmail = document.f1.email.value; if( sUserName=="" ) { alert("请输入用户名!"); return false; } if( sPassword=="" ) { alert("请输入密码!"); return false; } if( sEmail=="" ) { alert("请输入邮箱!"); return false; } if(sUserName.length<6) { alert("用户名请不小于6个字符! "); return false; } if(sPassword.length<6) { alert("密码请不小于6个字符! "); return false; } var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; if(!reg.test(sEmail)) { alert("邮箱格式错误 ! "); return false; } } </script> </body> </html>代码