Hello everyone, I am a little stuck an this simple task. I have created an html with a form that has 2 user input boxes and 4 button +,-,*,/,. Basically a simple calculator. And I have a jsp to process the form. I am trying to now create a java class to process the 4 methods, then call the class with the jsp through the html. So basically I want my class to do what my jsp is currently doing.
Here is my html code...

<!DOCTYPE HTML>
<html>
    <head>
        <title>KanJ_calculator</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>

    <body>
        <h3>Justin Calculator</h3>
        <a href="index.html">HOME</a><br>
        <a href="kanj_oop.html">OOP</a><br /><br />

    <form action=kanj_calculator.jsp method="get">
        INPUT 1: <input type = "text" name = "input1"><br /><br>
        INPUT 2: <input type = "text" name = "input2"><br /><br />         

        <input type = "submit" name = "btn+" Value = "+" style = "margin-right: 35px">
        <input type = "submit" name = "btn-" Value = "-" style = "margin-right: 35px">
        <input type = "submit" name = "btn*" Value = "*" style = "margin-right: 35px">
        <input type = "submit" name = "btn/" Value = "/">

    </form>

    </body>

</html>

And here is my current .jsp code...

<!DOCTYPE HTML><%@page language="java"
    contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Justin  kanj_calculator</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h3>Justin kanj_calculator.jsp</h3>
    <%
        int int1 = (Integer.parseInt(request.getParameter("input1")));
        int int2 = (Integer.parseInt(request.getParameter("input2")));

        try
        {
            if (request.getParameter("btn+") != null)
            {
                out.print("Result = " + (int1 + int2));
            }
            else if (request.getParameter("btn-") != null)
            {
                out.print("Result = " + (int1 - int2));
            }
            else if (request.getParameter("btn*") != null)
            {
                out.print("Result = " + (int1 * int2));
            }
            else if (request.getParameter("btn/") != null)
            {
                out.print("Result = " + (int1 / int2));
            }
        }
        catch (Exception e)
        {
            out.println("Error! Please enter a valid number: " + e);

        }
     %>
</body>
</html>

Here is my .java class code so far...which isn't much.

package kanj_unit4;

public class KanJ_Calculator 
{
    int input1 = 0;
    int input2 = 0;


}

Recommended Answers

All 8 Replies

First of all, for jsp related questions, you should post in this forum.

If you just copied this code from somewhere: stop doing so and learn to write your own.

If this code is yours: remove all of that Java code out of the jsp. the jsp file is meant to contain UI only. It's good you're trying to learn to use servlets, but don't start learning this based on an application: start the way you did with Java itself: have a servlet pass a simple "Hello world" message for you to show in your jsp. It will be a lot easier to understand.

Yes I wrote this code. And yes I know I need to remove the code from my jsp. I only put is in there to basically test my code.

You can't really test the code until you actually have it. re-writing the code to a servlet will be peanuts, what you need to do, is to learn to work with them. for this: start at the beginning. just have it pass and display a hardcoded hello world message.

after that: add an input field in which you enter your name, and have the jsp page show "hello world, " + yourName
as processed by the servlet.

I am not trying to code the jsp. I am trying to code the class. but here is my code for hello world.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Hello World Logic Exercise</title>
</head>
<body>

Hello World links to JSP page

<br></br><br></br> 

<!-- This is the HTML form that displays the buttons & its action property links to the file named in the " " for processing -->

<form action="HelloWorld.jsp">

    <input type="submit" name="language" value="English"></input>
    <input type="submit" name="language" value="Spanish"></input>


</form>

</body>
</html>

and here is the jsp code...

<!DOCTYPE HTML>

<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<head>
<title>Justin  Logic / Processing Output</title>
</head>

<body>
    <h2>
        <!-- ******************************************************************************************************* -->
        <!-- ***** Change FirstName & LastName to your firstname  in the following HTML statement ***** -->
        Justin  Output from Processing:
        <!-- ****************************************************************************************************** -->

        <br />
        <br />

        <%
            //get user data stored by web server in QueryString array
            String languageStr = request.getParameter("language");
            out.print("language = " + languageStr + "<br /><br />"); //for debug purposes

            if (languageStr.equalsIgnoreCase("English")) //test to see if user clicked the English button
            {
                out.print("Hello World, Justin "); //if user clicked the English button display "Hello World"
            } 
            else if (languageStr.equalsIgnoreCase("Spanish")) //else if English button not clicked test to see if user clicked Spanish button
            {
                out.print("Hola Mundo, Justin "); //if user clicked the Spanish button display "Hola Mundo"
            }
            else if (languageStr.equalsIgnoreCase("French")) //else if Spanish button not clicked test to see if user clicked French button
            {
                out.print("Bonjour tout le monde, Justin "); //if user clicked the FRench button display "Bonjour tout le monde"
            }
            else if (languageStr.equalsIgnoreCase("Italian")) //else if French button not clicked test to see if user clicked Italian button
            {
                out.print("Cia Mondo, Justin "); //if user clicked the Italian button display "Cia Mondo"
            }

            //Here only if NONE of the above if( ) statements are true     
            else //if none of the about if( ) statements are true then execute the following block of code
            {
                out.print("Unavailable Language"); //If none of the above if( ) statements are true then display "Unavailable" message
            }
        %>

    </h2>
</body>
</html>

I posted that as a suggestion to learn to work with servlets, not to put the code in your jsp file.
what textbook/resources are you using as study material?

I have Murachs C# 2010, Murachs Java Programming 4th Edition, Introduction to Java Using WebSphere 3rd edition.

Here is a simple html form using username and password inputs and the html code is...

<form name="loginForm" method="post" action="loginServlet">
    Username: <input type="text" name="username"/> <br/>
    Password: <input type="password" name="password"/> <br/>
    <input type="submit" value="Login" />
</form>

And I know how to do this in the java class...

package net.codejava.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // read form fields
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("username: " + username);
        System.out.println("password: " + password);

        // do some processing here...

        // get response writer
        PrintWriter writer = response.getWriter();

        // build HTML code
        String htmlRespone = "<html>";
        htmlRespone += "<h2>Your username is: " + username + "<br/>";       
        htmlRespone += "Your password is: " + password + "</h2>";     
        htmlRespone += "</html>";

        // return response
        writer.println(htmlRespone);

    }

}

But I don't understand how to do this with user inout of a double the add or subtract and so on. Am I just over thinking the process?

the fact that you have code like this:

// build HTML code
        String htmlRespone = "<html>";
        htmlRespone += "<h2>Your username is: " + username + "<br/>";       
        htmlRespone += "Your password is: " + password + "</h2>";     
        htmlRespone += "</html>";
        // return response
        writer.println(htmlRespone);

in your servlet shows you need to learn more.

Murachs Java Programming 4th Edition, Introduction to Java Using WebSphere 3rd edition.

Well, I can't really comment on these books (the C# book is a bit pointless to learn the Java language, though). But if you want to learn about jsp and servlets, why not pick up a book that deals with those? If you have a good basic knowledge of Java, Head First: Servlets & Jsp could help you out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.