数据库的名称是“mydb”。然后在该数据库中创建表,表名是”tb_user”。表的结构如下图所示:
启动eclipse并创建一个动态web工程,工程名称是“mynews”。在Target runtime中配置Tomcat的安装目录。如下图所示:
1.拷贝登录页到“WebContent”目录中,并增加如下红色的代码。
代码:
<div class="signin">
<div class="signin-head"><img src="images/test/head_120.png" alt="" class="img-circle"></div>
<form class="form-signin" role="form" method="post" action="${pageContext.request.contextPath}/UserLogin">
<input type="text" name="name" class="form-control" placeholder="user" required autofocus />
<input type="password" name="password" class="form-control" placeholder="password" required />
<button class="btn btn-lg btn-danger btn-block" type="submit">登录</button>
<label class="checkbox">
<input type="checkbox" value="remember-me"> 记住我
</label>
</form>
</div>
2.在src中创建包“swu.xxj.control”,并在该包中创建一个名为"UserLogin"的servlet。 3、在src中创建包”swu.xxj.dbutil”,并在该包中创建一个名为“DbConnect”的类。该类主要用来连接数据库。完整代码如下:
package swu.xxj.dbutil;
import java.sql.*;
public class DbConnect {
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/mydb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
conn = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库连接
* @param conn Connection对象
*/
public static void closeConnection(Connection conn){
// 判断conn是否为空
if(conn != null){
try {
conn.close(); // 关闭数据库连接
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
4、在src中创建包“swu.xxj.service”,并在该包中创建一个名为“UserService”的类。其代码如下:
5、修改“swu.xxj.control”包中的"UserLogin"的代码,在doPost方法里面添加如下代码:
rotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
boolean flag=UserService.checkUserExist(name, password);
if(flag){
HttpSession session=request.getSession();
session.setAttribute("name", name);
session.setAttribute("password", password);
response.sendRedirect("index.jps");
}
else{
response.sendRedirect("mylogin/login.jsp");
}
}
6、在“WebContent”目录下创建index.jsp。输出用户登录的信息,代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
欢迎${name }登录
</body>
</html>
·更改已有代码中数据库的密码为123456780
· 注意将文件资源夹中的驱动程序拷贝至webContent目录WIB-INF目录下的lib文件夹中
·在WEB-INF文件夹下新建index.jsp,并将webContent目录mylogin目录下的index.html文件重命名为login.jsp,否则网页运行失败。
【ps】项目资源包可以在我下载资源里找,大家一起学习哟~
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有