不会搭建环境点击进入==============》
不会创建工程点击进入==============》
login.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">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>请登录</title>
</head>
<body bgcolor=LightBlue>
<div align="center">
<br>
<H1>欢迎光临登录平台</H1>
<body>
<form action="login" method="post">
<table width="90%">
<tr>
<td width="50%" height="30" align="right">用户名:</td>
<td width="50%" height="30" align="left">
<input type="text" name="username">
</td>
</tr>
<tr>
<td width="50%" height="30" align="right">密 码:</td>
<td width="50%" height="30" align="left">
<input type="password" name="password">
</td>
</tr>
<td width="100%" height="40" align="center" colspan="2">
<input type="submit" name="sub" value="登 录">
</td>
</tr>
</table>
</form>
</body>
</html>
登录成功与失败跳转页面loginSuccess.jsp和loginFailed.jsp页面很简单,这里就不多写了。
连接数据库(DBCONN.java)页面:
package login.com;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBCONN {
// 设置成私有的
private static final String driver = "com.mysql.jdbc.Driver";// 数据库驱动
private static final String url = "jdbc:mysql://localhost:3306/test"; //"login"是指你的数据库名称 3306是端口
private static final String username = "root"; // 数据库用户名
private static final String password = "123456"; // 数据库密码
private static Connection conn = null; // 连接对象
// 静态代码块加载数据库驱动
static {
try {
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
// 单例模式返回数据库连接对象
public static Connection getConnection() throws Exception {
if (conn == null) {
conn = DriverManager.getConnection(url, username, password);
return conn;
}
return conn;
}
public static void main(String[] args) {
try {
Connection conn = DBCONN.getConnection();
if (conn != null) {
System.out.println("数据库连接正常");
} else {
System.out.println("数据库连接异常");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Servlet控制页面(loginServlet.java):
package login.com;
/**
* 这个类主要的功能是获取前端页面用户输入的用户名和密码 然后调用LoginSuccess方法进行用户名和密码与数据库中数据进行匹配
* 并进行跳转,用户名和密码正确跳转login_success.jsp,错误跳转login_failed.jsp
*/
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// 登录方法
public class loginServlet implements javax.servlet.Servlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");// 取得用户名
String password = request.getParameter("password");// 取得密码
System.out.println("页面取得用户名和密码"+username+"|"+password);
LoginSuccess db = new LoginSuccess(); // 构建登陆对象
boolean canLogin = db.LoginSuccess(username, password);// 取得用户名和密码
if (canLogin) {// 根据登陆情况,跳转页面
System.out.println("用户名和密码正确");
response.sendRedirect("loginSuccess.jsp");
} else {
response.sendRedirect("loginFailed.jsp");
System.out.println("用户名和密码错误");
}
}
public void destroy() {
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
HttpServletRequest rq = (HttpServletRequest) request;
HttpServletResponse rs = (HttpServletResponse) response;
try {
doPost(rq, rs);
} catch (Exception e) {
e.printStackTrace();
}
}
}
登录页面check(LoginSuccess.java):
package login.com;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// 调用数据库,进行查询用户名和密码
public class LoginSuccess{
public boolean LoginSuccess(String username, String password) throws Exception {
boolean returnValue = false;
String sql = "SELECT * FROM user";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBCONN.getConnection();
if (conn != null) {
System.out.println("数据库连接正常");
} else {
System.out.println("数据库连接异常");
}
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
rs.beforeFirst(); //移至第一条记录之前
while (rs.next()) {
String userName = rs.getString("username");
String passWord = rs.getString("password");
System.out.println("数据库取得用户名和密码"+username+"|"+password);
if (userName.equals(username) && passWord.equals(password)) {
// 如果用户名和密码都和数据库的一样,就返回true
returnValue = true;
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
}
Tomcat服务器配置文件修改,用于jsp与java文件建立连接:
>
-name>loginServlet -name>
-class>login.com.loginServlet -class>
>
-mapping>
-name>loginServlet -name>
-pattern>/login -pattern>
-mapping>
-app>
创建数据库test,并创建表user:
CREATE TABLE `user` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
欢迎大家阅读,如有问题或者获取源码,请留言,我看到会通过邮件发送源码。但不保证及时性。
更多JavaWeb学习文件点击