I've seen quite a few answers to my question but none fit my issue. The basic thing I am trying to do is this:
- Display a web page with tabs.
- One tab will have a form with many details. The second tab will display a result of some calculations based on user input in the form of a chart.
- I don't want to save the user data to a DB, only store in memory until browser is closed.
- The calculations are performed in a java class. The code for this already exists.
I am learning how to use intellijIDEA so have created a bare bones Hello World project just to learn how to pass data from user to java class doing the calculations and then display information on page. It is my first time learning java web applications using servlets and jsp. Here's what I have.
- index.jsp is the home page. The user clicks on link which displays calculator.jsp
- The calculator.jsp displays two tabs, one for mainform.jsp and the other for result.jsp
- The user fills in the form data displayed in mainform.jsp
- When the user clicks on the "result" tab then it goes into javascript file script.js
- script.js displayResultContent calls submit() on the details-form element which is the ID of the form. It then makes the result div visible in the Result tab.
- The form is linked to the servlet CalculateServlet. The doPost method is called which retrieves the form info and stores it in a java class called Person.
- The javascript then calls process(). This javascript method calls the CalculateServlet GET.
- The GET retrieves the details from the Calculator. In the real app this will be the result of the calculations to be displayed in the chart. But for my practice project it just returns the date of birth entered in the form.
- javascript then retrieves the element by ID and sets the value to be displayed. The problem is when the result is displayed the CalculateServlet is displaying a blank page. How do I stop this? Or what am I doing wrong. The code is below. I know it's a lot but sure hope someone can help.
header.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css"/>
<title>Header Title</title>
</head>
<body>
footer.jsp
<label class="footer">This is footer</label>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
index.jsp
<%@ include file = "header.jsp" %>
<div class="page">
<h1><%= "Hello World!" %>
</h1>
<br/>
<a href="calculator.jsp">Hello Servlet</a>
</div>
<%@ include file = "footer.jsp" %>
calculator.jsp
<%@ include file = "header.jsp" %>
<div id="tabs">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" id="details-tab" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false">Details</a>
</li>
<li class="nav-item">
<a class="nav-link" id="result-tab" href="#" tabindex="-1">Result</a>
</li>
</ul>
</div>
<div class="page" id="calculator">
<div class="content">
<div id="details">
<%@ include file ="mainform.jsp" %>
</div>
<div id="result">
<%@ include file ="result.jsp" %>
</div>
</div>
</div>
<%@ include file = "footer.jsp" %>
mainform.jsp
<div class="form-container">
<form name="detailsForm" id="details-form" method="post" action="CalculateServlet">
<!-- DATE OF BIRTH -->
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="form-group column-left">
<label for="date-of-birth" class="form-text">Date of Birth</label>
<div>
<input type="date" class="form-text" id="date-of-birth" name="date-of-birth">
</div>
</div>
</div>
<!-- CURRENT SUPER BALANCE -->
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="form-group column-left">
<label for="your-super-balance" class="form-text">Current Super Balance</label>
<div>
<input type="text" class="input-text" size="7" id="your-super-balance" name="your-super-balance"
onKeyDown="if (this.value.length == 7) this.value = this.value.slice(0, -1);"/>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="form-group column-left">
<label class="form-text">Job Category</label>
<div>
<select name="jobCat">
<option value="tech">Technology</option>
<option value="admin">Administration</option>
<option value="biology">Biology</option>
<option value="science">Science</option>
</select>
</div>
</div>
</div>
</form>
</div>
result.jsp
<div id="result-div">
<label class="form-label">Date of brith</label>
<br>
<label id="dob-label"></label>
</div>
script.js
function process() {
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState === 4){
//the request is completed, now check its status
if(ajaxRequest.status === 200){
var resultArray = JSON.parse(ajaxRequest.response);
var dobString = resultArray[0];
document.getElementById("dob-label").innerHTML = dobString;
}
else{
console.log("Status error: " + ajaxRequest.status);
}
}
else{
console.log("Ignored readyState: " + ajaxRequest.readyState);
}
}
ajaxRequest.open('GET', 'CalculateServlet');
ajaxRequest.send();
}
$(document).ready(function () {
$('#details-tab').click(displayDetails);
$('#result-tab').click(displayResultContent);
});
function displayDetails() {
$('#details').show();
$('#result').hide();
$('#result-tab').removeClass('active');
$('#details-tab').addClass('active');
}
function displayResultContent() {
$('#details').hide();
$('#result-tab').addClass('active');
$('#details-tab').removeClass('active');
document.getElementById("details-form").submit();
$('#result').show();
process();
}
style.css
html, body {
height: 100%;
margin: 0;
overflow:hidden;
background-color: antiquewhite;
}
.footer {
display: block;
width: 100%;
background-color: lightgrey;
height: 50px;
margin-top: 20px;
}
.form-container {
background-color: #FFF;
width: 70%;
height: 70%;
margin: 0 auto;
padding: 10px;
}
.column-left {
margin-top: 10px;
margin-left: 25%;
}
.column-right {
margin-right: 3%;
margin-top: 10px;
}
.form-label {
width: 200px;
height: 40px;
background-color: grey;
color: #FFF;
display:block;
}
#result {
margin-top: 40px;
display: none;
}
.page {
width: 70%;
margin: 0 auto;
margin-top: 20px;
border: 2px solid #000;
padding: 5px;
}
/*******************************************************************/
/* NAV TABS */
/******************************************************************/
nav ul {
margin: 0;
padding: 0;
}
.nav-link {
border-radius: 15px 15px 0px 0px !important;
text-decoration: none;
height: 40px;
font-size: 80%;
}
.nav-link:active,
.nav-link:hover,
.nav-link:focus {
border-radius: 15px 15px 0px 0px !important;
}
/*******************************************************************/
/* NAV TABS */
/******************************************************************/
#tabs {
border-bottom: 7px solid #FE6D73;
}
/* This is the actual tab */
.nav-link {
background-color: #eeeeee !important;
color: #aaaaaa;
width: 200px;
}
.nav-link:hover,
.nav-link:focus {
background-color: #FE6D73 !important;
color: #FEF9EF !important;
}
.nav-link.active {
background-color: #FE6D73 !important;
color: #FEF9EF !important;
}
.nav-item {
display: inline;
}
.nav-tabs>li>ul {
background-color: #FE6D73 !important;
}
/* Remove border from tabs */
.nav-tabs .nav-link {
border: 0px solid transparent;
}
li {
list-style: none;
}
#tabs {
width: 70%;
margin: 0 auto;
padding: 0;
}