原理 :(注册还没写,把正确的账号密码提前录入数据库)一个login.jsp供用户输入账号密码,action转发到check.jsp,request.getparameter获得输入内容,用jsp连接数据库的模板代码获取数据库中内容,if(a==b)做判断,request .getrequsetdiapature转到success.jsp或fail.jsp提示登录成功或失败
值得注意的是,用if(a==b)做信息判断时,必须把字符串用parseInt转成int型,否则会直接判断666和666不一样
String h3=request.getParameter("h3") ;
String h4=request.getParameter("h4") ;
int c = Integer.parseInt(h3);
int d= Integer.parseInt(h4);
if(c!=d)
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="check.jsp" method="post">
请输入用户名:<input type="text" name="username"><br/>
请输入密码:<input type="password" name="userpassword">
<input type="submit" value="登录">
</form>
</body>
</html>
check.jsp(目前只有这个db66数据库中mark表成功了,不知道为啥,另一个表或连接异常,可能跟int varchar有关)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.mysql.jdbc.Driver" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%String uname=request.getParameter("username") ;
String upassword=request.getParameter("userpassword");
out.print(uname);out.print(upassword);
%>
<!-- 连接数据库 -->
<%
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db66?&useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
if(conn != null){
out.println("-------------------");
out.println("-------------------");
out.println(" ");
out.print("<br />");
out.print("<br />");
Statement stmt = null;
ResultSet rs = null;
out.print("uname"+uname);
String jkjkj = "SELECT * FROM mark where id="+uname;
stmt = conn.createStatement();
rs = stmt.executeQuery(jkjkj);
out.print("<br />");
while (rs.next())
{
out.print(rs.getString("idd"));
out.print("是否等于");
out.print(upassword);
int a = Integer.parseInt(upassword);
int b= Integer.parseInt(rs.getString("idd"));
if(a==b)
{
request.getRequestDispatcher("success.jsp").forward(request,response);
}
else{
request.getRequestDispatcher("fail.jsp").forward(request,response);
}
}
out.print("<br />");
}
else{
out.print("连接失败!");
}
}catch (Exception e) {
out.print("数据库连接异常!");
}
%>
</body>
</html>