First, I recommend using UTF-8 instead of ISO-8859-1. The whole world is unifying and universalizing everything around the UTF-8 because it has the advantage of being able to encode any characters in any languages without problems, including mixing characters from totally dissimilar groups such as Chinese and Hebrew and is recognized and accepted by almost all modern character coding systems. In addition, its format is very close to the native format in which strings are stored in Java. It is even capable of encoding emojis ().
However, if you don’t want to use UTF-8, that’s fine. Just make sure to save your JSP file with the same encoding as it is served.
Also be careful with the HTML format that is injected into the page. Tag <img>
and <p>
within tags <h2>
not a good idea. Fortunately, replacing scriptlets with pure JSP itself should help you solve that problem. The tag <c:choose>
should help you:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cálculo de área</title>
</head>
<body>
<c:choose>
<c:when test="${param.formas == 1}">
<form action='calc-quadrado.jsp'>
<img src='img/quadrado.jpg' />
<h2>Calculando área do Quadrado</h2>
<input name="lado" placeholder="Digite o valor do lado" />
<input type="submit" value="Calcular" />
</form>
</c:when>
<c:when test="${param.formas == 2}">
<form action='calc-triangulo.jsp'>
<h2>Calculando área do Triângulo</h2>
<input name="base" placeholder="Digite o valor do base" />
<input name="altura" placeholder="Digite o valor do altura" />
<input type="submit" value="Calcular" />
</form>
</c:when>
<c:when test="${param.formas == 3}">
<form action='calc-circunferencia.jsp'>
<h2>Calculando área da Circunferência</h2>
<input name="raio" placeholder="Digite o raio" />
<input type="submit" value="Calcular" />
</form>
</c:when>
<c:otherwise>
<form action=''>
<h2>***Erro***</h2>
</form>
</c:otherwise>
</c:choose>
</body>
</html>
If you consider all this very repetitive, when repeating several HTML snippets in different <c:when>
s, you can set several variables in <c:when>
s using <c:set>
and then use them with <c:if>
, <c:url>
and <c:out>
:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cálculo de área</title>
</head>
<body>
<c:choose>
<c:when test="${param.formas == 1}">
<c:set var="target" value="calc-quadrado.jsp" />
<c:set var="img" value="img/quadrado.jpg" />
<c:set var="titulo" value="Calculando área do Quadrado" />
<c:set var="campo1" value="lado" />
<c:set var="descricao1" value="Digite o valor do lado" />
</c:when>
<c:when test="${param.formas == 2}">
<c:set var="target" value="calc-triangulo.jsp" />
<c:set var="titulo" value="Calculando área do Triângulo" />
<c:set var="campo1" value="base" />
<c:set var="descricao1" value="Digite o valor da base" />
<c:set var="campo2" value="altura" />
<c:set var="descricao2" value="Digite o valor da altura" />
</c:when>
<c:when test="${param.formas == 3}">
<c:set var="target" value="calc-circunferencia.jsp" />
<c:set var="titulo" value="Calculando área da Circunferência" />
<c:set var="campo1" value="raio" />
<c:set var="descricao1" value="Digite o valor do raio" />
</c:when>
<c:otherwise>
<c:set var="titulo" value="***Erro***" />
</c:otherwise>
</c:choose>
<form action='<c:url value="${target}" />>
<c:if test="${img}"><img src='<c:url value="${img}" />' /></c:if>
<h2><c:out value="${titulo}" /></h2>
<c:if test="${campo1}"><input name="<c:out value="${campo1}" />" placeholder="<c:out value="${descricao1}" />" /></c:if>
<c:if test="${campo2}"><input name="<c:out value="${campo2}" />" placeholder="<c:out value="${descricao2}" />" /></c:if>
<input type="submit" value="Calcular" />
</form>
</body>
</html>
Ah, and always make sure your HTML tags are well formed. The <input>
It’s only gonna work if you’re inside the <form>
.