JAVA web实现密码登录跳转

首先新建webproject并添加Tomcat Server

网页主体设置

<%--
  Created by IntelliJ IDEA.
  User: 25477
  Date: 2020/6/29
  Time: 10:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form action="/login" method="post">
    name:<input name="name" type="text">
    password:<input name="password" type="password">
    <input type="submit" value="login">
  <<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现登录页面需要以下步骤: 1. 创建一个 HTML 页面,包含用户名和密码的输入框以及提交按钮。 ```html <!DOCTYPE html> <html> <head> <title>Login Page</title> </head> <body> <h2>Login</h2> <form action="login" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Submit"> </form> </body> </html> ``` 2. 在后端编写 Java 代码实现登录验证逻辑。这里以 Servlet 为例,需要重写 `doPost` 方法,读取表单数据并判断用户名和密码是否正确。 ```java public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if (username != null && username.equals("admin") && password != null && password.equals("admin123")) { // 登录成功 request.getSession().setAttribute("username", username); response.sendRedirect("success.html"); } else { // 登录失败 response.sendRedirect("error.html"); } } } ``` 3. 在 web.xml 中配置 Servlet 映射和静态资源映射,使得浏览器访问 /login 能够调用 LoginServlet 进行登录验证。 ```xml <web-app> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.example.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/404.html</location> </error-page> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/mydb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app> ``` 4. 运行项目,浏览器访问登录页面,输入正确的用户名和密码后,即可跳转到成功页面;输入错误的用户名和密码后,即可跳转到错误页面。 以上是一个简单的 Java Web 实现登录页面的过程,具体实现方式还有很多种,可以根据自己的需求和技术水平进行选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值