mardi 20 février 2018

Java: How could we replace a Servlet with JSLT Standard Tag Library❓❔

Hello and thank you for reading this question:

I am learning web development and design patterns with Java and I would like to learn how to use JSLT tags. The reason is because of they are easier to read and are the recommended approach, as here is being said: How to avoid Java code in JSP files?

I would like to replace this working servlet:

<%-- 
    Document   : Cuestionario
    Created on : 10-feb-2018, 9:36:15
    Author     : YonePC
--%>

<%@page import="org.Cuestionario"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <% Cuestionario cuestionario = (Cuestionario) session.getAttribute("cuestionario");%>

        <h1><%= cuestionario.getPregunta1()%></h1>
        <form>
            <div style="display:flex">
                <h2><%= cuestionario.getRespuestaTexto11()%></h2>
                <input type="checkbox" name="respuestaVerdad11" value="<%= cuestionario.getRespuestaVerdad11()%>" />
                <h2><%= cuestionario.getRespuestaTexto12()%></h2>
                <input type="checkbox" name="respuestaVerdad11" value="<%= cuestionario.getRespuestaVerdad12()%>" />
                <h2><%= cuestionario.getRespuestaTexto13()%></h2>
                <input type="checkbox" name="respuestaVerdad11" value="<%= cuestionario.getRespuestaVerdad13()%>" />
            </div>
            <input type="reset" value="Enviar respuesta" />
        </form>
    </body>
</html>

To be able to use JSLT tags. I have followed the JSLT's wiki's steps: https://stackoverflow.com/tags/jstl/info

However when I try the following little example:

<%-- 
    Document   : Cuestionario
    Created on : 10-feb-2018, 9:36:15
    Author     : YonePC
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="org.Cuestionario"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <c:set var="cuestionario" value="${session.getAttribute(\"cuestionario\")}"></c:set>
        <c:out value="${cuestionario}"></c:out>
        <% Cuestionario cuestionario = (Cuestionario) session.getAttribute("cuestionario");%>

        <h1><%= cuestionario.getPregunta1()%></h1>
        <form>
            <div style="display:flex">
                <h2><%= cuestionario.getRespuestaTexto11()%></h2>
                <input type="checkbox" name="respuestaVerdad11" value="<%= cuestionario.getRespuestaVerdad11()%>" />
                <h2><%= cuestionario.getRespuestaTexto12()%></h2>
                <input type="checkbox" name="respuestaVerdad11" value="<%= cuestionario.getRespuestaVerdad12()%>" />
                <h2><%= cuestionario.getRespuestaTexto13()%></h2>
                <input type="checkbox" name="respuestaVerdad11" value="<%= cuestionario.getRespuestaVerdad13()%>" />
            </div>
            <input type="reset" value="Enviar respuesta" />
        </form>
    </body>
</html>

Which tries to declare a variable and print it, the JSP page is white.

I have also read to fix this issue by myself: Set request attribute using JSTL

What are all the escape characters?

https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html

And I have understood the tag's meaning with: https://www.journaldev.com/2090/jstl-tutorial-jstl-tags-example

Could you help me, please?

As a note I write the web.xml I included under WEB-INF:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">



</web-app>

Also I included the .jar both under the NetBean's project's libraries and under /WEB-INF/lib folder.

In addition the app's architecture is:

enter image description here

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire