jsp/bean/tomcat

Discussion in 'Java' started by Max, Jul 6, 2003.

  1. Max

    Max Guest

    I have a problem regarding a small application:
    in an HTML page, calcpage.html, when I click on the SUBMIT button, it is
    supposed to call a JSP page but nothing happens.
    The HTML page is located here:
    E:\jakarta-tomcat-4.1.24\webapps\ROOT\calcpage.html
    The JSP page is located here:
    E:\jakarta-tomcat-4.1.24\webapps\ROOT\calculate.jsp
    The bean is here :
    E:\jakarta-tomcat-4.1.24\webapps\ROOT\WEB-INF\classes\cbean\CalcBean.class

    What am i doing wrong ? thanks

    The URL is http://localhost:8080/calcpage.html

    Here is the code :

    CalcBean.java

    package cbean ; //1
    public class CalcBean {
    /**
    Calculator bean for Chapter 6
    */
    private int operand1 = 0 ; //2
    private int operand2 = 0 ;
    private double result = 0 ;
    private String operation = "" ;

    //No-arg constructor for bean....
    public CalcBean() { } //3

    //Get/Set methods follow
    public void setOperand1( int op1 ) { //4
    operand1 = op1 ;
    }
    public void setOperand2( int op2 ) {
    operand2 = op2 ;
    }
    public void setOperation( String oper ) {
    operation = oper ;
    }
    public void setResult( double aResult) {
    result = aResult ;
    }
    public int getOperand1() { //5
    return operand1 ;
    }
    public int getOperand2() {
    return operand2 ;
    }
    public String getOperation() {
    return operation;
    }

    public double getResult() {
    return performTheOperation() ; //6
    }
    /**
    Perform the operation.....generate the result
    */
    private double performTheOperation( ) {
    double aResult = 0.0 ;

    if ( operation.equals("+") )
    aResult = operand1 + operand2 ;
    else
    if ( operation.equals("-") )
    aResult = operand1 - operand2 ;
    else
    if ( operation.equals("*") )
    aResult = operand1 * operand2 ;
    else
    if ( operation.equals("/") )
    aResult = operand1 / operand2 ;

    return aResult ;
    }
    }

    calcpage.html

    <html>
    <head>
    <title>JSP Sample Page - Calculator Bean</title>
    </head>
    <body bgcolor="#dddddd">
    <center>
    <br>
    <h1>A Simple Calculator</h1>
    <!-- Here is the reference to the JSP that uses CalcBean -->
    <form name="calcform" action="calculate.jsp" method="POST" >

    <p>Enter Operand1 and Operand2 (Integers)
    <br>and Select an operation From the Pull Down
    <br>Menu, then Click <b>Calculate</b> to Continue
    <br>
    <hr width="50%">
    <table>
    <tr>
    <td><P>Enter Operand1:</td>
    <!-- Entered value to be CalcBean property called operand1 -->
    <td><input type="text" name="operand1" value="" width="25"></td>
    </tr>
    <tr>
    <td><P>Enter Operand2:</td>
    <!-- Entered value to be CalcBean property called operand2 -->
    <td><input type="text" name="operand2" value="" width="25"></td>
    </tr>
    <tr>
    <td><P>Select Operation:</td>
    <!-- Selected value to be CalcBean property called operation -->
    <td><select NAME="operation">
    <option> +
    <option> -
    <option> *
    <option> /
    </select>
    </td>
    <tr>
    <!-- Click here to invoke the JSP page coded in the FORM tag -->
    <td><input type="button" name="Calculate" value="Calculate"></td>
    </tr>
    </table>
    <hr width="50%">
    </center>

    </form>
    </body>
    </html>

    calculate.jsp

    <%-- Tell JSP that this page renders HTML --%>
    <%@ page contentType="text/html" %>
    <%-- Tell JSP to use the bean CalcBean --%>
    <jsp:useBean id="CalcBean" class="cbean.CalcBean" />
    <%-- Tell JSP to map CalcBean properties to like-named input parameters --%>
    <jsp:setProperty name="CalcBean" property="*" />
    <html>
    <head>
    <title>Show Calc Results bean</title>
    </head>

    <body bgcolor="#dddddd">
    Here's what was entered from <i>calcpage.html</i>: <p>
    <P>The next 4 lines show using the jsp:getProperty action to fetch bean
    properties
    <%-- jsp:getProperty writes the value of the bean property where coded --%>
    <p>Operand1 <b><jsp:getProperty name="CalcBean" property="operand1" /></b>
    <p>Operand2 <b><jsp:getProperty name="CalcBean" property="operand2" /></b>
    <p>Operation <b><jsp:getProperty name="CalcBean" property="operation" /></b>
    <p>Result <b><jsp:getProperty name="CalcBean" property="result" /></b>
    <br>
    <P>The next 4 lines show using the request implicit object to fetch input
    properties
    <%-- Access input parameters by accessing the request object. --%>
    <p>Operand1 <b><%= request.getParameter("operand1")%></b>
    <p>Operand2 <b><%= request.getParameter("operand2")%></b>
    <p>Operation <b><%= request.getParameter("operation")%></b>
    <p>result - not an input parameter!<b>
    <%= request.getParameter("result")%></b>
    <br>
    <p>The next group of lines access and display bean properties by using
    scriptlet code, expressions
    and JSP actions
    <%-- Invoke the 'get' method of the bean directly to get the property
    value --%>
    <% int enteredOperand1 = CalcBean.getOperand1();
    int enteredOperand2 = CalcBean.getOperand2();
    if (enteredOperand1 > enteredOperand2 ) { %>
    <%-- You may use expressions, actions and scriptlets together in your JSPs
    as shown below --%>
    <p> <jsp:getProperty name="CalcBean" property="operand1" /> is
    <b>larger</b> than <%= enteredOperand2 %>
    <% } else { %>
    <p> <%= enteredOperand1 %> is <b>smaller</b> than <jsp:getProperty
    name="CalcBean" property="operand2" />
    <% } %>

    </body>

    </html>
     
    Max, Jul 6, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benjamin Stewart

    To bean or not to bean...

    Benjamin Stewart, Jun 30, 2003, in forum: Java
    Replies:
    0
    Views:
    810
    Benjamin Stewart
    Jun 30, 2003
  2. David Thielen
    Replies:
    2
    Views:
    801
    Marek Lange
    Sep 12, 2003
  3. Miles Davenport
    Replies:
    4
    Views:
    557
    Andrew Thompson
    Feb 6, 2004
  4. kumar
    Replies:
    1
    Views:
    551
    Wendy S
    Feb 13, 2004
  5. Replies:
    3
    Views:
    1,281
Loading...

Share This Page