serlvet怎么控制另外一个html文件中标签的内容

J2EE 码拜 7年前 (2017-04-20) 1592次浏览
本人菜鸟,现在在做一个登陆页面。登陆页面是login.html,用户输入用户名和密码之后,点击登录按钮,LoginVerify.javat验证用户名和密码能否正确,假如错误,login.html显示:“用户名或密码错误”,现在的问题是,本人不知道怎么通过servlet让login.html显示这个错误信息。(修改login.html的p标签内容)
本人相当菜,恳请诸位高手帮本人实现这个功能。为了尽量看最少的代码,代码缩减如下:
login.html:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录页面</title>
</head>
<body>
    <form action="LoginVerify" method="post">
        <input type="text" id="user_name" name="user_name" />
                <input type="password" id="user_pwd" name="user_pwd" />
                <input type="submit"  value="登录" />
    </form>
    <p id="warning" name="warning">servlet写入的内容请放在这里</p>
</body>
</html>

LoginVerify.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 验证用户名和密码能否正确
        String userNameString = request.getParameter("user_name");
        String userPwdString = request.getParameter("user_pwd");
         
        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Test";
        String userName="sa";
        String userPwd="GnZx@801";
        Connection dbConnection;
        Statement statement;
        ResultSet resultSet;
        String sqlString = "SELECT user_name, user_pwd FROM tb_user WHERE user_name="" + userNameString + "" AND user_pwd="" + 
                userPwdString + """;
         
        try {
            Class.forName(driverName);
             
            try {
                dbConnection = DriverManager.getConnection(dbURL, userName, userPwd);
                statement = dbConnection.createStatement();
                resultSet = statement.executeQuery(sqlString);
                 
                // 假如在数据库中找不到指定的用户名和密码,说明用户登录输入的用户名或密码错误
                if (!resultSet.next()) {
                    // 不知道怎么样写
                     
                }
                     
                resultSet.close();
                statement.close();
                dbConnection.close();
                 
                // 设置响应内容类型
                response.setContentType("text/html;charset=UTF-8");
                // 要重定向的新位置
                response.sendRedirect("content/index.html"); 
            } catch (SQLException e) {
                e.printStackTrace();
            }
             
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
    }
解决方案

10

用Session或Request来返回无法登陆信息

10

或HTML换成JSP然后用${}表达式

20

1.使用jsp,servelt里面foward到jsp,jsp根据servlet传入的数据判断显示(当前你也可以不用servlet直接用jsp)
2.使用ajax

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明serlvet怎么控制另外一个html文件中标签的内容
喜欢 (0)
[1034331897@qq.com]
分享 (0)