• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why tomcat can't recognise the package?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic