Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP REFUSES to notice my CLASSES 2

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

My problem is I can’t get my JSP to recognize my homemade classes. Sorry in advance for the length of this post, but need to show all info.

I’m a relative newbie to Java servlets, but here’s my 2 main class codes:

Code for User class

public class User{
private String firstName;
private String lastName;
private String emailAddress;

//constructors
public User(){}

public User(String first, String last, String email){
firstName = first;
lastName = last;
emailAddress = email;
}

//methods
public void setFirstName(String f){
firstName = f;
}

public String getFirstName(){
return firstName;
}

public void setLastName(String l){
lastName = l;
}

public String getLastName(){
return lastName;
}

public void setEmailAddress(String e){
emailAddress = e;
}

public String getEmailAddress(){
return emailAddress;
}





Code for UserIO class


import java.io.*;

public class UserIO{
public synchronized static void addRecord(User user, String filename)
throws IOException{
PrintWriter out = new PrintWriter(new FileWriter(filename,true));

out.println(user.getEmailAddress() + "|" + user.getFirstName() + "|"
+ user.getLastName());

out.close();

}
}



****************************
My Environment Setup
****************************

- Running Tomcat 4.1 with this embedded directory structure:
o \Tomcat 4.1
\webapps
\JSP
\WEB-INF
\classes User.java and UserIO.java saved here


- Running Java 2 SDK 1.4.2_02
- Environment’s System Variables:
o PATH includes c:\j2sdk1.4.2_02\bin and C:\Tomcat 4.1
o CATALINA_HOME = C:\Tomcat 4.1
o CLASSPATH = %CATALINA_HOME%\common\lib\servlet.jar;%CATALINA_HOME%\webapps\JSP\WEB-INF\classes Also tried only one of these respective directories

****************************
Error While Compiling
****************************

C:\Tomcat 4.1\webapps\JSP\WEB-INF\classes> javac User.java

C:\Tomcat 4.1\webapps\JSP\WEB-INF\classes> javac UserIO.java


Both of these compile with no problem. Now I have User.class and UserIO.class.


****************************
Run the URL.
****************************

I run which collects a first/lastname and email and then calls this .JSP:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Chapter 4 - Email List Application (JSP + classes)</title>
</head>

<body>

<jsp:useBean id="user" scope="session" class="User" /> ?????? How do I import


<%
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String emailaddress = request.getParameter("emailaddress");

User user = new User(firstName,lastName,emailAddress);
UserIO.addRecord(user,"../webapps/JSP/UserEmail.txt");

%>


<h1> Thanks for joining our email list </h1>
<p> Here is the information that you entered:</p>

<table cellspacing=5 border=0>
<tr>
<td align="right">First Name:</td>
<td><%= user.getFirstName() %></td>
</tr>

<tr>
<td align="right">Last Name:</td>
<td><%= user.getLastName() %></td>
</tr>

<tr>
<td align="right">Email Address:</td>
<td><%= user.getEmailAddress() %></td>
</tr>
</table>

<p>To enter another email address, click on the Back <br> button in your browser
or the Return button shown <br> below. </p>

<form action="join_email_list.html" method="post">
<input type="submit" value="Return">
</form>


</body>
</html>



--------

Here’s the main error message I get


Cannot resolve symbol
Symbol: class User

Then it points to my code:
User user = new User(…..)

Another error is:

Cannot resolve symbol
Symbol: variable UserIO

Then it points to my code:
UserIO.addRecord….

Among other error messages I get.




How do I import my class? I've tried the <jsp:usebean> and also tried <% page import="User,UserIO" %> to no avail. Everyone says to have your compiled classes in a \classes folder and I do.

Thanks for all of your help.

Scripter73



Change Your Thinking, Change Your Life.
 
put the following line at the top of your JSP page.

<%@ page import="User,UserIO" %>

 
Thanks, byam for responding so quickly.

I tried your suggestion. Here's what I have in my show_email_entry.jsp page:


<%@ page import="User,UserIO" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Chapter 4 - Email List Application (JSP + classes)</title>
</head>

<body>

<%
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String emailaddress = request.getParameter("emailaddress");

User user = new User(firstName,lastName,emailAddress);
UserIO.addRecord(user,"../webapps/hJSP/UserEmail.txt");

%>





I had also previously put this within the <body> tag.

Here's the error message I get.


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

An error occurred at line: -1 in the jsp file: null

Generated servlet error:
[javac] Compiling 1 source file

C:\Tomcat 4.1\work\Standalone\localhost\hJSP\show_email_entry_jsp.java:7: '.' expected
import User;
^
C:\Tomcat 4.1\work\Standalone\localhost\hJSP\show_email_entry_jsp.java:8: '.' expected
import UserIO;
^
2 errors




Thanks for any help you can provide.

scripter73


Change Your Thinking, Change Your Life.
 
My guess is you are using Java 1.4. Since Java 1.4, you can no longer import a class not under a package.


you need to create a directory to hold the User and UserIO classes. Let say you create a directory call myPackage. i.e. the two java file will be in

%CATALINA_HOME%\webapps\JSP\WEB-INF\classes\myPackage\User.java
%CATALINA_HOME%\webapps\JSP\WEB-INF\classes\myPackage\UserIO.java

for each of the java class in the myPackage, add the following line at the begining of the class file
Code:
package myPackage

you can then compile it by
javac %CATALINA_HOME%\webapps\JSP\WEB-INF\classes\myPackage\*.java

And in your JSP, the import would be

Code:
<%@ page import="myPackage.User, myPackage.UserIO" %>
 
Hi byam,

This worked!!!!! I had never created a package before. I thought there was some other type of process. I didn't know all you had to do was create a directory.

I also like the idea of compiling all of the .java code at once? Is that necessary for a package?

Thanks again for breaking down the process into very understandable terms.

Take care,
scripter73


Change Your Thinking, Change Your Life.
 
Hey, byam,

One more question. As I become more familiar with this process (I'm coming from an ASP background), let me ask you this.

What would the classes User and UserIO be considered as? Java Beans? Servlets?

I like the idea of taking another task and writing a class for it, I'm just not sure what I should say I'm creating.

Thanks,
scripter73


Change Your Thinking, Change Your Life.
 
User and UserIO are not servlet. JSP describe how to constructs a servlet. JSP container compiles JSP into a servlet. You can find the generated servlet (.java) for your JSP in %CATALINA_HOME%\work\Standalone\localhost\JSP\...

The User class can be considered as a Java bean. It has the setter and getter methods for it properties. UserIO class is not a Java bean. UserIO is just a component providing a interface to add user record. It has no getter not setter method, and has no properties.
 
Thanks for clearing that up a bit.




Change Your Thinking, Change Your Life.
 
byam and this site's search engine come through again!

Thanks!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top