I’m learning JSP and I want to include subpage (on div named “content”) based on GET method generated by menu link.
<div id="menu">
<a href="/index.jsp?page=1">Subpage 2</a>
</div>
<div id="content>
//include somehow subpage s2.sjp
</div>
I’ve done somethink like that:
<%
int par = 0;
String which = "blank.jsp" ;
if (request.getParameter("page") != null) {
par = Integer.valueOf(request.getParameter("page"));
}
switch (par) {
case 0:
which = "blank.jsp";
break;
case 1:
which = "s2.jsp";
break;
}
%>
but I’m not sure if that is proper way to solve my problem in JSP.
You can just use EL in
<jsp:include>.Calling
foo.jsp?page=barwill let${param.page}resolve tobarand this<jsp:include>will then effectively include/WEB-INF/bar.jsp. No need for complex checks on numbers. Just use the filename as parameter.Note that the include JSP is been placed in
/WEB-INFfolder to prevent endusers from being able to open it directly by either purposefully or accidently calling its URL in browser address bar.You can put it in a JSTL
<c:catch>to prevent an exception from being thrown if the file doesn’t exist.