Issue
I want my budget to be displayed on the first visit of the website with the start = 50. And this should be shown without submitting once.
So problem nr.1 : How can I display the budget = 50 without letting my servlet work before neccessary?
problem nr.2:- After I typed numbers (XXXXXX) and submit them the budget should be reduced by 5. This should happen every time I submit the numbers. But it doesn't. It seems like it doesn't save the budget.
My JSP:-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="CSS/style.css">
</head>
<body>
<h5>Losziehung</h5>
<p id="u1">Derzeitiges Budget: ${budget}
<div id="formular">
<form method="post" action="GamblingServlet">
<p>Ihre Glueckszahl
<input name="eingabe" type="text" size="50" maxlength="6" placeholder="XXXXXX">
<br><button id="lossenden" name="losziehung"type="submit">Losziehung durchfuehren</button>
<br><p>Eine Losziehung kostet 5Eur
</form>
Derzeitiges Budget: ${budget}
</div>
</body>
</html>
My Servlet
@WebServlet("/GamblingServlet")
public class GamblingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String gluecksZahlen = request.getParameter("eingabe");
String budgetInt = "50";
HttpSession session = request.getSession();
session.setAttribute("budget", budgetInt);
if(session.isNew()) {
budgetInt = "45";
session = request.getSession();
session.setAttribute("budget", budgetInt);
} else {
String budgetZwei = (String) session.getAttribute("budget");
int sessionGuthaben = Integer.parseInt(budgetZwei);
sessionGuthaben = sessionGuthaben - 5;
budgetInt = Integer.toString(sessionGuthaben);
session.setAttribute("budget", budgetInt);
}
//Weiterleiten an eine andere Ressource (Servlet, HTML, JSP...)
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
Solution
You can check if the session
is not empty using <c:if></c:if>
like below :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Derzeitiges Budget: ${budget}
<c:if test="${empty budget}">
50 //will get print only if conditon is true
</c:if>
Then , at your backend(servlet) you can do like below :
String gluecksZahlen = request.getParameter("eingabe");
HttpSession session = request.getSession();
//check if seesion is null
if (request.getServletContext().getAttribute("budget") == null) {
//set default
session.setAttribute("budget", "45");
} else {
//get value from session
String budgetZwei = (String) request.getServletContext().getAttribute("budget");
int sessionGuthaben = Integer.parseInt(budgetZwei);
sessionGuthaben = sessionGuthaben - 5;
String budgetInt = Integer.toString(sessionGuthaben);
session.setAttribute("budget", budgetInt);
}
request.getRequestDispatcher("index.jsp").forward(request, response);
Answered By - Swati