0

I have the same Java scriptlet copied and pasted into multiple .jsp files. I would like to replace it with a solution that's easier to maintain and has better readability

    <%
    if (!MediaUtil.validateAuthorization()) {
        out.println("Unauthorized");
    } else {
        String srcquery = request.getQueryString();
        if (srcquery == null) {
            srcquery = "";
        }
        User currentUser = UserService.findCurrentUser();

        if (currentUser == null) {
            out.println(User.MESSAGE_NO_USER);
        } else {
    %>

    <html>
    <head>

EDIT: I have rewritten the code using JSTL after FrenchFigaro's answer. Final code for anyone interested is below:

<c:choose>
    <c:when test="${!MediaUtil.validateOrganization()}">
        <c:out value="Unauthorized"/>
    </c:when>
    <c:otherwise>
        <c:set var="srcquery" value="<%=request.getQueryString()%>"/>
        <c:choose>
            <c:when test="${srcquery == null}">
                <c:set var="srcquery" value=""/>
            </c:when>
        </c:choose>
        <c:set var="currentUser" value="<%=UserService.findCurrentUser()%>"/>
        <c:choose>
            <c:when test="${currentUser == null}">
                <c:out value="<%=User.MESSAGE_NO_USER%>"/>
            </c:when>
            <c:otherwise>
                <html>

                (...)

                </html>
            </c:otherwise>
        </c:choose>
    </c:otherwise>
</c:choose>


Lucas Mendonca
  • 387
  • 3
  • 13
  • Sounds good, go for it. If you have any problem while doing it, please explain that – Nico Haase May 17 '19 at 13:14
  • You could implement a Filter that will implement the same logic. More information on filters here: https://stackoverflow.com/questions/4122870/what-is-the-use-of-filter-and-chain-in-servlet – Sofo Gial May 17 '19 at 13:17

1 Answers1

1

To replace the if/else part, you can use <c:if> or <c:choose>

The first one provides a single choice and no else (but two tags with opposite conditions will do the trick).

Better to use <c:choose> in your case.

<c:choose>
    <c:when test="${condition}">...</c:when>
    <c:otherwise>...</c:otherwise>
</c:choose>

As for the rest, you will need to use a bean to transfer the data between your servlet and the JSP

FrenchFigaro
  • 371
  • 2
  • 18
  • Sounds good. But how can I reuse this in my multiple .jsp files without having to copy and paste the same code everywhere? Is there a way to put this into a function or something like that? – Lucas Mendonca May 17 '19 at 13:59
  • JSP allows the insertion of a child page inside a parent page. You can do this with `` or ``. I haven't done this in a very long time so I'm not sure how it would be done. Those are dynamic includes, which allow for the passing of parameters. You can also do a static include using `<%@include file="path/to/child.jsp" %>`. Those are less flexible, but more performant. I haven't used either in a while, so I wouldn't be the best resource on how to use them. – FrenchFigaro May 17 '19 at 14:28
  • @LucasMendonça Re: include. Here's an example on how to do that [https://www.javatpoint.com/jsp-include-action](https://www.javatpoint.com/jsp-include-action) – FrenchFigaro May 17 '19 at 14:33
  • @FrenchFigaro your link respond now "503 Service Unavailable" – 936940 Oct 12 '22 at 14:31
  • 1
    @936940 I tried it this instant and it works. Might have been a temporary issue on the site's end – FrenchFigaro Oct 13 '22 at 15:03