目录
一、简单理解MVC框架
二、项目结构
三、项目源码
3.1 User
3.2 UserDao
3.3 RegisterDao
3.4 servletControll
3.5 servletControllRegister
3.6 web.xml
3.7 login.jsp
3.8 register.jsp
3.9 success.jsp
3.10 failure.jsp
四、实现效果
总结
前言
本篇文章主要介绍利用MVC框架去实现一个简单的用户登录注册功能,内容主要以源码为主来介绍,目的是让大家成功搭建出此项目。项目使用SQL Server 数据库,需要导入sqljdbc4.jar包,jar包会在下面给大家提供。
一、简单理解MVC框架
经典MVC模式中,M是指业务模型,V是指用户界面,C则是控制器,使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。
其中,View的定义比较清晰,就是用户界面。Model即对数据进行逻辑处理的部分。Controller按照用户请求来选择对应的Model进行处理同时把处理结果返回到指定的jsp视图中,只是接收请求并决定调用哪个模型构件去处理请求,然后再确定用哪个视图来显示返回的数据。
二、项目结构
三、项目源码
3.1 User
代码如下(示例):
public class User {
// 封装实体类的JavaBean
private String name;
private String password;
public User() {
}
// 构造方法用来传入数值
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.2 UserDao
代码如下(示例):
import java.sql.*;
//处理业务逻辑的JavaBean
public class UserDao {
public static int login(User user) {
int flag = -1;
String NAME = "sa";
String PASSWORD = "123456";
String URL = "jdbc:sqlserver://localhost:1433;databaseName=test";
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 1、加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 2、建立连接
connection = DriverManager.getConnection(URL, NAME, PASSWORD);
// 3、操作数据库
String sql = "select count(*) from admins where name=? and password=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
// 4、执行
rs = pstmt.executeQuery();
if (rs.next()) {
flag = rs.getInt(1);
}
if (flag > 0) {// 登录成功
return 1;
} else {
return 0;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
return -1;
} catch (SQLException e) {
e.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3.3 RegisterDao
代码如下(示例):
import java.sql.*;
//封装注册功能业务逻辑的JavaBean
public class RegisterDao {
public static int register(User user) {
int count = -1;
String NAME = "sa";
String PASSWORD = "123456";
String URL = "jdbc:sqlserver://localhost:1433;databaseName=test";
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//1、加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2、连接
connection = DriverManager.getConnection(URL, NAME, PASSWORD);
//操作数据库
String sql = "insert into admins values(?,?)";//插入数据
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
count = pstmt.executeUpdate();
return count;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return -1;
} catch (SQLException e) {
e.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
if (pstmt != null) pstmt.close();
if (connection != null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3.4 servletControll
代码如下(示例):
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Servlet implementation class servletControll
*/
@WebServlet("/servletControll")
public class servletControll extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//取值
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
//封装
User user = new User(name, pwd);
//调用业务逻辑层
int result = UserDao.login(user);
if (result > 0) {
//登录成功
response.sendRedirect("success.jsp");//重定向到成功界面
} else {
//登录失败
response.sendRedirect("notsuccess.jsp");//定位到失败页面
}
//请求转发
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3.5 servletControllRegister
代码如下(示例):
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Servlet implementation class servletControllRegister
*/
@WebServlet("/servletControllRegister")
public class servletControllRegister extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//注册功能、、向数据库添加数据
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
User user = new User(name, pwd);//保存客户端输入的信息
int result = RegisterDao.register(user);
if (result > 0) {
//注册成功
response.sendRedirect("login.jsp");
} else {
//注册失败重新注册
response.sendRedirect("register.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3.6 web.xml
代码如下(示例):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 初始的页面 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 用户名和密码验证的servlet -->
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>servletControll</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
<!-- 显示欢迎的servlet -->
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>servletControllRegister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
3.7 login.jsp
代码如下(示例):
<%--
Created by IntelliJ IDEA.
User: 10504
Date: 2022/5/17
Time: 12:49
To change this template use File | Settings | File Templates.
--%>
登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="servletControll" method="post">
用户名:<input type="text" name="uname"/>
密码:<input type="password" name="upwd"/><br><br>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
<a href="register.jsp">注册新用户</a>
</form>
</body>
</html>
3.8 register.jsp
代码如下(示例):
<%--
Created by IntelliJ IDEA.
User: 10504
Date: 2022/5/17
Time: 12:55
To change this template use File | Settings | File Templates.
--%>
注册页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="servletControllRegister" method="post">
用户名:<input type="text" name="uname"/>
密码:<input type="password" name="upwd"/>
<input type="submit" value="注册"/>
</form>
</body>
</html>
3.9 success.jsp
代码如下(示例):
<%--
Created by IntelliJ IDEA.
User: 10504
Date: 2022/5/17
Time: 13:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
欢迎您的使用!
</body>
</html>
3.10 failure.jsp
代码如下(示例):
<%--
Created by IntelliJ IDEA.
User: 10504
Date: 2022/5/17
Time: 13:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录失败</title>
</head>
<body>
</body>
</html>
四、实现效果
总结
以上简单介绍了利用MVC框架实现用户登录注册功能的基本流程,具体的jsp界面只完成了功能,下去可以利用css技术渲染自己的界面。
有关数据库的问题,大家可以使用Microsoft SQL Server Management Studio 18来建立一个名为test的数据库,在里面建一个admins表,列名为name和password即可。另外还要注意更改自己的数据库登录账号和密码。
有学到的小伙伴希望多多关注、点赞、支持!文章来源:https://www.toymoban.com/news/detail-458546.html
以此处附上下载地址:sqljdbc4.jarhttps://pan.baidu.com/s/1Wc7QsZwJsMYe0-WEOXjZlw?pwd=7px5文章来源地址https://www.toymoban.com/news/detail-458546.html
到了这里,关于MVC框架实现用户登录注册功能(连接数据库)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!