由于前两篇博客都是一点点的去写的,而注册部分其实和上一篇博客登录部分类似,所以在这了我就不把每一步写那么细了,如果哪一个不清楚,可以先去看上一篇的 网页登录设计(二):登录部分 再看这个注册部分
同样的,注册首先需要一个.jsp页面,所以.jsp的代码为:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="RegServlet" method="post">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th colspan="2">注册页面</th>
</tr>
<tr>
<td><label for="userName">用户名: </label></td>
<td><input type="text" name="userName" id="" /></td>
</tr>
<tr>
<td><label for="pwd">密码: </label></td>
<td><input type="text" name="pwd" id="" /></td>
</tr>
<tr>
<td><label for="turePwd">确认密码: </label></td>
<td><input type="text" name="turePwd" id="" /></td>
</tr>
<tr>
<td><label for="name">真实姓名: </label></td>
<td><input type="text" name="name" id="" /></td>
</tr>
<tr>
<td><label for="sex">性别: </label></td>
<td><input type="text" name="sex" id="" /></td>
</tr>
<tr>
<td><label for="age">年龄: </label></td>
<td><input type="text" name="age" id="" /></td>
</tr>
<tr>
<td colspan="2">
<button><a href="login.jsp">登录</a></button>
<input type="submit" value="注册" />
</td>
</tr>
</table>
</form>
</body>
</html>
.jsp写好以后,写对应的servlet类的代码。
package lession.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lession.util.UtilDBHelper;
public class RegServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 为了方便,直接让doGet调用doPost即可
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.编码问题
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 响应格式
response.setContentType("text/html");
// 2. 接收数据(这个是按照注册的接收数据写的)
String userName = request.getParameter("userName"); // 用户名
String pwd = request.getParameter("pwd"); // 密码
String name = request.getParameter("name"); // 真实姓名
String sex = request.getParameter("sex"); // 性别
String ageStr = request.getParameter("age"); // 年龄
Integer age = Integer.parseInt(ageStr); // 类型转换
// jdbc插入数据
// 数据库连接
Connection conn = UtilDBHelper.getConn();
// SQL语句
String sql = "INSERT INTO message VALUES(?,?,?,?,?)";
// 交互
PreparedStatement ps = null;
boolean isNot = false;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
ps.setString(3, name);
ps.setString(4, sex);
ps.setInt(5, age);
// 执行
int count = ps.executeUpdate();
// 结果
if (count > 0) {
isNot = true;
} else {
isNot = false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 关闭资源
UtilDBHelper.close(conn, ps, null);
PrintWriter out = response.getWriter();
if (isNot) {
out.print("注册成功");
response.sendRedirect("login.jsp"); // 跳到登录页面去登录
} else {
out.print("注册失败");
response.sendRedirect("index.jsp"); // 继续重新注册
}
out.flush();
out.close();
}
}
在这里呢,如果不是通过创建servlet建的类,而是通过Class创建的话,我们还需要在web.xml中添加配置,至于原因,不清楚的可以去看我的前两篇博客.
所以,在web.xml中应该多两块儿代码,分别是:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>RegServlet</servlet-name>
<servlet-class>lession.servlet.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/RegServlet</url-pattern>
</servlet-mapping>
到这里呢,这个注册部分就写好了.然后我们大概运行看一下效果
首先这是简写的一个注册页面,样式什么的你可以根据自己的兴趣自己设置
这里那个密码和确认密码不要在意,即使这个输入的不一样,照样阔以注册成功,至于原因就是我们还没有学习js.但是可以先把那个注册页面有的东西添加一下.
这就是注册成功了.
然后去数据库查询一下,可以得到注册的数据
这样,这个登录设计的注册部分就设计好了.然后下面就要写登录成功进入的列表页面了.