I’m looking for help/advice with creating simple JSP website using equivalent of PHP include+switch function.
The goal is that I want to be able to switch between multiple JSP include pages in one main page.
What would be the simplest possible form of above ‘function’?
There you have the
<jsp:include>for. You can use EL to specify thepageattribute.Create a
/WEB-INF/main.jspfile which look like:You can control the
${page}value with help of a page controller servlet. Something like:Map this servlet in
web.xmlas follows:This way the servlet is accessible through
http://example.com/context/page/foo.jspand in this URL example it will then get/foo.jspfrom the pathinfo and thus set thepageattribute with the value/WEB-INF/foo.jspso that it is available in EL as${page}so that thejsp:includeknows what it should include. No need for nasty scriptlets or switch statements.In the
/WEB-INF/foo.jspyou can just write down HTML as if it is placed inside the HTML<body>tag.Note that the JSP files are placed in
/WEB-INF, this is done so to prevent direct access by URL so that the users cannot request them without going through the page controller, such as for examplehttp://example.com/context/foo.jspwhich would only return the partial content (the to-be-included page).Hope this helps.