由于此代码,无法编译JSP

时间:2017-03-20 03:03:10

标签: java jsp

我今天试图让自己学习JSP。

这是我正在测试的JSP代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page </title>
    </head>
    <body>
        <%! Queries x = new Queries(); %>
<TABLE BORDER=2>
<%

    for ( int i = 0; i < 20; i++ ) {
        %>
        <TR>
        <TD>Number</TD>
        <TD><%= i+1 %></TD>
        </TR>
        <%
    }
%>
</TABLE>
             <form action="parser">
            <p>Name:<input type="text" name="userName"></p>
            <p>Password:<input type="text" name="passWord"></p>
            <p><input type="submit" value="login"></p>
    </body>
</html>

当我尝试初始化时,问题出在这里:

<%! Queries x = new Queries(); %>

班级本身就在我的包裹里。

Queries.java

public class Queries {

 public String RetrieveNickName()  {
         Database conn = new Database(); 
         String sql = "SELECT nickname from user where uid = 1";
         String x = conn.querySQL(sql).toString();
         return x;
       } 
}

我尝试运行时收到此错误。

HTTP Status 500 - Unable to compile class for JSP: 


type Exception report

message Unable to compile class for JSP: 

description The server encountered an internal error that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 16 in the jsp file: /index.jsp
Queries cannot be resolved to a type
13:         <title>JSP Page </title>
14:     </head>
15:     <body>
16:         <%! Queries x = new Queries(); %>
17: <TABLE BORDER=2>
18: <%
19:     


An error occurred at line: 16 in the jsp file: /index.jsp
Queries cannot be resolved to a type
13:         <title>JSP Page </title>
14:     </head>
15:     <body>
16:         <%! Queries x = new Queries(); %>
17: <TABLE BORDER=2>
18: <%
19:     


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

http://imgur.com/a/NCSQd

3 个答案:

答案 0 :(得分:0)

请确保先导入课程。 <%@ page import="<your package>.Queries" %>

答案 1 :(得分:0)

也许您没有导入包裹:
<%@page contentType="text/html" pageEncoding="UTF-8"% import="packageName.Queries">

答案 2 :(得分:0)

问题似乎是默认包。默认包对于server是不可见的。所以你应该将它放在任何命名包中,类似于这个

/WEB-INF/classes/MyPackage/Queries.claass