Search code examples
javajspnetbeansglassfish

How I create a program in JSP that produces random addition equations that you can answer which then checks to see if you're correct or not?


Let me preface this by saying this: I am an absolute beginner regarding JSP. I'm still trying to understand the fundamentals while also get my work done in a timely manner without asking the instructor for help (thanks to them never returning emails when me and some other classmates have reached out regarding the work and things such as missing information or links).

I have been tasked to make a program in JSP that generates random addition problems. The user is able to input the answer they think is write and, after submitting them, will be told by the program which they've got right and wrong. I've written a program thus far that, while it runs, it doesn't actually display anything in the browser. It just constantly says "waiting for localhost'. Here is what I've made thus far:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@ page import = "java.util.*"%>

 <>
<!DOCTYPE html>
<html>
    <head>
        <title>Addition Quiz</title>
    </head>
    <body>
        <%
            int a, b, answer;

            Random rand = new Random();
             a = rand.nextInt(1000); 
             b = rand.nextInt(1000);

             %>

             <form>
             <%= a %> + <%= b %> =
             <input type="number" value="answer" name="answer"><br><br>
             <input type="submit" value="Submit">
             </form>

             <%

                 request.getParameter("answer");

                 answer = a + b;

                 if (answer==a+b) {

                     out.print("Correct");

                 } else if (answer!=(a+b)) {

                     out.print("Incorrect");
                 }
%>

    </body>
</html>

What am I doing wrong here and, in your opinion, how should I go about it? I'm using NetBeans and GlassFish.

EDIT: Added to the program as per the suggestion of Swati. While it now outputs, the page will automatically say correct and whenever I submit a number to answer the equation the page just sorta refreshes to a new equation.


Solution

  • As you submit it is reloading the page hence new random numbers are generating.Please find the below code. index.html

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page import="java.util.*"%>
    
    <>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Addition Quiz</title>
    </head>
    <body>
        <%
                int a, b, answer;
    
                Random rand = new Random();
                 a = rand.nextInt(1000); 
                 b = rand.nextInt(1000);
    
                 %>
    
        <form action="result.jsp">
            <%= a %>
            +
            <%= b %>
            <% 
                  request.setAttribute("a", a);
                  request.setAttribute("b", b);
                  %>
            = <input type="number" value="answer" name="answer"><br>
            <input type="hidden" name="a" value="<%=a%>">
            <input type="hidden" name="b" value="<%=b%>">
            <br><input type="submit" value="Submit">
        </form>
    
    
    </body>
    </html>
    

    result.html

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>answer is::<%=Integer.parseInt(request.getParameter("answer")) %></h1>
        <h1>a+b is::<%=Integer.parseInt(request.getParameter("a"))+Integer.parseInt(request.getParameter("b")) %></h1>
        <h1>RESULT IS <%= (Integer.parseInt(request.getParameter("answer")) == (Integer.parseInt(request.getParameter("a"))+Integer.parseInt(request.getParameter("b"))) )?"Correct":"InCorrect"%></h1>
    </body>
    </html>