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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tomcat and jsp and classes

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
EU
Hi,

im new to tomcat and jsp. ive created a class and im tring to get my jsp files to use this class.

as far as i can see their's no problem with the code but ive givin it below just in case.

ive inserted the classes into the following folders with no success (and included these in the classpath in environment variables)

C:\Tomcat 5.0\webapps\ROOT
C:\Tomcat 5.0\webapps\ROOT\cls
C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes

this is the error that gets returned after i click the submit button.

Code:
description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

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

An error occurred at line: 1 in the jsp file: /SaveNewName.jsp
Generated servlet error:
C:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\SaveNewName_jsp.java:42: cannot find symbol
symbol  : class userInfo
location: class org.apache.jsp.SaveNewName_jsp
      userInfo user = null;
      ^

here is the code for all the files
Code:
///// GetName.jsp

<HTML>
	<BODY>
		
		<FORM METHOD=POST ACTION="SaveNewName.jsp">
		
			What's your name?           <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
			What's your email Address?  <INPUT TYPE=TEXT NAME=useremail SIZE=50> <BR>
			What's your age?            <INPUT TYPE=TEXT NAME=userAge SIZE = 2><BR>
			
			<P><INPUT TYPE=SUBMIT>
		
		</FORM>
	</BODY>
</HTML>


///// SaveNewName.jsp
<jsp:useBean id="user" class="userInfo" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<HTML>
	<BODY>

		<A HREF="NewNextPage.jsp">Continue</A>

	</BODY>
</HTML>


//////// NewNextPage.jsp

<jsp:useBean id = "user" class "userInfo" scope ="session"/>

<HTML>
	<BODY>
		You entered  <BR>
		<BR>
		
		Name: 	<%= user.getUserName() %><BR>
		Email: 	<%= user.getUserEmail() %><BR>
		Age:	<%= user.getAge() %><BR>
	</BODY>
</HTML>

Cheers for the help

dex
 
Classes must be in a package in Tomcat now.

So your class would look like :

Code:
package com.acme;
public class UserInfo {
 ...
}

which you would put in this dir :

C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\com\acme

and your JSP would look like this :

Code:
jsp:useBean id="user" class="com.acme.UserInfo" scope="session"/>

--------------------------------------------------
Free Database Connection Pooling Software
 
cheers man that did the class working.

ive a bit of a problem with the output of the data though. this is the result of the above code.

You entered in the following information

Name: null
Age: 56
Email: null

it will store the age which is an int, but it wont do anything with the strings.

this is the code for the class, there arn’t any constructors because there were none in the sample code i found.

Code:
package com.test;

public class userInfo
{
	String strUserName;
	String strUserEmail;
	int intUserAge;
	
	public void setUserName (String strName)
	{
		strUserName = strName;
	}
	
	public void setUserEmail (String strEmail)
	{
		strUserEmail = strEmail;
	}
	
	public void setUserAge (int intAge)
	{
		intUserAge = intAge;
	}
	
	public String getUserName() { return strUserName;}
	
	public String getUserEmail() { return strUserEmail; }
	
	public int getUserAge() { return intUserAge; }
}

ive changed the code so that the strUserName = “hello” in the setUserName() method. This doesn’t seem to make any difference.

Ive also changed the return values in the ‘get’ methods with absolutely no change. After each re-compile I restart the tomcat5 server.

The only thing that will change is the age, and that’s only when you input it on the GetName.jsp file.

I'm at a loss as to what is going on with this.

Thanks for your help

dex
 
i figured out what i did wrong.

i needed to change the variables username and useremail below to userName, userEmail. the lower case was causing the problem.

What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>

What's your email Address? <INPUT TYPE=TEXT NAME=useremail SIZE=50> <BR>

What's your age? <INPUT TYPE=TEXT NAME=userAge SIZE = 2><BR>

 
You bean class really should look like this if you are following the JSP/JavaBean standard :

Code:
package com.test;

public class UserInfoBean {
    private String userName;
    private String userEmail;
    private int userAge;
    
    public void setUserName (String userName) {
        this.userName = userName;
    }
    
    public String getUserName() {
    	return userName;
    }
    
    public void setUserEmail (String userEmail) {
       this.userEmail = userEmail;
    }
    
    public String getUserEmail() { 
    	return userEmail; 
    }    
    
    public void setUserAge (int userAge) {
        this.userAge = userAge;
    }
    
    public int getUserAge() { 
    	return userAge; 
    }
    

}


Which means your input form would look like this :

Code:
<form method="POST" action="SaveNewName.jsp">
<table>
   <tr>
   	<td>What's your name ? </td>
   	<td><input type="text" name="userName" size="20/></td>
   </tr>
    <tr>
   	<td>What's your email Address ?</td>
   	<td><input type="text" name="userEmail" size="50/></td>
   </tr>
   <tr>
   	<td>What's your age ? </td>
   	<td><input type="text" name="userAge" size="2/></td>
   </tr>   
</table>
</form>



--------------------------------------------------
Free Database Connection Pooling Software
 
cheers for that advice, its always best to keep to the standards.

do you know if its possible to use swing and jsp together? the impression im getting off a couple of sites is that you cant use them together.

thanks again for your help

dex
 
You cannot really use Swing & JSP together, for the fundamental reason the JSP executes on the server, whereas Swing must execute on the client.

The closest you can get is using Swing in Applets, which are then embedded into html pages. Ofcourse you may embed an applet tag in a JSP page, but I don;t really count that as JSP-Swing interaction because the applet would not kick into life until the JSP has outputted the final HTML to the client.

--------------------------------------------------
Free Database Connection Pooling Software
 
thats good to know because what im doing is getting the user to add a few pictures to the applet, then these are sent to the server for various checks and then the result is returned.

i presume with the swing in the applets you can get a harddrive browser to pop up and then add the files to the applet?
 
the default for applets is that you may not interact with the client's file system. But you can get around this by signing the applet with a certificate, and getting the user to accept your certificate.

--------------------------------------------------
Free Database Connection Pooling Software
 
You can always use html to upload the files and made that checks on the server side.

Cheers.

Dian
 
ive made a template using the html. ive got an image place holder. i cant seem to get the name of the file, that i choose using the file browser to be passed into the src= part of the place holder.

Code:
<input name="imgGifFile" type="file"  ACCEPT="gif" size="100"  value ="imgName">

<img src="imgName" alt="Email Footer" name="EmailFooter" width="114" height="114" align="middle">

ive tried using value and the name of the input but nothing works.
would i require java script at this stage or php or something else.

thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top