JSP數(shù)據(jù)庫(kù)配置:程序和頁(yè)面的設(shè)置及測(cè)試效果
作者:佚名
之前介紹了JSP數(shù)據(jù)庫(kù)配置中MySQL表的建立和Eclipse項(xiàng)目的建立,下面繼續(xù)介紹JavaBean程序以及JSP頁(yè)面的設(shè)置,以及測(cè)試結(jié)果。
以下繼續(xù)介紹JSP數(shù)據(jù)庫(kù)配置的步驟。
JSP數(shù)據(jù)庫(kù)配置步驟三
在項(xiàng)目下新建包beans,在此包下編寫一個(gè)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() {
- /**
- * 關(guān)閉數(shù)據(jù)連接的方法
- * */
- 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();
- }
- /**
- * 調(diào)用關(guān)閉數(shù)據(jù)連接的方法,關(guān)閉數(shù)據(jù)庫(kù)連接
- * */
- closeConn();
- return tag;
- }
- }
JSP數(shù)據(jù)庫(kù)配置步驟四
新建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>實(shí)驗(yàn)二利用JavaBean實(shí)現(xiàn)用戶登錄< /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('登錄失敗!您沒(méi)有權(quán)限訪問(wèn)!');< /script");
- }
- %>
- < /body>
- < /html>
JSP數(shù)據(jù)庫(kù)配置步驟五
創(chuàng)建以歡迎登錄成功的頁(yè)面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%>,歡迎您訪問(wèn)!
- < %
- }
- %>
- < /body>
- < /html>
JSP數(shù)據(jù)庫(kù)配置步驟六
測(cè)試效果,如下:
①未進(jìn)行登錄操作:
②登錄成功
③登錄失敗
【編輯推薦】
責(zé)任編輯:yangsai
來(lái)源:
百度空間