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>


2
  • Sounds good, go for it. If you have any problem while doing it, please explain that Commented May 17, 2019 at 13:14
  • You could implement a Filter that will implement the same logic. More information on filters here: stackoverflow.com/questions/4122870/… Commented May 17, 2019 at 13:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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?
JSP allows the insertion of a child page inside a parent page. You can do this with <c:import> or <jsp:include>. 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.
@LucasMendonça Re: include. Here's an example on how to do that https://www.javatpoint.com/jsp-include-action
@FrenchFigaro your link respond now "503 Service Unavailable"
@936940 I tried it this instant and it works. Might have been a temporary issue on the site's end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.