这篇文章具体实现了接收从网页输入的数据与数据库中数据进行匹配并返回结果的这个一个简易登录功能
重在逻辑学习
第一步:设置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>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>cn.kgc.kb09.controller.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.html</url-pattern>
</servlet-mapping>
</web-app>
第二步:简易登录界面html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>登录功能测试</h1>
<form action="login.html" method="get">
<p><input type="text" name="username"></p>
<p><input type="password" name="password"></p>
<p><input type="submit" value="点我登录"></p>
</form>
</body>
</html>
第三步LoginServlet:接收网页数据
//用于接收网页数据并处理(该方法需要继承HttpServlet,该类需要配置Tomcat)
public class LoginServlet extends HttpServlet {
//该对象的功能是连接数据库并处理接收数据对比并返回结果,在后面会实现
private LoginService service;
//重载HttpServlet中的doGet或doPost或service方法
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//先获取到前台传回的数据
//接收页面username框中输入的数据
String username = req.getParameter("username");
//接收页面password框中输入的数据
String password = req.getParameter("password");
//实例化声明service对象(LoginService为接口/LoginServiceImpl为具体实现类)
service =new LoginServiceImpl();
//调用login方法,该功能是对比数据是否匹配存在的用户信息并返回结果
boolean canlogin = service.login(username, password);
if(canlogin){//可以登录
req.setAttribute("rst","恭喜"+username+",登录成功!");
}else {//不可以登录
req.setAttribute("rst","对不起登陆失败");
}
//转发请求到新的页面
req.getRequestDispatcher("result.jsp").forward(req,resp);
}
}
第四步LoginServiceImpl:处理接收数据是否合法在进行匹配数据
//该类实现用户输入的数据是否合法在进行匹配最后返回的结果
public class LoginServiceImpl implements LoginService{
//声明LoginDao对象,该功能为具体实现处理接收的数据与数据库中数据的匹配
LiginDao dao;
@Override
public boolean login(String username, String password) {
//实例化对象
dao=new LoginDaoImpl();
//合法性判断
//如果用户名或密码是否合法
//username.trim().equals("")为判断是否为空字符或存在空格
if (username==null || password==null || username.trim().equals("")
|| password.trim().equals("")){
return false;
}
//调用dao层进行数据匹配
User user=dao.queryUserByNameAndPwd(username,password);
//如果user不为空在代表匹配成功,则可以登录,否则不可以登录
if (user!=null){
return true;
}
return false;
}
}
第五步LoginDaoImpl:匹配数据返回结果
//连接数据库并返回查询结果
public class LoginDaoImpl implements LiginDao {
@Override
public User queryUserByNameAndPwd(String username, String password) {
//设置查询语句
String sql="select * from user_info where uname=? and password=?;";
//执行PstDao中的静态方法query方法来查询是否存在匹配数据
ResultSet rs = PstDao.query(sql, username, password);
//声明User类对象
User user=null;
try {
//如果有匹配数据则将数据传入user对象中
if (rs.next()){
user=new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
}
//并返回user
return user;
}
}
第六步PstDao:实现执行sql语句功能
public class PstDao {
private static String driver= Prop.getP("driver");//获取配置文件中driver对应数据
private static String url=Prop.getP("url");//获取配置文件中url连接地址数据
private static String user=Prop.getP("user");//获取配置文件中user用户名数据
private static String pwd=Prop.getP("pwd");//获取配置文件中pwd密码数据
//连接
public static Connection getConn(){
try {
//反射实现类driver
Class.forName(driver);
//完成连接功能并返回Connection类型对象
return DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//释放资源
public static void close(Connection conn, PreparedStatement pst, ResultSet res){
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (res!=null){
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询方法
public static ResultSet query(String sql,Object ...params){
//连接数据库
Connection conn = getConn();
//创建执行sql语句对象
PreparedStatement pst=null;
//创建ResultSet对象用于接收查询到的数据
ResultSet rst=null;
try {
//载入sql语句
pst = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//匹配对应条件语句
pst.setObject(i+1,params[i]);
}
//执行语句并将查询结果传入ResultSet
rst = pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rst;
}
public static int update(String sql,Object... params){
Connection conn = getConn();
PreparedStatement pst=null;
try {
pst =conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pst.setObject(i+1,params[i]);
}
return pst.executeUpdate();//执行修改语句并返回被影响的数量
} catch (SQLException e) {
e.printStackTrace();
}
return -1;//修改失败返回-1
}}
第七步Prop:实现获取配置文件信息功能
public class Prop {
//实例化Properties类型对象
private static Properties p=new Properties();
public static String getP(String param){
try {
//读取指定位置的配置文件信息
p.load(new FileInputStream("D:\\idea开发文件\\20200821-web\\resourse\\db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
//返回需要的属性信息
return p.getProperty(param);
}
}
第八步:设置配置文件信息
driver=com.mysql.jdbc.Driver
//数据库的地址以及你想访问的库名
url=jdbc:mysql://192.168.12.123:3306/userControl
//数据库用户名
user=root
//数据库密码
pwd=ok
第九步:跳转新的页面html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录结果页面</title>
</head>
<body>
<%
//执行java代码,显示rst对应的字符串信息
Object rst = request.getAttribute("rst");
%>
<h1><%=rst%></h1>
</body>
</html>
到这里简易登录功能就可以实现了,快去试试把!