问题描述
我正在处理一项任务,该任务要求在Tomcat6服务器上实现Web应用程序
I am working on an assignment that asked to implement a webapps on Tomcat6 server
在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 );
....
我已经运行了ant build,它会在下面生成TitlesBean.class文件/WEB-INF/classes/org/me/webapps/bookstore/
I have run ant build which generate the TitlesBean.class file under/WEB-INF/classes/org/me/webapps/bookstore/
但是,当我浏览jsp页面时,出现如下错误,看起来服务器无法找到或导入.class文件
However, when i browse the jsp page, i got the error as follow, looks like the server cannot find or import the .class file
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
the 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文件,只要它们不需要导入即可.我还应该尝试什么?
The setting of tomcat should be fine as i can run some simple jsp file with loop or condition as far as they dont need to import. What else should i try?
推荐答案
TitlesBean
位于org.me.webapps.bookstore
上,并且在您的jsp导入中,您已将其声明为org.me.webapps.Bookstore.TitlesBean
,并且您的大写字母B在您的jsp导入中进行声明.像这样声明
TitlesBean
is located at org.me.webapps.bookstore
and on your jsp import, you've declared it as org.me.webapps.Bookstore.TitlesBean
, you have an uppercase letter B on your declaration on your jsp import.declare it like this
<%@
page language = "java"
import = "org.me.webapps.bookstore.TitlesBean, java.util.*"
session = "true"
%>
这篇关于将类导入到Tomcat6中的JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!