JSP无法在Java资源中使用类

时间:2014-05-23 16:43:24

标签: java eclipse jsp

我使用eclipse来管理JSP项目,我似乎无法使用我的Java类。引发的异常如下所示:

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: 10 in the jsp file: /Login.jsp
UserSession cannot be resolved to a type
7: <body>
8:  <%
9:      File user = new File("C:\\Users\\Elijah\\" + request.getParameter("usr") + ".csv");
10:         UserSession usr = new UserSession(request.getParameter("usr"));
11:     
12:         FileInputStream fstream = new FileInputStream(user);
13:         DataInputStream reader = new DataInputStream(fstream);


An error occurred at line: 10 in the jsp file: /Login.jsp
UserSession cannot be resolved to a type
7: <body>
8:  <%
9:      File user = new File("C:\\Users\\Elijah\\" + request.getParameter("usr") + ".csv");
10:         UserSession usr = new UserSession(request.getParameter("usr"));
11:     
12:         FileInputStream fstream = new FileInputStream(user);
13:         DataInputStream reader = new DataInputStream(fstream);


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.47

经过一番环顾,我找到了this。然而,这没有用,因为eclipse不允许我这样做。我创建了classes文件夹,但是当我尝试创建包时,它会将它抛入java资源,而不是让我把它放在WEB-INF中。还有其他方法可以解决这个问题。在eclipse中生成的WEB-INF中有一个lib文件夹,但我不知道它是否可行。

2 个答案:

答案 0 :(得分:3)

您错过了在JSP中导入UserSession类。只需使用import页面指令在文件的最开头导入JSP中的类。

<%@ page import="com.x.y.z.UserSession" %>

或者使用该类的完全限定名称。

com.x.y.z.UserSession usr=new com.x.y.z.UserSession(request.getParameter("usr"));

页面指令

page指令可以包含所有导入包的列表。要导入多个项目,请用逗号分隔包名称,例如

<%@ page import="java.util.*,java.text.*" %>

答案 1 :(得分:1)

您必须使用import指令将UserSession类导入jsp。

导入页面指令的语法是:

你甚至可以使用一个指令导入多个类,用逗号分隔它们

您可以阅读本文:

http://docs.oracle.com/cd/A87860_01/doc/java.817/a83726/genlovw3.htm