以下继续介绍JSP数据库配置的步骤。
JSP数据库配置步骤三
在项目下新建包beans,在此包下编写一个JavaBean程序,命名为Test_2_4.java,代码为:
- package
- import java.io.UnsupportedEncodingException;
- import java.sql.*;
- import java.util.ResourceBundle;
- public class Test_2_4 {
- private String username;
- private String password;
- private Connection conn = null;
- private PreparedStatement ps = null;
- private ResultSet rs = null;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username)
- throws UnsupportedEncodingException {
- String temp = new String(username.getBytes("iso8859-1"), "utf-8");
- this.username = temp;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- private void closeConn() {
- /**
- * 关闭数据连接的方法
- * */
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- ps = null;
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- rs = null;
- if (conn != null)
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- conn = null;
- }
- public int query() {
- int tag = 0;
- if (username == null || password == null) {
- return 0;
- }
- ResourceBundle rb = ResourceBundle.getBundle("init");
- String dbDirver = rb.getString("connJDBC.dbDriver");
- String dbUrl = rb.getString("connJDBC.dbURL");
- String dbUsername = rb.getString("connJDBC.dbUsername");
- String dbPwd = rb.getString("connJDBC.dbPassword");
- try {
- Class.forName(dbDirver);
- conn = DriverManager.getConnection(dbUrl, dbUsername, dbPwd);
- String sql = "select * from users where username=? and password=?";
- ps = conn.prepareStatement(sql);
- ps.setString(1, username);
- ps.setString(2, password);
- rs = ps.executeQuery();
- if (rs.next()) {
- return 1;
- } else {
- return -1;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- /**
- * 调用关闭数据连接的方法,关闭数据库连接
- * */
- closeConn();
- return tag;
- }
- }
JSP数据库配置步骤四
新建jsp文件,命名为test_2_4.jsp,代码如下:
- < %@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- < jsp:useBean id="login" class="beans.Test_2_4" scope="session" />
- < jsp:setProperty name="login" property="*" />
- < !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=UTF-8">
- < title>实验二利用JavaBean实现用户登录< /title>
- < /head>
- < body>
- < form action="test_2_3.jsp" method="post">
- < div align="center">用户名< input type="text" name="username"
- size="16">< /div>
- < div align="center">密 码< input
- type="password" name="password" size="16">< /div>
- < div align="center">< input type="submit" value="登录"> < input
- type="reset" value="重置">< /div>
- < /form>
- < %
- request.setCharacterEncoding("utf-8");
- int isLogin = login.query();
- if (isLogin == 1) {
- String username = request.getParameter("username");
- session.putValue("username", username);
- response.sendRedirect("welcome.jsp");
- } else if (isLogin == -1) {
- out.println("< script language=javascript>alert('登录失败!您没有权限访问!');< /script");
- }
- %>
- < /body>
- < /html>
JSP数据库配置步骤五
创建以欢迎登录成功的页面welcome.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=UTF-8">
- < title>登录成功< /title>
- < /head>
- < body>
- < %
- request.setCharacterEncoding("utf-8");
- if (session.getValue("username") == ""
- || session.getValue("username") == null) {
- response.sendRedirect("test_2_4.jsp");
- } else {
- String username = session.getValue("username").toString();
- String user = new String(username.getBytes("iso8859-1"),
- "utf-8");
- %>
- < %=user%>,欢迎您访问!
- < %
- }
- %>
- < /body>
- < /html>
JSP数据库配置步骤六
测试效果,如下:
①未进行登录操作:
②登录成功
③登录失败
【编辑推荐】