3.Arithmetic operations implemented in session bean


MySessionBean.java :-
package com.ejb;
import javax.ejb.Stateless;
@Stateless
public class MySessionBean implements MySessionBeanRemote
{
public int add(int a, int b) {
return a+b;
}
public int sub(int a, int b) {
return a-b;
}
public int mul(int a, int b) {
return a*b;
}
public int div(int a, int b) {
return a/b;
}
}

MySessionBeanRemote.java :-
package com.ejb;
import javax.ejb.Remote;
@Remote
public interface MySessionBeanRemote
{
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
int div(int a, int b);
}

index.jsp :-
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Page</title>
</head>
<body>
<form action=”test” method=”post”>
<table border=”1″>
<thead>
<tr>
<th colspan=”2″>Calculator</th>
</tr>
</thead>
<tbody>
<tr>
<td>Enter 1st number </td>
<td><input type=”text” name=”t1″ value=”” /></td>
</tr>
<tr>
<td>Enter 2nd number </td>
<td><input type=”text” name=”t2″ value=”” /></td>
</tr>
<tr>
<td colspan=”2″>
<input type=”radio” name=”rdogrp” value=”Add” checked=”checked” />Add
<input type=”radio” name=”rdogrp” value=”Subtract” />Subtract
<input type=”radio” name=”rdogrp” value=”Multiply” />Multiply
<input type=”radio” name=”rdogrp” value=”Divide” />Divide </td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<input type=”submit” value=”Click to see the result” name=”b1″ align=”center” /> </td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

test.java :-
import com.ejb.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class test extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8”);
PrintWriter out = response.getWriter();
try {
MySessionBean bean = new MySessionBean();
int a = Integer.parseInt(request.getParameter(“t1”));
int b = Integer.parseInt(request.getParameter(“t2”));
String s = request.getParameter(“rdogrp”);
out.println(“<h3>”);
if(s.equals(“Add”))
out.println(“Result :- ” + bean.add(a, b));
else if(s.equals(“Subtract”))
out.println(“Result :- ” + bean.sub(a, b));
else if(s.equals(“Multiply”))
out.println(“Result :- ” + bean.mul(a, b));
else if(s.equals(“Divide”))
out.println(“Result :- ” + bean.div(a, b));
out.println(“</h3>”);
} finally {
out.close();
}
}
}