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?