【问题标题】:I am not sure why I am getting an error for this simple code in a jsp file [closed]我不确定为什么我在jsp文件中收到这个简单代码的错误[关闭]
【发布时间】:2019-11-20 19:35:36
【问题描述】:

this is the error that comes up 我已经尝试了几件事来使它工作,但我似乎无法弄清楚这个问题。这部分是决定登录是否成功的部分,错误在登录成功的部分,它输出并发布用户名以供下一页使用。错误专门在此行<%=out.print(idnum)%> 编辑:该行在发布道歉时由于某种原因被切断。我还添加了整个代码,不要介意明显的安全问题,这对我的工作并不重要

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%
        //user variable declarations
        String idnum = "";
        String pwd = "";

        String user = request.getParameter("user");
        String password = request.getParameter("pwd");
        Class.forName("com.mysql.jdbc.Driver");
        java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignment2", "root",
                "1234");
        Statement stmt = conn.createStatement();
        String query = "SELECT * from users where idnum = '" + user + "'";
        //out.println("Query: "+query);
        ResultSet rs = stmt.executeQuery(query);

        //get idnum and password for checks
        while (rs.next()) {
            idnum = rs.getString("idnum");
        }
        query = "SELECT * from users where idnum = '" + user + "'";
        rs = stmt.executeQuery(query);
        while (rs.next()) {
            pwd = rs.getString("password");
        }
        if (pwd.equals(password)) {
    %>
    </br>Welcome 
    <form action="BuyTicket.jsp" method="post">
        <%=out.print(idnum)%>
        <input type="submit" text="Buy Tickets" />
    </form>

    </br>
    <%
        } else {
    %>
    </br>Invalid Username or Password please try one of the following
    </br>
    <a href="Login.html">Login</a>
    </br>
    <a href="Register.html">Register</a>
    </br>
    <%
        }
    %>
    <a href="index.html">Home</a>
</body>
</html>

【问题讨论】:

  • 我很困惑。这是 PHP 还是 Java?
  • 好吧,你删掉了有用的信息,让我们看到错误出现在哪一行......既然你可以复制和粘贴文本,为什么还要发布屏幕截图?
  • 不知道那是什么,但是,按照纯逻辑,不应该将任何%&gt; 与开头&lt;% 配对吗? (第 3 行)并将错误消息作为链接和图片发布 - 很难阅读
  • @spork 在标题中它是一个 jsp。这段代码是jsp中的java。
  • @LowKeyEnergy 很抱歉,由于某种原因,它在发布时被切断了。我按照我的意愿把线放回去了

标签: java html jsp


【解决方案1】:

对于 if 语句开始的表达式,您没有开头的 &lt;%。试试这个:

<%=out.print(idnum)%>
<%
    if (pwd.equals(password)) {
%>
    </br>Welcome 
    <form action="BuyTicket.jsp" method="post">
        <%=out.print(idnum)%>
        <input type="submit" text="Buy Tickets" />
    </form>

    </br>
<%
    } else {
%>
    </br>Invalid Username or Password please try one of the following
    </br>
    <a href="Login.html">Login</a>
    </br>
    <a href="Register.html">Register</a>
    </br>
<%
    }
%>

【讨论】:

  • 这是一个sn-p的代码。如果这更有帮助,我可以发布整个代码。它具有 jsp 所需的所有标签,只是 java 代码不起作用
  • 当然可以发布。基本上,你需要平衡所有的 scriplet 标签,当你想编写 java 代码时,它必须在 &lt;%%&gt; 标签之间。
  • 添加了所有代码
  • 我认为你不需要out.print,你有out.print(idnum),这是一个打印到输出的JSP表达式,所以你应该能够只做&lt;%= idnum %&gt;
  • 没意识到这行得通,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多