Skip to content

Java, JSP, JSTL, EL, Tomcat notes and tips

Michael Hulse edited this page Aug 2, 2017 · 8 revisions

How to include taglibs (and other globals) without repeating yourself

  1. Create a file called global.jsp (for example).
  2. Add your taglib calls and other important code:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:set var="page" scope="session" value="${fn:split(fn:split(pageContext.request.servletPath, '.')[0], '/')[0]}" />
  1. Put this at the top of every include file you need the taglibs and/or variables:
<%@ include file="/includes/global.jsp" %>

Now you can use EL (Expression Language) and/or <c:xxx> tags on your templates.

Comments

<%-- … something here … --%>

Includes

See: What's the difference between including files with JSP include directive, JSP include action and using JSP Tag Files?

Static includes (compile time):

<%@ include file="path/to/file.jsp" %>

Dynamic includes (execution time):

<jsp:include page="path/to/file.jsp" />
<c:import url="http://www.example.com/foo/bar.html" />

Passing params to jsp:include:

<jsp:include page="includes/top.jsp">
	<jsp:param name="title" value="Detective Activity" />
</jsp:include>

Links

To be sorted

	<%--
		<c:set var="s" value="${request.getHeader('referer')}" />
		<%out.println(request.getHeader("referer"));%>
		(${s})
		${fn:contains(s, 'support-team.jsp')}
		if(request.getHeader("referer").indexOf("support-team.jsp") != -1)
	--%>
	${header.referer}, ${header['referer']}, ${param.from}, ${param['from']}
	<c:if test="${param.from == 'support-team'}">
		<a href="#" class="close-ish" onclick="history.go(-1);return false;">Close</a>
	</c:if>




	${header.referer}
	<c:if test="${fn:contains(header.referer, 'support-team.jsp')}">
		<a href="#" class="close-ish" onclick="history.go(-1);return false;">Close</a>
	</c:if>

This looks like lots of great tips: http://stackoverflow.com/questions/1296235/jsp-tricks-to-make-templating-easier

Clone this wiki locally