• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why tomcat can't recognise the package?

 
Janet Yap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I have create a java bean in a folder called mybeans located at:
C:\Tomcat\classes\mybeans
I can call this bean via jsp without any problem. These are the codes in the jsp file:
*****************************************************
<%@ page language="java" contentType="text/html" %>
<html><head><title>Using Simple Bean</title></head><body>
<%-- create an instance of the SimpleBean --%>
<jsp:useBean id="msg" class="mybeans.SimpleBean" />
<jsp:setProperty name="msg" property="message1" value="Saying Hello" />
<jsp:setProperty name="msg" property="message2" value="with a JavaBean" />
<b><font color="red"><jsp:getProperty name="msg" property="message1" /></font></b>
<i><font color="red"><jsp:getProperty name="msg" property="message2" /></font></i>
</body></html>
**************************************************************************

However, when i try to use this bean in a servlet, i have problem to compile the servlet. The following is the error i get when i compile it:
**********************************************
Message.java:4: package mybeans does not exist
import mybeans.SimpleBean;
^
Message.java:13: cannot resolve symbol
symbol : class SimpleBean
location: class Message
SimpleBean t= new SimpleBean();
^
Message.java:13: cannot resolve symbol
symbol : class SimpleBean
location: class Message
SimpleBean t= new SimpleBean();
^
Message.java:25: cannot resolve symbol
symbol : variable t
location: class Message
String a=t.getMessage1();
^
Message.java:26: cannot resolve symbol
symbol : variable t
location: class Message
String b=t.getMessage2();
^
5 errors
**************************************************
The codes in the java bean are as follows:
package mybeans;
public class SimpleBean
{
private String message1;
private String message2;

public void setMessage1(String input)
{
message1=input;
}
public String getMessage1()
{
return message1;
}
public void setMessage2(String input)
{
message2=input;
}
public String getMessage2()
{
return message2;
}
}
************************************************************
whereas, the java servlet has the following codes:
***********************************************************
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import mybeans.SimpleBean;
public class Message extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
SimpleBean t= new SimpleBean();
t.setMessage1("Hello,");
t.setMessage1("SP");

}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
PrintWriter out = res.getWriter();
String a=t.getMessage1();
String b=t.getMessage2();
out.println("<html>");
out.println("<body>");
out.print("+a+");
out.println("+b+");
out.println("</body>");
out.println("</html>");
}
}
**********************************************************
May I know how this problem can be solved?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem may be solved by following the correct convention for where class files go.
have create a java bean in a folder called mybeans located at:
C:\Tomcat\classes\mybeans

Is NOT it. How in the world did you come up with that?
Your Tomcat installation comes with extensive documentation. Try the ClassLoader How-To link in the tomcat-docs for complete information on where classes should go - or simply look at the way the examples are organized.
Bill
 
Janet Yap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for yor reply
the problem is solved after i have placed the package under C:\Tomcat\webapps\WEB-INF\class directory
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue isn't where you put the class. The issue is having accessibility to the class. If I am was compiling from the command line then it wouldn't matter how well I had set up my Tomcat libraries if my classpath didn't point to the class. My guess is that you are using an IDE and once you placed the class in the correct directory the IDE found the class and was able to compile.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic