Jsp+Servlet实现简朴登录注册查询(2)

public class JDBCUtil { private JDBCUtil(){} //静态代码块在类加载时执行,而且执行一次。 static{ try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //获取数据库毗连工具 public static Connection getConnection() throws Exception{ String url="jdbc:mysql://127.0.0.1:3306/zy?&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"; String user="root"; String password="rootroot"; return DriverManager.getConnection(url,user,password); } /** *封锁资源 * @param conn 毗连工具 * @param ps 数据库操纵工具 * @param rs 功效集 */ public static void close(ResultSet rs, Statement ps, Connection conn){ if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

index.jsp

<%@ page import="entity.User" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>查询页面</title> </head> <body> <div> <h2/>输入用户名,查询信息 <form action="/findByName" method="get"> <input type="text"> <input type="submit" value="查询"> </form> <% User userInfo = (User) request.getAttribute("userInfo"); %> <% if (userInfo != null) { %> <table> <tr> <th>用户名</th> <th>暗码</th> <th>年数</th> </tr> <tr> <td> &nbsp; &nbsp; <%=userInfo.getName()%> &nbsp; &nbsp;</td> <td> &nbsp; &nbsp; <%=userInfo.getPassword()%> &nbsp; &nbsp;</td> <td> &nbsp; &nbsp; <%=userInfo.getAge()%> &nbsp; &nbsp;</td> </tr> </table> <% } %> </div> </body> </html>

register.jsp

<%@ page import="com.mysql.cj.util.StringUtils" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <br> <br> <% String info =(String) request.getAttribute("info"); %> <% if (!StringUtils.isNullOrEmpty(info)){ %> <h1 ><%=info%></h1> <% } %> <div> <form action="/register" method="post"> <table> <tr> <th>用户名</th> <td><input type="text"/></td> </tr> <tr> <th>暗码</th> <td><input type="password"/></td> </tr> <tr> <th>年数</th> <td><input type="text"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value="注册"/> <input type="reset" value="清空"/> </td> </tr> </table> </form> </div> </body> </html>

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee " version="4.0"> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>servlet.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>RegisterServlet</servlet-name> <servlet-class>servlet.RegisterServlet</servlet-class> </servlet> <servlet> <servlet-name>FindByNameServlet</servlet-name> <servlet-class>servlet.FindByNameServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>FindByNameServlet</servlet-name> <url-pattern>/findByName</url-pattern> </servlet-mapping> <!--配置默认接待文件法则--> <welcome-file-list> <welcome-file>login.html</welcome-file> <!--servlet 作为默认接待文件 ‘/'需要去掉--> </welcome-file-list> </web-app>

login.html

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsddfj.html