十一、通过匹配数据库中存储的用户名、密码信息进行登录

1、通过前台对输入的用户名、密码、id进行与存储在数据库t_user表中的信息进行匹配,如果匹配则跳转到登录成功页面。不匹配则跳转到登录失败页面

 

LoginServlet

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.awt.RepaintArea;

public class LoginServlet extends HttpServlet {
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  req.setCharacterEncoding("UTF-8");
  String name = req.getParameter("name");
  String password = req.getParameter("password");
  String id=req.getParameter("id");
  System.out.println(name);
  System.out.println(password);
  System.out.println(Integer.parseInt(id));
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  boolean loginSuccess = false;
  // 连接数据库验证用户名和密码
  try {
   // 1、注册驱动
   Class.forName("oracle.jdbc.OracleDriver");

  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  try {
   // 2、获取连接
   conn = DriverManager.getConnection(
     "jdbc:oracle:thin:@10.125.4.55:1521:orcl", "zsgsdfgs",
     "zds123");
   conn.setAutoCommit(false);
   String sqlString = "select * from t_user where username=? and password=? and id=?";
   ps=conn.prepareStatement(sqlString);
   System.out.println(ps);
   ps.setString(1, name);
   ps.setString(2, password);
   ps.setInt(3, Integer.parseInt(id));
   rs = ps.executeQuery();
   if (rs.next()) {
    loginSuccess = true;
   }
   conn.commit();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   if (rs != null) {
    try {
     rs.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (conn != null) {
    try {
     conn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (ps != null) {
    try {
     ps.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

  if (loginSuccess) {
   resp.sendRedirect(req.getContextPath() + "/welcome.html");
  } else {
   resp.sendRedirect(req.getContextPath() + "/login_error.html");
  }
 }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>prj-servlet12</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>Login.html</welcome-file>
  </welcome-file-list>
  <servlet>
   <servlet-name>login</servlet-name>
   <servlet-class>LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>login</servlet-name>
   <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
 <form action="/prj-servlet12/login" method="post">
  username<input type="text" name="name">
  password<input type="text" name="password">
  id<input type="text" name="id">
  <input type="submit" value="login">
 </form>
</body>
</html>

welcome.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>welcome page</title>
</head>
<body>
login success,welcome!
</body>
</html>

login_error.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败,用户名不存在,或者密码错误,请重新<a href="/prj-servlet19/login.html">登录</a>
</body>
</html>

 

posted @ 2017-02-16 14:16  爱笑的berg  阅读(1445)  评论(0编辑  收藏  举报