stateless ejb and html file

gudii9
gudii9 used Ask the Experts™
on
hi,

I build one stateless session bean example with presentation tier html inddex page and web tier servlet within ejb web project then business tier in the ejb project


<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="ConverterServlet" method="POST">
              <input type="number" name="value"></br>
              <select name="type">
                    <option value="C2F">celcius to fahrenheit</option>
                    <option value="F2C">fahrenheit  to celcius</option>
              </select></br>
              <input type="submit" value="Convert">
        </form>
            
    </body>
</html>

Open in new window


do i not need to add close tags for input html element
<input type="submit" value="Convert">

also do i not need to ad begin tag for </br>

please advise

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import bean.ConverterBeanLocal;
import bean.ConverterBeanLocal.ConvertType;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author saiganesh
 */
@WebServlet(name = "ConverterServlet", urlPatterns = {"/ConverterServlet"})
public class ConverterServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    @EJB
    ConverterBeanLocal converterBean;
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        double result = converterBean.calculate(Double.valueOf(request.getParameter("value")), ConvertType.valueOf(request.getParameter("type")));
        
        
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ConverterServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ConverterServlet at " + result + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <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>

}

Open in new window



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import javax.ejb.Stateless;

/**
 *
 * @author saiganesh
 */
@Stateless
public class ConverterBean implements ConverterBeanLocal {

    @Override
    public double calculate(double value, ConvertType type) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        
        if(type==ConvertType.C2F){
            return value*2+30;
        }
        return (value-32)/1.8;
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}

Open in new window



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import javax.ejb.Local;

/**
 *
 * @author saiganesh
 */
@Local
public interface ConverterBeanLocal {
    public enum ConvertType
    {
        C2F,F2C
    }
    public double calculate(double value, ConvertType type);
    
}

Open in new window

indexPAge.png
result.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018
Commented:
do i not need to add close tags for input html element
<input type="submit" value="Convert">
You need to. The correct formatting is
<input type="submit" value="Convert"/>

Open in new window

, but most browsers are forgiving, so you can get away with it.

also do i not need to ad begin tag for </br>
No.
<br/>

Open in new window

is there on its own.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial