168

I am currently trying to learn JSP. My question is, at present I used to include the header and footer of the page using:

<%@include file="includes/header.jsp" %>

and

<%@include file="includes/footer.jsp" %>

But now, I have separated the page content also. So, if user clicks on a page, say products, it has to load the JSP file which is situated in: includes/pages/products.jsp So, the link to the user is like: <a href="index.jsp?p=products">Products</a>.

So, I have to get the p value and display the page based on it.

Following is what I have done so far.

<%
 if(request.getParameter("p")!=null)
 { 
   String p = request.getParameter("p");
%>    

<%@include file="includes/page_name.jsp" %>

<% 
 }
%>

So, how do I place the value of variable "p" in position of "page_name" ?

Or, is there any other method that I could use ?

In PHP, we could use the include() or include_once(). I am bit stuck in this JSP. :(

2
  • 1
    Why not hardcode the pagename there? anyways you are checking for P, then you know distination page will be p.jsp, so any reason to complicate?
    – kosa
    Feb 2, 2012 at 9:52
  • Thanks for replying. It was for an example. Actually, I have subdivided the page into several. For example, if I have a Products page in admin area, to add new product, edit a product, search a product, view a product, etc.. it is separated into sub pages. So, hardcoding each line would look nasty. Say, to display my name for only 10 times, I could either choose to print using a loop or write down the 10 lines. But I think, choosing the loop is better. Isn't it ? I think, this might be the solution: stackoverflow.com/questions/482788/… Feb 2, 2012 at 10:03

6 Answers 6

248

What you're doing is a static include. A static include is resolved at compile time, and may thus not use a parameter value, which is only known at execution time.

What you need is a dynamic include:

<jsp:include page="..." />

Note that you should use the JSP EL rather than scriptlets. It also seems that you're implementing a central controller with index.jsp. You should use a servlet to do that instead, and dispatch to the appropriate JSP from this servlet. Or better, use an existing MVC framework like Stripes or Spring MVC.

7
  • 1
    Thanks for your suggestions. :) I have tried using jsp:include tag. But at runtime, it gives a file not found error. Exception details:java.io.FileNotFoundException: /includes/&lt;%= p %&gt;.jsp. Looks like the <%= p %> is used as it is in the file path! Feb 2, 2012 at 10:25
  • 7
    Don't use scriptlets. Use JSP EL: page="/includes/${param.p}". Or try with page="<%= "/includes/" + p %>".
    – JB Nizet
    Feb 2, 2012 at 11:21
  • 3
    Thank you. I used it like this: <jsp:include page="<%= "includes/" + p + ".jsp" %>" /> and it's working now :) Feb 2, 2012 at 11:34
  • 2
    Very good answer! I've seen this pattern (central controller in index.jsp) too many times and it always leads to trouble. Mar 6, 2013 at 9:58
  • Should not that be translation time? And I think you can still use parameters with that? Jul 14, 2014 at 16:45
27

You can use parameters like that

<jsp:include page='about.jsp'>
    <jsp:param name="articleId" value=""/>
</jsp:include>

and

in about.jsp you can take the paramter

<%String leftAds = request.getParameter("articleId");%>
18

You can use Include Directives

<%
 if(request.getParameter("p")!=null)
 { 
   String p = request.getParameter("p");
%>    

<%@include file="<%="includes/" + p +".jsp"%>"%>

<% 
 }
%>

or JSP Include Action

<%
 if(request.getParameter("p")!=null)
 { 
   String p = request.getParameter("p");
%>    

<jsp:include page="<%="includes/"+p+".jsp"%>"/>

<% 
 }
%>

the different is include directive includes a file during the translation phase. while JSP Include Action includes a file at the time the page is requested

I recommend Spring MVC Framework as your controller to manipulate things. use url pattern instead of parameter.

example:

www.yourwebsite.com/products

instead of

www.yourwebsite.com/?p=products

Watch this video Spring MVC Framework

3
  • 1
    the video is protected ..please share it to public
    – AutoMEta
    Jun 27, 2013 at 18:38
  • Can you check if your first example actually works? I can't seem to get it to work.
    – Jus12
    Nov 14, 2013 at 20:17
  • what happened to the video ? Apr 29, 2022 at 17:01
11

At page translation time, the content of the file given in the include directive is ‘pasted’ as it is, in the place where the JSP include directive is used. Then the source JSP page is converted into a java servlet class. The included file can be a static resource or a JSP page. Generally, JSP include directive is used to include header banners and footers.

Syntax for include a jsp file:

<%@ include file="relative url">

Example

<%@include file="page_name.jsp" %>
6

1.<a href="index.jsp?p=products">Products</a> when user clicks on Products link,you can directly call products.jsp.

I mean u can maintain name of the JSP file same as parameter Value.

<%
 if(request.getParameter("p")!=null)
 { 
   String contextPath="includes/";
   String p = request.getParameter("p");
   p=p+".jsp";
   p=contextPath+p;

%>    

<%@include file="<%=p%>" %>

<% 
 }
%>

or

2.you can maintain external resource file with key,value pairs. like below

products : products.jsp

customer : customers.jsp

you can programatically retrieve the name of JSP file from properies file.

this way you can easily change the name of JSP file

5
  • Thank you. I am using the first thing at the moment. Actually, I am having problem with howto do this ! Feb 2, 2012 at 10:29
  • org.apache.jasper.JasperException: /admin.jsp(239,44) PWC6117: File "/<%=p%>" not found. Did that worked in your side ? Feb 2, 2012 at 10:56
  • this is path related issue check if file exists in ur path,I guess u r missing context path (should be includes/admin.jsp).I have not simulated entre scenerio my side Feb 2, 2012 at 11:03
  • 1
    I have tried echoing the value of "p". It is: includes/add.jsp. Then used this in include derivative: <%@ include file="includes/add.jsp" %>. It shows the page contents. But if I use this line instead: <%@ include file="<%=p%>" %>, it shows exception ! Feb 2, 2012 at 11:18
  • Hi, i have same problem. Getting error File "<%=p%>" not found. If I will use real value it is working.
    – Reddy SK
    Jun 12, 2019 at 12:04
0

For a reason I don't yet understand, after I used <%@include file="includes/footer.jsp" %> in my index.jsp then in the other jsp files like register.jsp I had to use <%@ include file="footer.jsp"%>. As you see there was no more need to use full path, STS had store my initial path.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.