JakartaJEEWebDevelopment

Jakarta JEE8 Enterprise Web Development

View on GitHub

Combining Java and JSP in our JEE 8 Web App Module

https://jakarta.ee/specifications/pages/3.0/jakarta-server-pages-spec-3.0.html#implicit-objects

We will now return to our charity registration example and start to separate our presentation logic and business logic with Java and JSP

Java combined with JSP Start Branch

1. Configuring JSP Properties in the Deployment Descriptor

Implicit Objects

Instead of defining the properties at the top of the JSP, we define them in the web.xml

Setting Properties for Groups of JSP Pages

<jsp-config>
 <!-- Can define one or more property group-->	
<!-- url-pattern is used to specify where to apply the properties -->
<jsp-property-group>
	 <!-- Specifies the location of where to apply the JSP properties-->	
    <url-pattern>*.jsp</url-pattern> <!-- All jsp(f) files, all locations -->
    <url-pattern>*.jspf</url-pattern>
    <page-encoding>UTF-8</page-encoding>
    <scripting-invalid>false</scripting-invalid>
    <include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
    <trim-directive-whitespaces>true</trim-directive-whitespaces>
    <default-content-type>text/html</default-content-type>
</jsp-property-group>

</jsp-config>

because of this multiple property groups there are complicated rules of precedence that you will have to follow, therefore it is much easer to stick to the “Keep it Simple Stupid” - KISS principle This tag is used to include the base.jspf at the beginning of each JSP file

<include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>

to include files at the end of each JSP file in a specific property group we use

<include-coda>          you can also use multiples of these two tags in a property group

<page-encoding>
<default-content-type>

we are already familiar with in our implicit variable examples from our previous tutorial

<trim-directive-whitespaces>true</trim-directive-whitespaces>

this removes white space generated by directives in JSP

<scripting-invalid>			

allows Embedded Java in any JSP.

The above property tags has to appear in the order and only are non optional

We placed the base.jspf in the WEB-INF directory to prevent access from the web

<%@ page import="com.nicordesigns.Registration, com.nicordesigns.Attachment" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

The index.jsp file has been updated as follows:

<%@ page session="false" %>
<c:redirect url="/registrations" /> 		

session is false presents the JSESSIONID parameter from being appended to the redirect url to the registrations servlet in the second line

2. Forwarding a request from a Servlet to a JSP

In the Model View Controller Pattern used by combining Servlets and JSP’s , the servlet acts as the Controller that does the business logic and updates the database Model and the JSP acts as the View Presentation that interacts with the Controller Servlet

3. Using the Request Dispatcher

Next we will change the RegistrationServlets viewRegistration method but first we will update our presentation layer which is the viewRegistration.jsp

private void showRegistrationForm(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
{
	
 //Never use response object after a 'forward'
 request.getRequestDispatcher("/WEB-INF/jsp/view/registrationForm.jsp")
 .forward(request, response);
}

now forwards to the following registrationForm JSP internally without using browser http redirect

<%@ page session="false" %>
<!DOCTYPE html>
<html>
    <head>
        <title>Customer Support</title>
    </head>
    <body>
        <h2>Create a Registration</h2>
        <form method="POST" action="registrations" enctype="multipart/form-data">
            <input type="hidden" name="action" value="create"/>
            Your Name<br/>
            <input type="text" name="customerName"><br/><br/>
            Subject<br/>
            <input type="text" name="subject"><br/><br/>
            Body<br/>
            <textarea name="body" rows="5" cols="30"></textarea><br/><br/>
            <b>Attachments</b><br/>
            <input type="file" name="file1"/><br/><br/>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>

4. Designing for the View Layer

Next we look at the viewRegistration.jsp

<%@ page session="false" import="com.nicordesigns.Attachment, 			com.nicordesigns.Registration" %>
	<%
    String registrationId = (String)request.getAttribute("RegistrationId");
	    Registration registration =(Registration)request.getAttribute("Registration");
	%>
<!DOCTYPE html>
	<html>
<head>
    <title>Customer Support</title>
</head>
<body>
    <h2>Registration #<%= registrationId %>: <%= registration.getSubject() %></h2>
    <i>Customer Name - <%= registration.getCustomerName() %></i><br /><br />
    <%= registration.getBody() %><br /><br />
    <%
        if(registration.getNumberOfAttachments() > 0)
        {
            %>Attachments: <%
            int i = 0;
            for(Attachment a : registration.getAttachments())
            {
                if(i++ > 0)
                    out.print(", ");
                %><a href="<c:url value="/registrations">
                    <c:param name="action" value="download" />
                    <c:param name="RegistrationId" value="<%= registrationId %>" />
                    <c:param name="attachment" value="<%= a.getName() %>" />
                </c:url>"><%= a.getName() %></a><%
            }
            %><br /><br /><%
        }
    %>
    <a href="<c:url value="/registrations" />">Return to list Registrations</a>
</body>
</html>

we see that the presentation layer requires registrationId and registration which is why we update the viewRegistration method in the RegistrationServlet

private void viewRegistration(HttpServletRequest request,
                        HttpServletResponse response)
        throws ServletException, IOException
{
    String idString = request.getParameter("registrationId");
    Registration registration = this.getRegistration(idString, response);
    if(registration == null)
        return;

    request.setAttribute("RegistrationId", idString);
    request.setAttribute("Registration", registration);

    request.getRequestDispatcher("/WEB-INF/jsp/view/viewRegistration.jsp")
           .forward(request, response);
}

finally we separate the view and presentation of the listRegistration.jsp

<%@ page session="false" import="java.util.Map, com.nicordesigns.Attachment, 		com.nicordesigns.Registration" %>
	<%
	    @SuppressWarnings("unchecked")
	    Map<Integer, Registration> registrationDatabase =
	            (Map<Integer, Registration>)request.getAttribute("registrationDatabase");
	%>
	<!DOCTYPE html>
	<html>
<head>
    <title>Customer Support</title>
</head>
<body>
    <h2>Registrations</h2>
    <a href="<c:url value="/Registrations">
        <c:param name="action" value="create" />
    </c:url>">Create Registration</a><br /><br />
    <%
        if(registrationDatabase.size() == 0)
        {
            %><i>There are no Registrations in the system.</i><%
        }
        else
        {
            for(int id : registrationDatabase.keySet())
            {
                String idString = Integer.toString(id);
                Registration registration = registrationDatabase.get(id);
                %>Registration #<%= idString %>: <a href="<c:url value="/Registrations">
                    <c:param name="action" value="view" />
                    <c:param name="RegistrationId" value="<%= idString %>" />
                </c:url>"><%= registration.getSubject() %></a> (customer:
    			<%= registration.getCustomerName() %>)<br /><%
            }
        }
    %>
</body>
</html>

and we update the listRegistrations method in the RegistrationServlet.java

private void listRegistrations(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
request.setAttribute("registrationDatabase", this.registrationDatabase);

request.getRequestDispatcher("/WEB-INF/jsp/view/listRegistrations.jsp").forward(request, 		response);
}

5. Now we will run and debug the application

Java combined with JSP Finish Branch