用jsp实现一个简单的登陆注册功能
登陆功能实现
1. 建立工具类util,用于访问数据库
- 代码如下:
public class DBUtil{
private static String url="jdbc:mysql://localhost:3306/huake_db";
private static String user="root";
private static String password="12345678";
//注册驱动
//静态代码块只调用一次,驱动只需要注册一次,后面直接连接即可
static{
try {
Class.forName("com,.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection(){
Connection connection =null;
try {
connection=DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return connection;
}
//释放资源
public static void close(ResultSet rSet,PreparedStatement pStatement,Connection connection){
try {
if(rSet!=null)
rSet.close();
if(pStatement!=null)
pStatement.close();
if(connection!=null)
connection.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
- 直接用类名调用静态方法就可以连接数据库,和关闭数据库
2. 建立动态web 3.0项目,登陆注册功能:
- 建立index.jsp文件
<c:if></c:if>
使用时要在jsp引用下面这句话
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!--
action=login 表单数据提交的位置
method=post 表单数据提交的方式
-->
<form action="login" method="post"><br/>
<input type="text" name="username"/><br/>
<input type="password" name="pass"/><br/>
<input type="submit" value="登陆"/>
<c:if test="${param.type==1}">
<font color="red">用户名或者密码错误</font>
</c:if>
</form>
<form action="signin.jsp" method="post">
<input type="submit" value="注册"><br/>
<c:if test="${param.type==0}">
<font color="green">注册成功</font>
</c:if>
</form>
- LoginServlet.java主要代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取表单数据
String name=request.getParameter("username");
String pwd=request.getParameter("pass");
User user=dao.login(name, pwd);
if (user!=null) {
System.out.println("登陆成功");
/**
* 页面跳转
* 1请求转发
* 由服务器内部跳转,用户感知不到页面的跳转,地址栏不会发生变化
* 会把请求到的数据携带到下一个页面,用户只发送一次请求
* 2.重定向
* 由用户跳转,用户可以感知到页面的跳转,地址栏会发生变化,
* 不会把请求到的数据携带到下一个页面,用户发送了两次请求
*/
RequestDispatcher dispatcher=request.getRequestDispatcher("main.jsp");
request.setAttribute("user", user);
dispatcher.forward(request, response);
// response.sendRedirect("main.jsp"); //重定向
return;
}
else{
System.out.println("用户名或者密码错误");
response.sendRedirect("index.jsp?type=1");
return ;
}
}
注册功能实现
3. 往数据库插入数据
@Override
public boolean signin(String username, String password) {
try {
connection=DBUtil.getConnection();
String sql="insert into t_login(username,password) values(?,?)";
pStatement=connection.prepareStatement(sql);
pStatement.setString(1, username);
pStatement.setString(2, password);
pStatement.executeUpdate();
DBUtil.close(rSet, pStatement, connection);
return true;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return false;
}
5.注册界面signin.jsp
<form action="signin" method="post">
<input type="text" name="name"/><br/>
<input type="password" name="pwd"/><br/>
<input type="password" name="repwd"><br/>
<input type="submit" value="注册"/><br/>
<c:if test="${param.type==1}">
<font color="red">前后密码不一致,请重新输入</font>
</c:if>
<c:if test="${param.type==0}">
<font color="green">注册成功</font>
</c:if>
<c:if test="${param.type==2}">
<font color="red">发生未知错误,注册失败</font>
</c:if>
6.注册处理业务,sigin.java代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.sendRedirect("signin.jsp"); //重定向
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
String repwd=request.getParameter("repwd");
if(pwd.equals(repwd)){
System.out.println("前后密码一致");
if(dao.signin(name, pwd)){
//response.sendRedirect("signin.jsp?type=0");
response.sendRedirect("index.jsp?type=0");
return;
}
else{
response.sendRedirect("signin.jsp?type=2");
return;
}
}
else{
System.out.println("前后密码不一致");
response.sendRedirect("signin.jsp?type=1");
return ;
}
}