Hi,
Here is the complete code for my bean and other nodes
1- CMP Bean created using Netbeans 5.0
import javax.ejb.*;
public abstract class ContentTypesBean implements EntityBean, ContentTypesLocalBusiness {
private EntityContext context;
// <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click on the + sign on the left to edit the code.">
// TODO Consider creating Transfer Object to encapsulate data
// TODO Review finder methods
/**
* @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
*/
public void setEntityContext(EntityContext aContext) {
context = aContext;
}
/**
* @see javax.ejb.EntityBean#ejbActivate()
*/
public void ejbActivate() {
}
/**
* @see javax.ejb.EntityBean#ejbPassivate()
*/
public void ejbPassivate() {
}
/**
* @see javax.ejb.EntityBean#ejbRemove()
*/
public void ejbRemove() {
}
/**
* @see javax.ejb.EntityBean#unsetEntityContext()
*/
public void unsetEntityContext() {
context = null;
}
/**
* @see javax.ejb.EntityBean#ejbLoad()
*/
public void ejbLoad() {
}
/**
* @see javax.ejb.EntityBean#ejbStore()
*/
public void ejbStore() {
}
// </editor-fold>
public abstract
String getName();
public abstract void setName(String name);
public abstract String getFileType();
public abstract void setFileType(String fileType);
public abstract String getDataType();
public abstract void setDataType(String dataType);
public abstract Integer getStatus();
public abstract void setStatus(Integer status);
public abstract java.lang.Integer getId();
public abstract void setId(java.lang.Integer id);
public Integer ejbCreate(Integer id, String name, String fileType, String dataType, Integer status) throws CreateException {
if (id == null) {
throw new CreateException("The field \"id\" must not be null");
}
// TODO add additional validation code, throw CreateException if data is not valid
setId(id);
setName(name);
setFileType(fileType);
setDataType(dataType);
setStatus(status);
return null;
}
public void ejbPostCreate(Integer id, String name, String fileType, String dataType, Integer status) {
// TODO populate relationships here if appropriate
}
}
2- Session Bean
import java.util.Iterator;
import javax.ejb.*;
public class ContentTypesSessionBean implements SessionBean, ContentTypesSessionRemoteBusiness, ContentTypesSessionLocalBusiness {
private SessionContext context;
// <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
// TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
// TODO Add business methods or web service operations
/**
* @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
*/
public void setSessionContext(SessionContext aContext) {
context = aContext;
}
/**
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() {
}
/**
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() {
}
/**
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() {
}
// </editor-fold>
/**
* See section 7.10.3 of the EJB 2.0 specification
* See section 7.11.3 of the EJB 2.1 specification
*/
public void ejbCreate() {
// TODO implement ejbCreate if necessary, acquire resources
// This method has access to the JNDI context so resource aquisition
// spanning all methods can be performed here such as home interfaces
// and data sources.
}
// Add business logic below. (Right-click in editor and choose
// "EJB Methods > Add Business Method" or "Web Service > Add Operation")
private rishi.ContentTypesLocalHome lookupContentTypesBean() {
try {
javax.naming.Context c = new javax.naming.InitialContext();
rishi.ContentTypesLocalHome rv = (rishi.ContentTypesLocalHome) c.lookup("java:comp/env/ejb/ContentTypesBean");
return rv;
}
catch(javax.naming.NamingException ne) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
throw new RuntimeException(ne);
}
}
public int createNewContentType(java.lang.Integer id, String name, String fileType, String dataType, java.lang.Integer status) throws rishi.MyWapException {
int newId=0;
try{
rishi.ContentTypesLocalHome contentType= lookupContentTypesBean();
contentType.create(id,name,fileType,dataType,status);
newId=1;
}catch(Exception ex){
newId=-1;
}
return newId;
}
public int findContentType(String paramVal, String paramName) throws rishi.MyWapException {
int contentType=0;
try{
java.util.Collection col;
if(paramName.equalsIgnoreCase("name"))
col=lookupContentTypesBean().findByName(paramVal);
else if(paramName.equalsIgnoreCase("fileType"))
col=lookupContentTypesBean().findByFileType(paramVal);
else if(paramName.equalsIgnoreCase("dataType"))
col=lookupContentTypesBean().findByDataType(paramVal);
else
col=lookupContentTypesBean().findByStatus(new Integer(Integer.parseInt(paramVal)));
if(col!=null && col.size()>0)
{
Iterator itr=col.iterator();
if(itr.hasNext())
contentType=((ContentTypesLocal)itr.next()).getId().intValue();
else
contentType=0;
}
else
contentType=0;
}catch(Exception ex){
contentType=-1;
}
return contentType;
}
}
3-
Java servlet invoking session bean
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CreateMyObject extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet CreateMyObject</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet CreateMyObject at " + request.getContextPath () + "</h1>");
String id=request.getParameter("id");
String name=request.getParameter("name");
String dataType=request.getParameter("datatype");
String fileType=request.getParameter("filetype");
short status=1;
int retrId=0;
try{
rishi.ContentTypesSessionLocal sessObj=lookupContentTypesSessionBean();
sessObj.createNewContentType(new Integer(id),name,fileType,dataType,new Integer(status));
retrId=sessObj.findContentType(name,"name");
}catch(Exception ex){
retrId=-2;
}
out.println("<BR>id:"+id);
out.println("<BR>name:"+name);
out.println("<BR>dataType:"+dataType);
out.println("<BR>fileType:"+fileType);
out.println("<BR>status:"+status);
out.println("<BR>retrId:"+retrId);
out.println("</body>");
out.println("</html>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
private rishi.ContentTypesSessionLocal lookupContentTypesSessionBean() {
try {
javax.naming.Context c = new javax.naming.InitialContext();
rishi.ContentTypesSessionLocalHome rv = (rishi.ContentTypesSessionLocalHome) c.lookup("java:comp/env/ejb/ContentTypesSessionBean");
return rv.create();
}
catch(javax.naming.NamingException ne) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
throw new RuntimeException(ne);
}
catch(javax.ejb.CreateException ce) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
throw new RuntimeException(ce);
}
}
}
Kindly advice if i am supposed to write code for writing data to database in CMP bean also and if it is then at which stage i shall do that.
Regs,
Rahi