JSP是表现层,显示出要注册的内容。用表单提交进行数据转发,表单提交给Servlet进行处理,进行数据库连接
将数据写入数据库,写入成功,返回成功注册信息。具体代码如下:
JSP代码负责显示,充当显示层。与用户进行交互,将用户提交数据转发给服务器处理的功能。
<%@ page language="java" contentType="text/html" pageEncoding="GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用户注册</title> <style type="text/css"> body{ margin: 0px;font-size: 12px; } .box{ border: 1px solid #D1DEB2; width: 150px; height: 20px; } .div1{ background-image: url(images/bg.jpg); height: 600px; width: 803px; padding-left:20px; padding-top:220px; text-align:left; } </style> <script type="text/javascript"> function reg(form){ if(form.username.value == ""){ alert("用户不能为空!"); return false; } if(form.password.value == ""){ alert("密码不能为空!"); return false; } if(form.repassword.value == ""){ alert("确认密码不能为空!"); return false; } if(form.password.value != form.repassword.value){ alert("两次密码输入不一致!"); return false; } if(form.question.value == ""){ alert("密码找回问题不能为空!"); return false; } if(form.answer.value == ""){ alert("密码找回答案不能为空!"); return false; } if(form.email.value == ""){ alert("电子邮箱不能为空!"); return false; } } </script> </head> <body> <div align="center"> <div class="div1"> <form action="RegServlet" method="post" onsubmit="return reg(this);"> <table align="center" border="0" width="500"> <tr> <td align="right" width="30%">用户名:</td> <td><input type="text" name="username" class="box"></td> </tr> <tr> <td align="right">密 码:</td> <td><input type="password" name="password" class="box"></td> </tr> <tr> <td align="right">确认密码:</td> <td><input type="password" name="repassword" class="box"></td> </tr> <tr> <td align="right">性 别:</td> <td> <input type="radio" name="sex" value="男" checked="checked">男 <input type="radio" name="sex" value="女">女 </td> </tr> <tr> <td align="right">密码找回问题:</td> <td><input type="text" name="question" class="box"></td> </tr> <tr> <td align="right">密码找回答案:</td> <td><input type="text" name="answer" class="box"></td> </tr> <tr> <td align="right">邮 箱:</td> <td><input type="text" name="email" class="box"></td> </tr> <tr> <td colspan="2" align="center" height="40"> <input type="submit" value="注 册"> <input type="reset" value="重 置"> </td> </tr> </table> </form> </div> </div> </body> </html>Servlet负责处理业务逻辑,将提交的数据进行处理同。例如,进行数据验证,身份验证等等。具体业务处理代码如下:
package com.lyq; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RegServlet extends HttpServlet { private static final long serialVersionUID = 1789481329876401944L; // 数据库连接Connection private Connection conn; // 初始化方法 public void init() throws ServletException { super.init(); try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接url String url = "jdbc:mysql://localhost:3306/registersystem"; // 获取数据库连接 conn = DriverManager.getConnection(url, "root", "123"); System.out.println("数据库连接成功!"); } catch (Exception e) { e.printStackTrace(); } } // doPost()方法处理注册请求 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置request与response的编码 response.setContentType("text/html"); request.setCharacterEncoding("GBK"); response.setCharacterEncoding("GBK"); // 获取表单中属性值 String username = request.getParameter("username"); String password = request.getParameter("password"); String sex = request.getParameter("sex"); String question = request.getParameter("question"); String answer = request.getParameter("answer"); String email = request.getParameter("email"); // 判断数据库中否连接成功 if (conn != null) { try { // 插入注册信息的SQL语句(使用?占位符) String sql = "insert into tb_user(username,sex,question,answer,email,password) " + "values(?,?,?,?,?,?)"; // 创建PreparedStatement对象 PreparedStatement ps = conn.prepareStatement(sql); // 对SQL语句中的参数动态赋值 ps.setString(1, username); ps.setString(2, password); ps.setString(3, sex); ps.setString(4, question); ps.setString(5, answer); ps.setString(6, email); // 执行更新操作 ps.executeUpdate(); // 获取PrintWriter对象 PrintWriter out = response.getWriter(); // 输出注册结果信息 out.print("<h1 aling='center'>"); out.print(username + "注册成功!"); out.print("</h1>"); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } else { // 发送数据库连接错误提示信息 response.sendError(500, "数据库连接错误!"); } } }