Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 3520: Website Development Assignment 5: JSP and Servlets Purposes of this ass

ID: 3920398 • Letter: C

Question

CS 3520: Website Development Assignment 5: JSP and Servlets Purposes of this assignment To introdace To reinforee TML orms General idea of the assignment Write a web application to function as a simple hand calculator that also keeps a "paper trail" of all your previous work. Details Your prog components: An HTML page This page is dynamically generated by JSP. It contains a trail of all the work you have done previously, for example something like this: 0.0+ 2.5 2.5+ 5.07.5 3.55 =26.625/-3.0=-8.875 etc. In addition, it should have the following HTML form elements: a text field in which you can enter numbers; add, subtract, multiply, and divide buttons; a clear button, to reset the current value to zero; and a clear all button, to reset the current value to zero and "erase" the paper trail (for when your page is getting too long and you want to start over) Since the user should enter only numbers into the text field, use JavaScript to validate the numbers before you submit the form. (Hint: Google on "JavaScript validate input field.") A servlet The servlet should handle getting the request, calling some other class to do the work, and forwarding the result to JSP for formatting. Along the way, the servlet should do session tracking, that is, it needs to keep track of the "paper trail" and the current value. I strongly recommend ram should consist of the following

Explanation / Answer

SOLUTION:

According to the given data the below code illustrates;

index.jsp:

<%--
Document : index
Created on : Aug 22, 2017, 1:04:43 PM
Author : debsdas
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculator Page</title>
</head>
<body>
<h1>Calculator</h1>
<form action="ControllerServlet" method="GET">
<input type="text" name="num1"/>
<select name="operation">
<option value="+">+</option>
<option value="-">_</option>
<option value="/">/</option>
<option value="*">*</option>
</select><br>
<input type="submit" name="Calculate" value="Calculate"/>
  
</form>
<form action="ControllerServlet" method="POST">
<input type="submit" value="Reset"></input>
  
</form>
  

<% if(request.getAttribute("res")!=null) { %>
<%= request.getAttribute("res")%>
<% } %>
</body>
</html>

  

Servlet:


package servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Calculation;


/**
*
* @author debsdas
*/
@WebServlet(name = "ControllerServlet", urlPatterns = {"/ControllerServlet"})
public class ControllerServlet extends HttpServlet {
  
String res="0";
  
// <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
*/
double num=0;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//method to calculate, invoked when uses presses calculate button
double num1=Double.parseDouble(request.getParameter("num1"));
String operation=request.getParameter("operation");
  
HttpSession session = request.getSession();
if(session!=null && session.getAttribute("num")!=null){
if(session.getAttribute("num") instanceof Double)
num=(Double)session.getAttribute("num");
else if(session.getAttribute("num") instanceof Integer)
num=(Integer)session.getAttribute("num");
}
else
num=0;
Calculation calc=new Calculation();   
num=calc.calculate(num,num1,operation); // call the model method
session.setAttribute("num", num);
String res="";
if(session.getAttribute("res")!=null)
res=(String)session.getAttribute("res");
else
res="0";
res+=operation+" "+num1+"="+num;
session.setAttribute("res", res);
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
request.setAttribute("res", res);
rd.forward(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 {
// method to reset the paper trail, invoked when user presses reset button
HttpSession session = request.getSession();
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
session.invalidate();
rd.forward(request, response);
//processRequest(request, response);
}

}

Model : Calculation.java

public class Calculation {
public double calculate(double num,double num1, String operation){
if(operation.equals("+")){
num=num+num1;
}else if(operation.equals("-")){
num=num-num1;
}else if(operation.equals("/")){
num=num/num1;
}else if(operation.equals("*")){
num=num*num1;
}

return num;
}
}


Web.xml:

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

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<display-name>Calculator Application</display-name>
<description>
This is a simple web application for Calculator.
</description>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>servlets.ControllerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/ControllerServlet</url-pattern>
</servlet-mapping>
</web-app>