【问题标题】:muliple annotations found at this line in jsp在 jsp 的这一行找到多个注释
【发布时间】:2016-11-22 10:11:06
【问题描述】:

我在一个jsp程序中工作,尝试自己学习jsp。

所以我做了一个测验程序,问题是从数据库表中获取的。所以这里是测验页面的代码,问题发布的地方。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <%@ include file="_header.jsp"%>

    <center>QUIZ PROGRAM</center>

    <br />

    <%@ page import="java.sql.*"%>

    <%
        //print the question and answer

        int questionaire = 1;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz", "root", "");
        PreparedStatement pst = con.prepareStatement("SELECT * FROM questions WHERE id = ?");
        pst.setInt(1, questionaire);

        ResultSet rs = pst.executeQuery();

        int questionId;
        String questionp;

        if (rs.next()) {
            questionId = rs.getInt("id");
            String questionp = rs.getString("question");
            //String option1 = rs.getString("option1");
            //String option2 = rs.getString("option2");
            //String right = rs.getString("right");
            questionaire++;
        }
        //get the answer and check
        //String question1 = "asd";
    %>

    <center>
        <form method="post" action="quiz.jsp">
            <table border="1" cellpadding="5" cellspacing="2" align="center">
                <thead>
                    <tr>
                        <th colspan="2"><% out.println(questionp); %></th>
                    </tr>
                </thead>
                <tbody>

                    <tr>
                        <td><input type="radio" name="question"
                            value="value1">Yes</td>
                        <td><input type="radio" name="question"
                            value="value2">No</td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit"
                            value="Next" onClick="next();" /></td>
                    </tr>
                </tbody>
            </table>
        </form>
    </center>



</body>
</html>

在上面的代码中,我在这一行中收到一个错误,指出,

String questionp = rs.getString("question");

在这一行发现了多个注释

【问题讨论】:

  • 您的问题是什么?您面临的问题是什么?请参阅此link 了解如何正确陈述您的问题。
  • &lt;% out.println("questionp"); %&gt; 正在打印值 questionp 而不是变量。使用&lt;% out.println(questionp); %&gt;&lt;%= questionp %&gt;(我认为)。但就像 greenPadawan 说的,请看How to Ask 并提供minimal reproducible example 下一次。只要写出来,你就会发现你的问题。我要补充一点,您应该快速学习如何使用ServletsJstl 来正确编写JSP;)
  • @greenPadawn 抱歉,我没有解释我遇到的问题,现在我已经编辑了问题,解释了我的问题,但仍然显示错误。
  • @AxelH 谢谢,我已经更新了我的问题,正如你所说,我通过删除双引号更改了代码

标签: jsp


【解决方案1】:

你的输出是错误的

<% out.println("questionp"); %>

这不会打印您恢复的值,而是文本“questionp”。

你需要输出变量

<% out.println(questionp); %>

或者

<%= questionp %>

更多解决方案,see the post

编辑:

对于您的编辑,multiple annontaion 表示您已经声明了一个具有此名称的变量。在这里,您声明了两次questionp

String questionp;
if (rs.next()) {
    String questionp = rs.getString("question");

删除类型:

String questionp = null;
if (rs.next()) {
    questionp = rs.getString("question");

【讨论】:

  • 我在行中遇到错误, 局部变量 questionp 可能尚未初始化
  • @javailike 这是因为我没有检查......因为你没有else,所以字符串可能永远不会被初始化。因此,使用默认值或 null 初始化值。 (见编辑代码)这是一些基本的Java,不懂Java的不要学JSP。
  • int 怎么样,int questionId = null;这告诉我错误
  • 再说一遍,这是基本的 Java,不要着急……int 是原始类型,不能为空。但是您始终可以将其设置为不可能的值,如果您只有正数,则为 -1。
  • 感谢它的解决,但是这一行 在 eclipse ide 中显示错误
猜你喜欢
  • 2015-10-02
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多