在JSP中调用Java类

时间:2013-10-29 02:47:35

标签: java class jsp

我已经完成了相当多的研究,从我所看到的,我有一切正确。我正在尝试在JSP文件中调用Java类。每次运行它时,都会出现以下错误:

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

An error occurred at line: 25 in the jsp file: /3680-HW4-Taylor-Jacob/HTML/main.jsp
DateAssistant.formatDate cannot be resolved to a type
22:             out.println("Welcome, " + request.getParameter("usrname") + "!");
23:             
24:             if (timezone.equalsIgnoreCase("United States")){
25:             out.write("

You logged in on: " + new DateAssistant.formatDate(gc, timezone) + "



"); 26: 27: } 28: out.write("Browse Movies Catalog"); 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)

我将Class文件放在正确的位置(WEB-INF / classes / wu / andy / DateAssistant.class),并且我已经从我看到的内容中正确导入了。这是我的JSP源代码:

<%@ page import="java.util.GregorianCalendar, wu.andy.*" %>
<%
    String interest = request.getParameter("interest");
    String password = request.getParameter("password");
    GregorianCalendar gc = new GregorianCalendar();
    int tz = 0;
    String timezone = request.getParameter("country");

    if(password.equals("JTaylor")){
        if(interest.equals("music")){
            response.sendRedirect("music.html");
        } 
        else if(interest.equals("games")){
            response.sendRedirect("games.html");
        } 
        else if(interest.equals("movies")){

            out.write("<html>");
            out.write("<head/>");
            out.write("<body>");
            out.write("<h1 style='color:green'>Movies Galore :: User Main Page</h1>");
            out.println("Welcome, " + request.getParameter("usrname") + "!");

            if (timezone.equalsIgnoreCase("United States")){
            out.write("<p>You logged in on: " + new DateAssistant.formatDate(gc, timezone) + "</p><br/><br/>");

            }
            out.write("<a href='movies.html'>Browse Movies Catalog</a>");


        }
    } else{
        response.sendRedirect("login.html");
    }
%>

我知道我的代码不是“好习惯”,但这就是我教授想要的。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

看起来您忘记在jsp标头中导入DateAssistant类。

<%@ page import="java.util.GregorianCalendar, wu.andy.*, XXXXX.DateAssistant" %>

答案 1 :(得分:0)

您的JSP专栏:

        out.write("<p>You logged in on: " + new DateAssistant.formatDate(gc, timezone) + "</p><br/><br/>");

使用new旁边的DateAssistant.formatDate()关键字。除非这是一个公共内部类,否则我认为这是一个静态方法,在这种情况下你应该放弃new