2013-03-02 47 views
0

我上的作業,要求執行的Tomcat6服務器要導入類JSP中的Tomcat6

上的webapps在我已經運行Ant構建其產生TitlesBean JSP文件

<%@ 
page language = "java" 
import = "org.me.webapps.Bookstore.TitlesBean, java.util.*" 
session = "true" 
%> 
<!-- begin document --> 
<html xmlns = "http://www.w3.org/1999/xhtml"> 

<body> 
<%-- begin JSP scriptlet to create list of books --%> 
<% 
    TitlesBean titlesBean = new TitlesBean(); 
    List titles = titlesBean.getTitles(); 
    BookBean currentBook; 

    // store titles in session for further use 
    session.setAttribute("titles", titles); 

.... 

工作。但是,當我瀏覽jsp頁面時,出現如下錯誤,看起來像服務器無法找到或導入.class文件文件

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

An error occurred at line: 6 in the generated java file 
Only a type can be imported. org.me.webapps.bookstore.TitlesBean resolves to a package 

An error occurred at line: 32 in the jsp file: /war/books2.jsp 
TitlesBean cannot be resolved to a type 
29:  
30: <%-- begin JSP scriptlet to create list of books --%> 
31: <% 
32:  TitlesBean titlesBean = new TitlesBean(); 
33:  List titles = titlesBean.getTitles(); 
34:  BookBean currentBook; 
..... 

的TitlesBean.java

// TitlesBean.java 
// Class TitlesBean makes a database connection and retrieves 
// the books from the database. 
package org.me.webapps.bookstore; 

// Java core packages 
import java.io.*; 
import java.sql.*; 
import java.util.*; 
import java.net.*; 

public class TitlesBean implements Serializable { 
private static final long serialVersionUID = 6723471178342776147L; 
private Connection connection; 
private PreparedStatement titlesQuery; 

// construct TitlesBean object 
public TitlesBean() { 
    // attempt database connection and setup SQL statements 
    try { 
     URL myUrl = getClass().getResource("TitlesBean.class"); 

     System.out.println(getDatabasePath(myUrl.toString())); 

     Class.forName("org.hsqldb.jdbcDriver"); 
     connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/bookdb", "sa", ""); 

     titlesQuery = connection 
       .prepareStatement("SELECT isbn, title, editionNumber, " 
         + "copyright, publisherID, imageFile, price " 
         + "FROM titles ORDER BY title"); 
    } 

    // process exceptions during database setup 
    catch (SQLException sqlException) { 
     sqlException.printStackTrace(); 
    } 

    // process problems locating data source 
    catch (Exception exception) { 
     exception.printStackTrace(); 
    } 
} 

// return a List of BookBeans 
public List<BookBean> getTitles() { 
    List<BookBean> titlesList = new ArrayList<BookBean>(); 

    // obtain list of titles 
    try { 
     ResultSet results = titlesQuery.executeQuery(); 

     // get row data 
     while (results.next()) { 
      BookBean book = new BookBean(); 

      book.setISBN(results.getString("isbn")); 
      book.setTitle(results.getString("title")); 
      book.setEditionNumber(results.getInt("editionNumber")); 
      book.setCopyright(results.getString("copyright")); 
      book.setPublisherID(results.getInt("publisherID")); 
      book.setImageFile(results.getString("imageFile")); 
      book.setPrice(results.getDouble("price")); 

      titlesList.add(book); 
     } 
    } catch (SQLException exception) { 
     exception.printStackTrace(); 
    } 

    return titlesList; 
} 

private String getDatabasePath(String classPath) { 
    String path = ""; 
    String crtToken; 

    StringTokenizer tokens = new StringTokenizer(classPath, "/"); 
    int num = tokens.countTokens(); 
    tokens.nextToken(); 

    for (int i = 1; i < num; i++) { 
     crtToken = tokens.nextToken(); 
     if (crtToken.equals("classes")) { 
      break; 
     } 
     path = path + crtToken + "\\"; 
    } 

    return path; 
} 

// close statements and terminate database connection 
protected void finalize() { 
    // attempt to close database connection 
    try { 
     connection.close(); 
    } 

    // process SQLException on close operation 
    catch (SQLException sqlException) { 
     sqlException.printStackTrace(); 
    } 
} 
} 

的tomcat的設置應該是罰款,我可以爲他們不需要進口遠跑帶環或條件一些簡單的JSP文件。我還應該嘗試什麼?

+0

我想在進口JSP應該be'import =「org.me.webapps.bookstore.TitlesBean」'你有資本'B'在書店 – kaysush 2013-03-02 12:42:36

回答

0

TitlesBean位於org.me.webapps.bookstore並且在您的jsp導入中,您聲明它爲org.me.webapps.Bookstore.TitlesBean,因此您的jsp導入的聲明上有一個大寫字母B. 聲明像這樣

<%@ 
     page language = "java" 
     import = "org.me.webapps.bookstore.TitlesBean, java.util.*" 
     session = "true" 
%>