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

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

转载注明出处:http://www.heiqu.com/d19d909eb47248798ba04f57a69ae258.html