用javaweb写一个精美登入界面
时间: 2023-11-29 16:58:31 浏览: 87
一个java写的登录界面
3星 · 编辑精心推荐
以下是一个简单的精美登入界面的示例代码,使用Java Servlet和JSP技术:
index.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录界面</title>
<style type="text/css">
* { margin: 0; padding: 0; }
body { background-color: #f1f1f1; }
.login-box { width: 400px; height: 400px; margin: 150px auto; background-color: #fff; border-radius: 10px; box-shadow: 0 0 20px rgba(0,0,0,.2); }
.login-box h2 { text-align: center; font-size: 28px; color: #555; margin-top: 50px; }
.login-box form { margin-top: 50px; text-align: center; }
.login-box input[type="text"], .login-box input[type="password"] { width: 80%; height: 40px; padding: 0 10px; border: none; outline: none; border-radius: 20px; font-size: 16px; margin-bottom: 20px; }
.login-box input[type="submit"] { width: 40%; height: 40px; background-color: #4CAF50; border: none; outline: none; border-radius: 20px; color: #fff; font-size: 18px; cursor: pointer; }
.login-box input[type="submit"]:hover { background-color: #3e8e41; }
</style>
</head>
<body>
<div class="login-box">
<h2>登录</h2>
<form action="login" method="post">
<input type="text" name="username" placeholder="用户名"><br>
<input type="password" name="password" placeholder="密码"><br>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
```
LoginServlet.java:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin") && password.equals("123456")) {
response.sendRedirect("success.jsp");
} else {
response.sendRedirect("error.jsp");
}
}
}
```
success.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h2>欢迎您,admin!</h2>
</body>
</html>
```
error.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录失败</title>
</head>
<body>
<h2>用户名或密码错误!</h2>
</body>
</html>
```
该示例使用了HTML和CSS来创建一个简单而漂亮的登录界面。当用户提交表单时,Java Servlet负责处理登录验证逻辑,并根据结果将用户重定向到不同的页面。如果用户名和密码正确,则将用户重定向到success.jsp页面,否则将用户重定向到error.jsp页面。
阅读全文