[jboss-user] [EJB/JBoss] - The requested resource (/NewsApp-war/ListNews) is not availa

badhikary81 do-not-reply at jboss.com
Fri Apr 25 02:36:35 EDT 2008


Hi!
I am Basudev.
I am trying to run a simple enterprise application. My application's name is 'NewsApp' and I have used jboss-5.0.0.Beta4 , jdk1.6.0 and Netbean IDE 6.1.When I run my application then server throw the requested resource (/NewsApp-war/ListNews) is not available.

Here is my code
1.ListNews.java
package web;

import ejb.NewsEntity;
import ejb.NewsEntityFacadeLocal;
import java.io.*;
//import java.net.*;

import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

public class ListNews extends HttpServlet {
    @EJB
    private NewsEntityFacadeLocal newsEntityFacade;
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            out.println("");
            out.println("");
            out.println("Servlet ListNews");  
            out.println("");
            out.println("");
            out.println("<h1>Servlet ListNews at " + request.getContextPath () + "</h1>");
           //newsEntityFacade = (NewsEntityFacadeLocal) lookupNewsEntityFacade();
            List news = newsEntityFacade.findAll();
            for (Iterator it = news.iterator(); it.hasNext();) {
            NewsEntity elem = (NewsEntity) it.next();
            out.println(" "+elem.getTitle()+" ");
            out.println(elem.getBody()+" ");
}
            out.println("Add new message");

            out.println("");
            out.println("");
           
        } finally { 
            out.close();
        }
    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
   
    /*private ejb.NewsEntityFacadeLocal lookupNewsEntityFacade() {
    try {
       javax.naming.Context c = new javax.naming.InitialContext();
       return (ejb.NewsEntityFacadeLocal) c.lookup("NewsApp/NewsEntityFacade/local");
   }
   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);
    }
 }*/
    // </editor-fold>
}

postMessage.java
package web;

import ejb.NewsEntity;
import java.io.*;
//import java.net.*;

import java.util.Queue;
import javax.jms.Destination;
import javax.jms.JMSException;
//import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.MessageProducer;

  public class PostMessage extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP GET and POST 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");
        String title=request.getParameter("title");
        String body=request.getParameter("body");

       if ((title!=null) && (body!=null)) {

        Queue queue = null;
        QueueConnection connection = null;
        QueueSession session = null;
        MessageProducer messageProducer = null;
        try {

        InitialContext ctx = new InitialContext();
        queue = (Queue) ctx.lookup("queue/mdb");
        QueueConnectionFactory factory =
                (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
        connection = factory.createQueueConnection();
        session = connection.createQueueSession(false,
                QueueSession.AUTO_ACKNOWLEDGE);
        messageProducer = session.createProducer((Destination) queue);

        ObjectMessage message = session.createObjectMessage();
        // here we create a NewsEntity, that will be sent in JMS message
        NewsEntity e = new NewsEntity();
        e.setTitle(title);
        e.setBody(body);

        message.setObject(e);
        messageProducer.send(message);
        messageProducer.close();
        connection.close();
        response.sendRedirect("ListNews");

    } catch (JMSException ex) {
        ex.printStackTrace();
    } catch (NamingException ex) {
        ex.printStackTrace();
    }

}
        PrintWriter out = response.getWriter();
        try {
            
            out.println("");
            out.println("");
            out.println("Servlet PostMessage");  
            out.println("");
            out.println("");
            out.println("<h1>Servlet PostMessage at " + request.getContextPath () + "</h1>");
            out.println("");
            out.println("Title: ");
            out.println("Message: ");
            out.println("");
            out.println("");
            out.println("");
            out.println("");
               } finally { 
            out.close();
        }
    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
NewsEntity.java
package ejb;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class NewsEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String title;
    private String body;

    public void setId(Long id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof NewsEntity)) {
            return false;
        }
        NewsEntity other = (NewsEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ejb.NewsEntity[id=" + id + "]";
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

}
NewMessageBean.java
package ejb;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@MessageDriven(mappedName = "jms/NewMessage", activationConfig =  {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/mdb")
    })
public class NewMessageBean implements MessageListener {
     @Resource
     private MessageDrivenContext mdc;
     @PersistenceContext
     private EntityManager em;
    
    public NewMessageBean() {
    }

    public void onMessage(Message message) {
        ObjectMessage msg = null;
    try {
        if (message instanceof ObjectMessage) {
            msg = (ObjectMessage) message;
            NewsEntity e = (NewsEntity) msg.getObject();
            save(e);
        }
    } catch (JMSException e) {
        e.printStackTrace();
        mdc.setRollbackOnly();
    } catch (Throwable te) {
        te.printStackTrace();
    }
    }

    public void save(Object object) {
        em.persist(object);
    }
    
}
NewsEntityFacade.java
package ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Administrator
 */
@Stateless
public class NewsEntityFacade implements NewsEntityFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(NewsEntity newsEntity) {
        em.persist(newsEntity);
    }

    public void edit(NewsEntity newsEntity) {
        em.merge(newsEntity);
    }

    public void remove(NewsEntity newsEntity) {
        em.remove(em.merge(newsEntity));
    }

    public NewsEntity find(Object id) {
        return em.find(ejb.NewsEntity.class, id);
    }

    public List findAll() {
        return em.createQuery("select object(o) from NewsEntity as o").getResultList();
    }

}

please Sir I need help.Where is my fault?How can solve it? 


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4146715#4146715

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4146715



More information about the jboss-user mailing list