You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用JavaScript在同一HTML登录页显示密码错误提示

在同一登录页显示密码错误提示(无需JavaScript)

Got it, let's fix this step by step! The core issue you're hitting is that static HTML files can't parse JSP EL expressions like ${errMsg}—right now, your login.html is just showing the literal text ${errMsg} instead of the error message you set in the Servlet. We can fix this with a few simple changes, no JavaScript required.

Step 1: Convert your static HTML to a JSP page

Rename login.html to login.jsp. JSP (JavaServer Pages) is a dynamic page format that your servlet container will process, allowing it to read and display the errMsg attribute you set in your Servlet.

Step 2: Update the error message display in the JSP

Adjust the login form to only show the error message when it exists (so you don't have an empty red span on page load). You can use either JSTL tags or raw Java code for this:

Option 1: Using JSTL (cleaner approach)

<p>Sign into your account</p>
<form action="bs" align="center" method="POST">
    <input type="text" name="u_name" placeholder="Username"><br>
    <input type="password" name="pswd2" placeholder="Password"><br>
    <br>
    <input type="submit" value="Login!">
    <%-- Only show the error if errMsg isn't empty --%>
    <c:if test="${not empty errMsg}">
        <span style="color:red;">${errMsg}</span>
    </c:if>
</form>

Don't forget to add the JSTL core taglib reference at the top of your login.jsp if you haven't already:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Option 2: Using raw Java code (no JSTL needed)

If you don't want to use JSTL, you can do a direct check with Java:

<p>Sign into your account</p>
<form action="bs" align="center" method="POST">
    <input type="text" name="u_name" placeholder="Username"><br>
    <input type="password" name="pswd2" placeholder="Password"><br>
    <br>
    <input type="submit" value="Login!">
    <% 
        String errMsg = (String) request.getAttribute("errMsg");
        if (errMsg != null && !errMsg.isEmpty()) {
    %>
        <span style="color:red;"><%= errMsg %></span>
    <% } %>
</form>

Step 3: Update the Servlet's forward target

Change the RequestDispatcher path from login.html to login.jsp so your servlet forwards to the dynamic JSP page instead of the static HTML:

// Replace your existing forward code with this
request.setAttribute("errMsg", "Invalid username or password");
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
dispatcher.forward(request, response);

Full Modified Code Examples

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Login</title>
</head>
<body>
<p>Sign into your account</p>
<form action="bs" align="center" method="POST">
    <input type="text" name="u_name" placeholder="Username"><br>
    <input type="password" name="pswd2" placeholder="Password"><br>
    <br>
    <input type="submit" value="Login!">
    <c:if test="${not empty errMsg}">
        <span style="color:red;">${errMsg}</span>
    </c:if>
</form>
</body>
</html>

Servlet (Key Validation Logic)

// Your existing database check logic
if(rs.next()) {
    ResultSet rs1=stmt.executeQuery(vsql1);
    if(rs1.next()) {
        out.println("----Welcome----");
        // Optional: Redirect to a home page instead of printing here
        // response.sendRedirect("home.jsp");
    } else {
        request.setAttribute("errMsg", "Invalid username or password");
        RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
        dispatcher.forward(request, response);
    }
} else {
    request.setAttribute("errMsg", "Invalid username or password");
    RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
    dispatcher.forward(request, response);
}

How This Works

  • When validation fails, your Servlet stores the error message in the request object and forwards to login.jsp.
  • The JSP page parses the ${errMsg} expression, reads the value from the request, and displays it as red text only when it exists.
  • Everything happens server-side, so no JavaScript is required at all.

内容的提问来源于stack exchange,提问作者RquitS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:22:00