JSP scriptlet code for EL

Asked

Viewed 190 times

-1

How can I rewrite this JSP code using Expression Language?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>



    <%

        int tipoForma;
        String tituloFormulario, headerFormulario;


        if (request.getParameter("formas") != null){
            tipoForma = Integer.parseInt(request.getParameter("formas"));
        }else{
            tipoForma = 0;
        }


        if (tipoForma == 1){
            tituloFormulario = "<img src='img/quadrado.jpg'/> <p>Calculando área do Quadrado </p>";
            headerFormulario = "<form action='calc-quadrado.jsp'/>"; 

        }else if (tipoForma == 2){
            tituloFormulario = "<p>Calculando área do Triângulo </p>";
            headerFormulario = "<form action='calc-triangulo.jsp'/>";

        }else if (tipoForma == 3){
            tituloFormulario = "<p>Calculando área da Circunferencia </p>";
            headerFormulario = "<form action='calc-circunferencia.jsp'/>";
        }else{
            tituloFormulario = "***Erro***";
            headerFormulario = "<form action='calc-triangulo.jsp'/>";
        }

        out.println(headerFormulario);

    %>

    <h2><%=tituloFormulario%></h2>

    <%if (tipoForma == 1){%>
        <input name="lado" placeholder="Digite o valor do lado">
    <%}else if (tipoForma == 2){%>
        <input name="base" placeholder="Digite o valor do base">
        <input name="lado" placeholder="Digite o valor do altura">
    <%}else if (tipoForma == 3){%>
        <input name="base" placeholder="Digite o raio">
    <%}else{%>
        <%=tituloFormulario%>
    <%}%>



    <input type="submit" value="Calcular">




</body>
</html>

1 answer

1

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>.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.