Select * From World where type="computing";
Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0
This tutorial is available in pdf book : Step By Step Create Simple Calculator using Eclipse Galileo & EJB 3.0
Remark:
I received many emails that talk about the error of “Firstcalc not bound” the problem that if you didn’t referenced the EJB project in the web application your server can not know the calcImpl class for this reason it shows this error.
To resolve this error just try to remove @Stateless(mappedName = “Firstcalc”) and replace it by @stateless,in the naming function in the servlet replace calc cl = (calc) ctx.lookup(”Firstcalc”); by cl=(calc) ctx.lookup(”calcImpl/local”);
I used the bounding without a mapped name of the calcImpl class, it’s Work correctly(tested with jboss server).
We will use in this tutorial:
- Server Application Using EJB:
- Configuration JBoss Server:
Server –> Runtime Environments.
not the Content of the directory! :
to this section:
until finish
then if jdk is not checked check it ,or add it if it doesn’t exist:
EJB Application.
- EJB Application:
@Stateless(mappedName=”Firstcalc”)
public class calcImpl implements calc {
public float sum(float a,float b)
{ return a+b; }
public float mult(float a,float b)
{ return a*b; }
public float minus(float a,float b)
{ return a-b; }
public float div(float a,float b) {
try {
return a/b;
}catch(Exception e){
System.out.println(”Error:devision by 0!!”);
return 0;
}
}
}
- Web Client Application:
<h2> <b> Hello World To The Simple Calculator </b> </h2>
<% float a=2,b=1;
if (request.getAttribute(”a”)!=null)
a=Float.parseFloat(request.getAttribute(”a”).toString());
if( request.getAttribute(”b”)!=null)
b=Float.parseFloat(request.getAttribute(”b”).toString()); %>
<form method=”post” action=”calcServlet”>
<b>Number 1:</b><input type=’text’ name=’n1′ value=”<%=a%>” /> <br/>
<b>Number 2:</b><input type=’text’ name=’n2′ value=”<%=b%>” /> <br/>
<u><b>Options:</b></u> <br/>
<ul>
<li><b>+</b><input type=’radio’ name=”oper” value=’+’ checked /></li>
<li><b> -</b><input type=’radio’ name=”oper” value=’-’ /></li>
<li><b>*</b><input type=’radio’ name=”oper” value=’*’ /></li>
<li> <b>/</b><input type=’radio’ name=”oper” value=’/’ /></li> </ul>
<b>——————————————-</b> <br/>
<input type=”submit” value=”Executer” /> </form>
<font color=’blue’><b>Result is: </b> <%=session.getAttribute(”result”)%> </font> <br/>
<font color=’red’ >Error: <%=session.getAttribute(”error”)%></font>
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
HttpSession session=request.getSession(true);
RequestDispatcher rd=this.getServletContext().getRequestDispatcher(”/index.jsp”);
float a=Float.parseFloat(request.getParameter(”n1″));
float b=Float.parseFloat(request.getParameter(”n2″));
char oper=request.getParameter(”oper”).charAt(0);
float result=0;
try {
Context ctx=new InitialContext();
// call the calcImpl class of the SimpleCalculator EJB with the mappedName
calc cl=(calc) ctx.lookup(”Firstcalc”);
switch(oper)
{
case ‘+’: result=cl.sum(a, b); break;
case ‘-’: result =cl.minus(a, b); break;
case ‘*’: result =cl.mult(a, b); break;
}
session.setAttribute(”result”,result); request.setAttribute(”a”, a); request.setAttribute(”b”, b);
}catch(NamingException e){
session.setAttribute(”erreur: “,e.getMessage());
}
rd.forward(request,response);
to let the the web client application to know the calc interface we must configure the build path of the project in adding the first project SimpleCalculator :
in the project section click add and select SimpleCalculator:
now add this import to your servlet class :
import ejb.calc;
let’s deploy the two project ,firstly play the JBoss Server in Server section:
deploying the EJB Project :
with the same way deploy SimpleCalculatorWeb:
if you have any remarks or questions dont hesitate to post it.



















about 1 year ago
I am getting an “javax.naming.NameNotFoundException: FirstCalc not bound” exception…….
about 1 year ago
I think that you didn’t referenced the EJB project in the web application your server doesn’t know the calcImpl class and for that it shows this error , ok don’t worry to resolve this error just try to remove @Stateless(mappedName = “Firstcalc”) and replace it by @stateless and in the naming function in the servlet replace calc cl = (calc) ctx.lookup(”Firstcalc”); by cl=(calc) ctx.lookup(”calcImpl/local”);
To explain what I made, I’m just used the bounding without a mapped name of the calcImpl class.
good luck ^^
about 1 year ago
it a gr8 tutorial but I am keep getting the next error
21:35:16,765 ERROR [[CalcServlet]] Servlet.service() for servlet CalcServlet threw exception
java.lang.NullPointerException
about 1 year ago
To be more specific
exception
and line 37 in CalcServlet is :
float a=Float.parseFloat(request.getParameter(”n1″));
java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
java.lang.Float.parseFloat(Unknown Source)
web.CalcServlet.doGet(CalcServlet.java:37)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
about 1 year ago
I think that you had an error of casting, the cause of error is you tried to enter an invalid number which can not be converted to float type. try a valid number (1,2,3….)? if it doesn’t work , try to pass directly a number in the code for testing purpose :
float a=Float.parseFloat(request.getParameter(”4″)); //just for test .
if it works then you had a problem of converting from string to float.
about 1 year ago
I’m very new to EJB and after following the guide using Eclipse Galileo SR2 + JBoss 5.1.0 GA, I have the following error when I perform the final step of deployment.
The requested resource (/SimpleCalculatorWeb/calcServlet) is not available.
I check out \jboss-5.1.0.GA\server\default\deploy\ and I found SimpleCalculator.jar and SimpleCalculatorWeb.war being created there after deployment. Is there anything else I’m missing? Please advise! Thanks!
about 1 year ago
Please ignore my previous message. I have found the problem. Turn out I should use http://localhost:8080/SimpleCalculatorWeb/calServlet instead of http://localhost:8080/SimpleCalculatorWeb/calcServlet
about 1 year ago
1. java.lang.NullPointerException from Lusio
>> fix by edit index.jsp
Use this correct code:
Number 1:<input type='text' name="n1" value="” />
Number 2:<input type='text' name="n2" value="” />
( comment: name should use ” , Should not use ‘)
2. I found another error code in “calcServlet.java” code
Replace:
session.setAttribute(”erreur: “,e.getMessage());
By:
session.setAttribute(”error”,e.getMessage());
about 1 year ago
However Thank so much for your share knowledge.
This Example, That grate start lean the EJB \(^.^)/
because I try to find how to implement that topic
about 1 year ago
still the same problem with the null pointer ..any advice