In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to configure the database in JSP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Step 3 of JSP database configuration
Create a new package beans under the project, and write a JavaBean program under this package, named Test_2_4.java, with the following code:
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 () {/ * method to close data connection * * / 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 ();} / * call the method to close the data connection and close the database connection * * / closeConn (); return tag;}}
Step 4 of JSP database configuration
Create a new jsp file and name it test_2_4.jsp. The code is as follows:
< %@ 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>Experiment 2 uses JavaBean to realize user login
< /title> < /head> < body> < form action="test_2_3.jsp" method="post"> < div align="center">User name
< input type="text" name="username" size="16"> < /div> < div align="center">Secret code
< 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 ('login failed! You do not have permission to access!')
< /script"); } %> < /body> < /html>Step 5 of JSP database configuration
Create a page welcome.jsp to welcome the successful login, as follows:
< %@ 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>Login succeeded
< /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%>Welcome to visit!
< % } %> < /body> < /html>Step 6 of JSP database configuration
The test results are as follows:
① did not log in:
② login succeeded
③ login failed
This is the end of the article on "how to configure the database in JSP". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.