Stateless Session Bean(EJB2.X)

Select File  New Project… and then choose the Java Web category and Web Application project:

Right-click the Converter project and select New  Session Bean. Specify an EJB name, package, and choose to create a stateless session

Click Finish.

Expand the Enterprise Beans under the project and right click on ConverterBean and choose Add  Business Method.

In this example from the Mastering Enterprise JavaBeans, specify the name as cToF and a return type of String. If the method required parameters, you can add them at the same time by clicking Add…:

Right-click the Converter project and select New Servlet. Specify an Servlet name, package.

After coding, click run.

ConverterServlet.java

package converter.web;

import java.io.IOException;

import java.io.PrintWriter;

import javax.ejb.EJB;

import javax.servlet.annotation.WebServlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import converter.ejb.ConverterBean;

@WebServlet

public class ConverterServlet extends HttpServlet {

@EJB

ConverterBean converter;

protected void processRequest(

HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

// Output the results

out.println("<html>");

out.println("<head>");

out.println("<title>Temperature Converter</title>");

out.println("</head>");

out.println("<body>");

out.println(

"<h1>Temperature Converter </h1>");

try {

String degree = request.getParameter("degree");

if ((degree != null) & (degree.length() > 0)) {

double d = Double.parseDouble(degree);

if (request.getParameter("C TO F") != null) {

String centigrade = converter.cToF(d);

out.println(

"<p>" + degree + " centigrade degrees are "

+ centigrade + " .</p>");

}

if (request.getParameter("F TO C") != null) {

String fahrenheit = converter.fToC(d);

out.println(

"<p>" + degree + " centigrade degrees are "

+ fahrenheit + " .</p>");

}

} else {

out.println("<p>Enter degree to convert:</p>");

out.println("<form method=\"get\">");

out.println(

"<p> <input type=\"text\" name=\"degree\" size=\"25\"</p>");

out.println("<br/>");

out.println(

"<input type=\"submit\" name=\"F TO C\" value=\"F TO C\">"

+ "<input type=\"submit\" name=\"C TO F\" value=\"C TO F\">");

out.println("</form>");

}

} finally {

out.println("</body>");

out.println("</html>");

out.close();

}

}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

/**

* Handles the HTTP <code>GET</code> method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doGet(

HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

/**

* Handles the HTTP <code>POST</code> method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doPost(

HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

/**

* Returns a short description of the servlet.

* @return a String containing servlet description

*/

@Override

public String getServletInfo() {

return "Short description";

} // </editor-fold>

}

ConverterBean.java

/*

* Copyright 2011 Oracle and/or its affiliates.

* All rights reserved. You may not modify, use,

* reproduce, or distribute this software except in

* compliance with the terms of the License at:

*

*/

package converter.ejb;

import java.math.BigDecimal;

import java.text.DecimalFormat;

import javax.ejb.Stateless;

/**

* This is the bean class for the ConverterBean enterprise bean.

* @author ian

*/

@Stateless

public class ConverterBean {

private DecimalFormat twoDigits = new DecimalFormat ("0.00");

public String cToF(double c) {

String result = twoDigits.format(((c*9.0/5.0 + 32)*100)/100.0);

return result;

}

public String fToC(double f) {

String result = twoDigits.format(((f-32)*5.0/9.0*100)/100.0);

return result;

}

}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

xmlns="

xmlns:xsi="

xsi:schemaLocation="

<servlet>

<servlet-name>ConverterServlet</servlet-name>

<servlet-class>converter.web.ConverterServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ConverterServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>