Programming using JSP and Tomcat: cannot be resolved to a type error

V

Vincent Ly

I'm aware that this is a popular error:

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

An error occurred at line: 19 in the jsp file: /sample.jsp
Connection.Connect cannot be resolved to a type
16: try
17: {
18:
19: Connection.Connect con = new Connection.Connect();
20: ResultSet rs = con.execute(query);
21:
22: int numCols = rs.getMetaData().getColumnCount();


An error occurred at line: 19 in the jsp file: /sample.jsp
Connection.Connect cannot be resolved to a type
16: try
17: {
18:
19: Connection.Connect con = new Connection.Connect();
20: ResultSet rs = con.execute(query);
21:
22: int numCols = rs.getMetaData().getColumnCount();

From most solutions, it appears that I misplaced the java class.
However, I've placed the Connect class in WEB-INF/classes/Connection/,
which was what was recommended by most. This is the error I still
received.

I'm sorry, but is there something I'm doing wrong?
 
V

Vincent Ly

Ah, right. This is the code I was using.

<h3>JSP and JDBC</h3>
<hr>

<%-- JSP directive --%>
<%@ page import="java.net.*, java.io.*, java.sql.*, java.util.*"
%>

<%-- JSP scriplet --%>
<%
String query=request.getParameter("query");

try
{

Connection.Connect con = new Connection.Connect
();
ResultSet rs = con.execute(query);

int numCols = rs.getMetaData().getColumnCount
();

%><TABLE BORDER=2 ALIGN=LEFT><%
%><TR><%
int j=0;
for (j=1; j<=numCols; j++)
out.println("<TH>"+
rs.getMetaData().getColumnName(j)+"</TH>");
%></TR><%

while (rs.next())
{
%><TR><%
int i=0;
for (i=1; i<=numCols; i++)
out.println("<TD>"+rs.getString(i)+"</
TD>");
%></TR><%
}
%></TABLE><%

con.close();
}
catch (Exception e)
{out.println(e.toString());}

%>
 
A

Arne Vajhøj

Vincent said:
I'm aware that this is a popular error:

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

An error occurred at line: 19 in the jsp file: /sample.jsp
Connection.Connect cannot be resolved to a type
16: try
17: {
18:
19: Connection.Connect con = new Connection.Connect();
20: ResultSet rs = con.execute(query);
21:
22: int numCols = rs.getMetaData().getColumnCount();


An error occurred at line: 19 in the jsp file: /sample.jsp
Connection.Connect cannot be resolved to a type
16: try
17: {
18:
19: Connection.Connect con = new Connection.Connect();
20: ResultSet rs = con.execute(query);
21:
22: int numCols = rs.getMetaData().getColumnCount();

From most solutions, it appears that I misplaced the java class.
However, I've placed the Connect class in WEB-INF/classes/Connection/,
which was what was recommended by most. This is the error I still
received.

I'm sorry, but is there something I'm doing wrong?

So you have a:

WEB-INF/classes/Connection/Connect.class

and Connect.java has:

package Connection;

in the top?

Arne
 
V

Vincent Ly

Yes, the Connect.java has package Connection at the top.

You answered a question similar to this in the past; I've reviewed the
response you've made to that question and made the changes
accordingly. But, the problem still persists.

Thanks for your help.
 
L

Lew

Please do not top-post (corrected).

Vincent said:
Yes, the Connect.java has package Connection at the top.

That partially (by which I mean incompletely) answers his second question, but
what was the answer to his first question?
 
V

Vincent Ly

Please do not top-post (corrected).



That partially (by which I mean incompletely) answers his second question, but
what was the answer to his first question?

Sorry, I was under the impression that he was making a statement.

To clarify:

I have a Connect class in WEB-INF/classes/Connection/; in other words:

WEB-INF/classes/Connection/Connect.class exists.

And in Connect.java used to create the Connect.class, I have "package
Connection;" at the top.

Would it be helpful to see the actual class?

What does top-post mean? Replying on top of a quote?
 
A

Arne Vajhøj

Vincent said:
Sorry, I was under the impression that he was making a statement.

To clarify:

I have a Connect class in WEB-INF/classes/Connection/; in other words:

WEB-INF/classes/Connection/Connect.class exists.

And in Connect.java used to create the Connect.class, I have "package
Connection;" at the top.

And you restarted Tomcat after putting the class there?
Would it be helpful to see the actual class?

Most likely not.

But if the likely does not turn up solving the problem, then we may
need to look at the unlikely.
What does top-post mean? Replying on top of a quote?

Yes.

Arne
 
V

Vincent Ly

And you restarted Tomcat after putting the class there?


Most likely not.

But if the likely does not turn up solving the problem, then we may
need to look at the unlikely.


Yes.

Arne

Yes, I've tried restarting Tomcat. The error is still there.

/usr/share/tomcat6/webapps/dbproject/WEB-INF/classes/Connection

is where I'm storing the Connect.class file.

It doesn't appear to have any mistakes regarding capitalization or
anything.

Thank you for your time.
 
T

Tim Slattery

Vincent Ly said:
I'm aware that this is a popular error:

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

An error occurred at line: 19 in the jsp file: /sample.jsp
Connection.Connect cannot be resolved to a type
16: try
17: {
18:
19: Connection.Connect con = new Connection.Connect();


Maybe it's just me, but I don't understand this. The "new" command is
used with a class name, it invokes the proper constructor and creates
a new instance of the class. Using "new" with a method makes no sense
to me. Also, "Connection" is an interface, and has no constructor. To
get one you use the getConnection static method of the DriverManager
class.

Looks to me like it should be something like:
Connection con =
DriverManager.getConnection(<arguments here>);
 
L

Lew

Tim said:
Maybe it's just me, but I don't understand this. The "new" command is
used with a class name, it invokes the proper constructor and creates
a new instance of the class. Using "new" with a method makes no sense

Since he's using it with a constructor that isn't a problem. You've been
thrown off by the OP's unconventional capitalizing a package name.
to me. Also, "Connection" is an interface, and has no constructor. To

No, 'Connection' is not an interface here.
get one you use the getConnection static method of the DriverManager
class.

Looks to me like it should be something like:
Connection con =
DriverManager.getConnection(<arguments here>);

This would not work since in the OP's code 'Connection' is a package.

To the OP: By convention, package names are spelled in all lower-case,
particularly when there is a risk of confusion with a well-known type. Also,
your code might have been clearer with an 'import' and simple class names.
Also, naming a type with a verb is rather confusing; verbs are conventionally
used as method names and nouns or adjectives are used as type names. Tim's
confusion is understandable, for all that it could have been avoided with
very, very careful reading of your post.
 
V

Vincent Ly

Since he's using it with a constructor that isn't a problem.  You've been
thrown off by the OP's unconventional capitalizing a package name.


No, 'Connection' is not an interface here.



This would not work since in the OP's code 'Connection' is a package.

To the OP:  By convention, package names are spelled in all lower-case,
particularly when there is a risk of confusion with a well-known type.  Also,
your code might have been clearer with an 'import' and simple class names..
Also, naming a type with a verb is rather confusing; verbs are conventionally
used as method names and nouns or adjectives are used as type names.  Tim's
confusion is understandable, for all that it could have been avoided with
very, very careful reading of your post.

I see, that makes sense. I'll try reorganizing the code a bit then.

Thanks.
 
V

Vincent Ly

I see, that makes sense.  I'll try reorganizing the code a bit then.

Thanks.

So, I changed Connect.java to DBConnection.java and removed the
package. I moved the DBConnection.class to
/usr/share/tomcat6/webapps/dbproject/WEB-INF/classes/DBConnection.class

I also changed the
Connection.Connect con = new Connection.Connect(); to
DBConnection con = new DBConnection();

Am I doing it correctly so far? I'm still getting the same error. Is
there a scenario where that error may show without the directory tree
or syntax being the problem?
 
V

Vincent Ly

So, I changed Connect.java to DBConnection.java and removed the
package.  I moved the DBConnection.class to


I also changed the


Am I doing it correctly so far?  I'm still getting the same error.  Is
there a scenario where that error may show without the directory tree
or syntax being the problem?

I seemed to have solved the problem.
Thanks Arne, Lew, and Tim for all your patience with me.

It's a bit mysterious, but it seems javac with the -d options works
better than mkdir.
 
L

Lew

You should assiduously avoid putting classes in the default (unnamed)
package.

All non-toy code should use packages.
 
A

Arne Vajhøj

Vincent said:
I seemed to have solved the problem.

Classes in default package is not good.

Especially not in JSP context !
It's a bit mysterious, but it seems javac with the -d options works
better than mkdir.

I fear that the problem will come back in either the same form
or a variation.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,991
Latest member
Josephnag

Latest Threads

Top