jsp唯美登录注册界面代码

时间: 2023-10-01 16:06:56 浏览: 32
以下是一个简单的 JSP 登录/注册界面的代码示例,包括 HTML、CSS 和 JSP: HTML: ```html <!DOCTYPE html> <html> <head> <title>Login/Register</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <h1>Login</h1> <form action="login.jsp" method="POST"> <label for="username">Username:</label> <input type="text" id="username" name="username" required> <label for="password">Password:</label> <input type="password" id="password" name="password" required> <input type="submit" value="Login"> </form> <hr> <h1>Register</h1> <form action="register.jsp" method="POST"> <label for="new_username">Username:</label> <input type="text" id="new_username" name="new_username" required> <label for="new_password">Password:</label> <input type="password" id="new_password" name="new_password" required> <label for="confirm_password">Confirm Password:</label> <input type="password" id="confirm_password" name="confirm_password" required> <input type="submit" value="Register"> </form> </div> </body> </html> ``` CSS: ```css .container { margin: 0 auto; width: 50%; text-align: center; } form { display: inline-block; margin-top: 20px; } label { display: block; margin-top: 10px; } input[type=text], input[type=password] { padding: 10px; margin-top: 5px; margin-bottom: 15px; border: none; background-color: #f1f1f1; } input[type=submit] { background-color: #4CAF50; color: white; padding: 12px 20px; border: none; border-radius: 5px; cursor: pointer; margin-top: 10px; } input[type=submit]:hover { background-color: #45a049; } hr { border: 1px solid #f1f1f1; margin-top: 50px; margin-bottom: 50px; } ``` JSP - Login: ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.*" %> <% String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); pstmt = conn.prepareStatement("SELECT * FROM users WHERE username=? AND password=?"); pstmt.setString(1, username); pstmt.setString(2, password); rs = pstmt.executeQuery(); if(rs.next()) { out.println("<h2>Login Successful!</h2>"); out.println("<p>Welcome, " + username + "!</p>"); } else { out.println("<h2>Login Failed!</h2>"); out.println("<p>Invalid username or password.</p>"); } } catch(SQLException se) { se.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { try { if(rs!=null) rs.close(); if(pstmt!=null) pstmt.close(); if(conn!=null) conn.close(); } catch(SQLException se) { se.printStackTrace(); } } %> ``` JSP - Register: ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.*" %> <% String new_username = request.getParameter("new_username"); String new_password = request.getParameter("new_password"); String confirm_password = request.getParameter("confirm_password"); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); pstmt = conn.prepareStatement("SELECT * FROM users WHERE username=?"); pstmt.setString(1, new_username); rs = pstmt.executeQuery(); if(rs.next()) { out.println("<h2>Registration Failed!</h2>"); out.println("<p>Username already exists.</p>"); } else if(!new_password.equals(confirm_password)) { out.println("<h2>Registration Failed!</h2>"); out.println("<p>Passwords do not match.</p>"); } else { pstmt = conn.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)"); pstmt.setString(1, new_username); pstmt.setString(2, new_password); pstmt.executeUpdate(); out.println("<h2>Registration Successful!</h2>"); out.println("<p>Welcome, " + new_username + "!</p>"); } } catch(SQLException se) { se.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { try { if(rs!=null) rs.close(); if(pstmt!=null) pstmt.close(); if(conn!=null) conn.close(); } catch(SQLException se) { se.printStackTrace(); } } %> ``` 请注意,这只是一个简单的示例,可能不适用于生产环境。在实际使用中,您应该将密码存储为安全的哈希值,并实现更多的安全措施。

相关推荐

最新推荐

recommend-type

JSP实现用户登录、注册和退出功能

主要为大家详细介绍了JSP实现用户登录、注册和退出功能,具有实用性和参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

jsp编写简单的学生信息注册界面

信息注册界面&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="formal"name="formal"method="post"action="successful.jsp"&gt; 信息注册页面 学号: &lt;input name="usernum"type="text"id="usernum"/&...
recommend-type

JSP实现简单的用户登录并显示出用户信息的方法

主要介绍了JSP实现简单的用户登录并显示出用户信息的方法,通过简单的登陆及登陆显示页面实现这一功能,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

jsp登录系统设计源代码

-- 该Login页面是一个简单的登录界面 --&gt; &lt;!-- 该JSP程序是用来测试与MySQL数据库的连接, 需要一个数据库:LearnJSP,和其中一个表:userinfo 表中有两个字段分别为:UserName varchar (20) not null,UserPwd ...
recommend-type

钢桁架结构振动特性渐变分析工具

钢桁架结构振动特性渐变分析工具
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。