Meaningless Drivel is fun!*
The moose likes EJB and other Java EE Technologies and the fly likes @EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply locked New topic
Author

@EJB

Dura Hurtado
Ranch Hand

Joined: Feb 16, 2011
Posts: 120
Hi , I am using JBoss 5, and the code is this way:

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="get" action="M">

<input type="text" name="nombre">

<input type="submit" value="enviar">
</form>

<%String d=request.getAttribute("result").toString();

out.println(d);

%>


</body>
</html>

M.java(servlet)
------------------

Here a thing happends which i don´t understand, if I type:

(404 error when @EJB is executed in runtime)

package c;

import java.io.IOException;
import java.util.Properties;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ejb.Calc;


/**
* Servlet implementation class M
*/
public class M extends HttpServlet {


private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@EJB
//private Calc ejb;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

Context ctx = new InitialContext(properties);


Calc hello = null;

hello = (Calc) ctx.lookup("CalcImpl/local");

System.out.println(hello.getNombre());
} catch (NamingException e) {
e.printStackTrace();
}


String string=request.getParameter("nombre");

if(string.equals(null)){

}else{
System.out.println("----------------------------------else");

String nombre="n";//=ejb.getNombre();
request.setAttribute("result", nombre);
System.out.println("----------------------------------else"+request.getAttribute("result"));
}

RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");

System.out.println("----------------------------------dispatcher"+rd);
rd.include(request,response);
}




}


---------------------------------------

but if I type:

package c;

import java.io.IOException;
import java.util.Properties;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ejb.Calc;


/**
* Servlet implementation class M
*/
public class M extends HttpServlet {


private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");

Properties properties = new Properties();

properties.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs",
"=org.jboss.namingrg.jnp.interfaces");

properties.put("java.naming.provider.url", "localhost:1099");
try{
Context ctx = new InitialContext(properties); Calc hello = null;

hello = (Calc) ctx.lookup("CalcImpl/local");

System.out.println(hello.getNombre());
} catch (NamingException e) {
e.printStackTrace();
}


String string=request.getParameter("nombre");

if(string.equals(null)){

}else{
System.out.println("----------------------------------else");

String nombre="n";//=ejb.getNombre();
request.setAttribute("result", nombre);
System.out.println("----------------------------------else"+request.getAttribute("result"));
}

RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");

System.out.println("----------------------------------dispatcher"+rd);
rd.include(request,response);
}




}

Then it works properly, ¿Anybody knows why?. The last example has been copy from the net it works but I don´t understand what way it injects the EJB.

¿What way could I know what EJB container I am using in JBoss5?

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Duplicate of http://www.coderanch.com/t/541628/EJB-JEE/java/EJB;jsessionid=CC2B1352E519CC7EC60854E3096B6D3B

Dura, can you avoid posting the same question twice? Thanks. I'll lock this thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @EJB