javaweb登录注册页面
时间: 2023-09-13 16:09:59 浏览: 81
JavaWeb登录注册页面
以下是一个简单的JavaWeb登录注册页面的示例,包含了基本的HTML、CSS和Java代码。你可以根据需要进行修改和完善。
index.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login/Register</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="form-container">
<ul class="tab-group">
<li class="tab active"><a href="#login">Log In</a></li>
<li class="tab"><a href="#register">Register</a></li>
</ul>
<div class="tab-content">
<div id="login">
<h1>Welcome Back!</h1>
<form action="login" method="post">
<div class="field-wrap">
<label>
Email Address<span class="req">*</span>
</label>
<input type="email" name="email" required>
</div>
<div class="field-wrap">
<label>
Password<span class="req">*</span>
</label>
<input type="password" name="password" required>
</div>
<button class="button button-block">Log In</button>
</form>
</div>
<div id="register">
<h1>Sign Up for Free</h1>
<form action="register" method="post">
<div class="top-row">
<div class="field-wrap">
<label>
First Name<span class="req">*</span>
</label>
<input type="text" name="firstName" required>
</div>
<div class="field-wrap">
<label>
Last Name<span class="req">*</span>
</label>
<input type="text" name="lastName" required>
</div>
</div>
<div class="field-wrap">
<label>
Email Address<span class="req">*</span>
</label>
<input type="email" name="email" required>
</div>
<div class="field-wrap">
<label>
Password<span class="req">*</span>
</label>
<input type="password" name="password" required>
</div>
<button type="submit" class="button button-block">Register</button>
</form>
</div>
</div>
</div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src="script.js"></script>
</body>
</html>
```
style.css
```css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background: #f5f5f5;
-webkit-font-smoothing: antialiased;
}
.container {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.2);
overflow: hidden;
}
.form-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 400px;
height: 400px;
background: #fff;
border-radius: 10px;
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
}
.tab-group {
list-style: none;
padding: 0;
margin: 0 0 40px 0;
position: relative;
}
.tab-group:after {
content: "";
display: table;
clear: both;
}
.tab-group li a {
display: block;
text-decoration: none;
padding: 15px;
background: rgba(0, 0, 0, 0.25);
color: #fff;
font-size: 20px;
float: left;
width: 50%;
text-align: center;
cursor: pointer;
transition: all 0.5s ease;
}
.tab-group li:first-child a {
border-top-left-radius: 10px;
}
.tab-group li:last-child a {
border-top-right-radius: 10px;
}
.tab-group .active a {
background: #fff;
color: #000;
}
.tab-content > div:last-child {
display: none;
}
h1 {
text-align: center;
font-size: 30px;
margin: 0;
padding: 0 0 20px 0;
}
.field-wrap {
position: relative;
margin-bottom: 20px;
}
.field-wrap input {
font-size: 18px;
width: 100%;
padding: 10px 0;
border: none;
border-bottom: 1px solid #ddd;
background-color: transparent;
transition: border-color 0.5s ease;
}
.field-wrap input:focus {
outline: none;
border-color: #4a89dc;
}
.field-wrap input:valid ~ label {
top: -20px;
font-size: 14px;
color: #4a89dc;
}
.field-wrap input:focus ~ label {
top: -20px;
font-size: 14px;
color: #4a89dc;
}
.field-wrap label {
position: absolute;
top: 0;
left: 0;
font-size: 18px;
color: #999;
transition: all 0.5s ease;
}
.button {
border: none;
background: #4a89dc;
color: #fff;
padding: 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 18px;
border-radius: 5px;
width: 100%;
cursor: pointer;
transition: all 0.5s ease;
}
.button:hover,
.button:focus {
background: #4c6ef5;
}
.button-block {
display: block;
}
.forgot {
margin-top: -20px;
text-align: right;
}
```
script.js
```javascript
$(document).ready(function(){
$('.tab a').on('click', function(e){
e.preventDefault();
$(this).parent().addClass('active');
$(this).parent().siblings().removeClass('active');
target = $(this).attr('href');
$('.tab-content > div').not(target).hide();
$(target).fadeIn(600);
});
});
```
LoginServlet.java
```java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String password = request.getParameter("password");
// validate email and password
if (email.equals("example@example.com") && password.equals("password")) {
HttpSession session = request.getSession();
session.setAttribute("email", email);
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("index.html");
}
}
}
```
RegisterServlet.java
```java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
String password = request.getParameter("password");
// validate form data
if (firstName != null && lastName != null && email != null && password != null) {
response.sendRedirect("success.html");
} else {
response.sendRedirect("index.html");
}
}
}
```
welcome.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome, <%= session.getAttribute("email") %>!</h1>
<a href="logout">Log Out</a>
</body>
</html>
```
success.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success</title>
</head>
<body>
<h1>Registration Successful!</h1>
</body>
</html>
```
注意:上述代码只是一个简单的示例,没有进行任何数据加密和用户身份验证,只是用来演示JavaWeb登录注册流程的基本原理和实现方式。在实际应用中,需要进行更为严格的数据验证和安全措施,以确保用户的数据和隐私安全。
阅读全文