关键代码
1.index_ login.jsp
<body>
<form action="../Login_ser" method="post" >
账号:<input type="text" name="zh" ><br>
密码:<input type="password" name="mm" ><br>
<input type="submit" value="登录">
<input type="reset" value="重填">
</form>
</body>
2.login. succeed.jsp
<body>
恭喜您<%=session.getAttribute("zh") %>,登录成功。
</body>
3.login_ failed.jisp
<body>
很遗憾<%=session.getAttribute("zh") %>,您登录失败。
</body>
4Login_ tf.java
package Loginjdbc;
public class Login_tf {
private int user ;
private int password;
private int user1;
private int password1;
public Login_tf(int user, int password, int user1, int password1) {
super();
this.user = user;
this.password = password;
this.user1 = user1;
this.password1 = password1;
}
public int getUser1() {
return user1;
}
public void setUser1(int user1) {
this.user1 = user1;
}
public int getPassword1() {
return password1;
}
public void setPassword1(int password1) {
this.password1 = password1;
}
public int getUser() {
return user;
}
public void setUser(int user) {
this.user = user;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public int a()
{
if(user==user1&&password==password1)
{
return 1;
}else
{
return 0;
}
}
}
5DBBean.java
package bean;
import java.sql.*;
public class DBBean {
private String driverStr="com.mysql.cj.jdbc.Driver";
private String connStr="jdbc:mysql://localhost:3306/login?characterEncoding=gb2312&serverTimezone=Hongkong";
private String dbusername="root";
private String dbpassword="123456";
private Connection conn=null ;
private Statement stmt=null;
public DBBean()
{
try {
Class.forName(driverStr);
conn = DriverManager.getConnection( connStr,dbusername,dbpassword);
stmt=conn.createStatement();
} catch(Exception ex) {System.out.println("无法同数据库建立链接!");}
}
public int excuteUpdate(String s)
{
int result=0;
try {result=stmt.executeUpdate(s);}
catch(Exception ex) {System.out.println("执行更新错误!");}
return result;
}
public ResultSet executeQuery(String s)
{
ResultSet rs =null;
try {rs=stmt.executeQuery(s);}
catch(Exception ex) {System.out.println("执行查询错误!");}
return rs;
}
public void close()
{
try
{
stmt.close();
conn.close();
}
catch (Exception ex) {}
}
}
6.Login_ ser.java
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
String zh=request.getParameter("zh");
String mm=request.getParameter("mm");
int zh1=Integer.parseInt(zh);
int mm1=Integer.parseInt(mm);
session.setAttribute("zh", zh);
DBBean other =new DBBean();
String se ="select user, password from login ";
String a=null;
String b=null;
ResultSet rs = other.executeQuery(se);
try {
while(rs.next()) {
a= rs.getString("user");
b=rs.getString("password");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int a1=Integer.parseInt(a);
int b1=Integer.parseInt(b);
Login_tf s =new Login_tf(zh1, mm1, a1, b1);
int result= s.a();
if(result==1)
{
request.getRequestDispatcher("/loginjdbc/login_succeed.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("/loginjdbc/login_failed.jsp").forward(request, response);
}
}