21 June 2008

Simple JSP - Servlet - Session Bean

This example shows how is simple structure of JSP, Servlet, Session Bean and interaction among them.


-------------------------
// index.jsp
// here is simple html structure
form name="convert" action="convert" method="post"
input name="money" type="text"
input value="Submit" type="submit"
// close codes with simple html structure

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

// servlet convert.class

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ejb.EJB;
import ejb.converterlocal;

public class convert extends HttpServlet {
@EJB
private converterLocal ConverterBean;
protected void processRequest (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException

Printwriter out = response.getWriter();
String amount = request.getParameter("money");
Double amountMoney = new Double(amount);
double result = converterBean.euroToDollar(amountMoney.doubleValue());
out.println(result);
out.close;
}

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

// EJB session bean - ConverterLocal

package ejb;

import javax.ejb.Local;

@Local
public interface converterLocal {
double euroToDollar (double amount);
}

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

//EJB session bean - ConverterBean

package ejb;

import javax.ejb.stateless;

@Stateless
public class converterBean implements ejb.ConverterLocal {
double euroRate = 1,5;

public double euroToDollar (double amount){
return amount*euroRate;
}
}

These codes are not error checked so it is possible to face some small errors but all these errors are typing errors and easy to find. General srtucture of jsp-servlet-ejb is working.

No comments: