Include tags within a JSP block (making readable code)

I've created a page that includes segments of other pages based upon a condition passed via a parameter. Arg! Let me start over.
My code looks something like this...
<%
   String foo = request.getParameter( "action" );
   if ( foo == null ) {
%>
      <jsp:include page="error.html" flush="true" />
<%
   } else if ( foo.equals( "add" ) ) {
%>
      <jsp:include page="add_user.jsp" flush="true" />
<%
   } else if ( foo.equals( "help" ) ) {
%>
      <jsp:include page="help.html" flush="true" />
<%
   } else {
%>
      <jsp:forward page="display_status.jsp" flush="true" />
<%
%>And obviously, this is kind of hard to read. Is there a way that I can do includes and forwards within the context of the JSP block itself? I'm looking for something like...
<%
   if ( foo == null ) {
      this.include( "error.html", "true" );
   } else if ( foo.equals( "add" ) ) {
      this.include( "add_user.jsp", "true" );
   } ... else {
      this.forward( "display_status.jsp" );
%>...which should hopefully be a lot more readable.

for example
<jsp:include page="text.jsp" flush="true" />
is equivalent to
<% JspRuntimeLibrary.include(request, response, "text.jsp", out, true); %>
ugh...
There should be more simple solution.

Similar Messages

  • Flush in the jsp:include tag

    should I state flush="true" every time I include a jsp using <jsp:include> tag in a jsp pages? For example I am trying to include a menu bar on the top of every jsp pages. Should I flush the buffer every time? if not, will it affect the performance?
    Thank you
    Cal

    Hi,
    This is a known problem, ��flush before you include�� limitation in JSP 1.1.
    As a result, you have to state flush="true" every time you include a jsp using <jsp:include> tag if you are using JSP1.1.
    Fortunately, it is fixed in JSP1.2. The flush attribute controls flushing. If true, then, if the page output is buffered and the flush attribute is given a ��true�� value, then the buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The default value for the flush attribute is ��false��.
    Gary Wang
    Developer Technical Support
    SUN Microsystems
    http://www.sun.com/developers/support/

  • Scriptlet within a jsp:include tag

    In Jrun 2.3.3 you could include a scriptlet within a <jsp:include> tag like this:
    <%
    String language = "E";
    %>
    <jsp:include page="/Conditions<%= language %>.html"/>
    Since I have to programme for four languages on our site, I used this convenient way of including different language pages on the fly all over the site.
    I then had a problem with 2.3.3 so I got 2.3.8 and discovered that all of these jsp:includes with scriptlets didn't work any more.
    I hastily uninstalled 2.3.8 and have continued with 2.3.3 ever since, thinking that they would sort this out in 3.0., which they didn't.
    Presumably to deal with this when I want to upgrade JRun I will have to use a load of if{} statements instead, but for four languages I will need four if {} statements which is incredibly long-winded compared to the single line of code above.
    <%
    if(language.equals("E")){
    %>
    <jsp:include page="/ConditionsE.html"/>
    <%
    %>
    Can anybody help:
    1) by giving me a neater solution that the one I've come up with.
    2) by explaining what happened at JRun to cause this change.
    Thanks.

    I could be wrong, but at least with WSAD (WebSphere), I couldn't do this:
    <html:text maxlength="14" size="10" property="proofOfClaim[<%=memberAccountIndex%>].sentDate%>"/>
    but could do this:
    <% String property = "proofOfClaim[" + memberAccountIndex +"].sentDate"; %>
    <html:text maxlength="14" size="10" property="<%=property%>%>"/>
    I think the reason is that since <html:text> is a custom tag, any scriptlets <%=...%> must be the ENTIRE value, and not be inserted in the middle of a value, for an attribute. Try it out. :)

  • Problem with flush=true in the jsp:include tag

    Hello
    I have deployed a JSP based application , based on the apache struts framework. The web server is Sun One Webserver 6 Service Pack4, on a windows 2000 machine
    The same application was tried on SunONE application server 7, and it works without any problem. However in this case the following error , on the page
    javax:servlet.jspexception:Illegal to flush within a custom tag
    Does this mean flush="true" is not allowed within custom tags
    Or is there a workaround for this
    Any help is appreciated
    thanks
    - Aniruddha

    Hi,
    This is a known problem, ��flush before you include�� limitation in JSP 1.1.
    As a result, you have to state flush="true" every time you include a jsp using <jsp:include> tag if you are using JSP1.1.
    Fortunately, it is fixed in JSP1.2. The flush attribute controls flushing. If true, then, if the page output is buffered and the flush attribute is given a ��true�� value, then the buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The default value for the flush attribute is ��false��.
    Gary Wang
    Developer Technical Support
    SUN Microsystems
    http://www.sun.com/developers/support/

  • How to dynamically include jsp files using jsf el in include tag ?

    Hi,
    I'm trying to get the jsp file name from my backing bean to use it in an include tag but I can't make it work
    When using this piece of code I can get the generated path
             <h:outputText value="/_partial/#{myBean.jspName}.jsp"/>whereas in this code the jsf el is not replaced by this value.
            <%@ include file="/_partial/#{myBean.jspName}.jsp" %> Have you any idea of how to solve this ? I'm currently developing for J2EE 1.4 platform.     
    Thanks for your help,
    Regards.

    If you're using JSF 1.2 and JSTL 1.2 at a JSP 2.1 environment, then you can just use <jsp:include> with unified EL.
    If you're using JSF 1.1, then you may find this article useful [http://balusc.blogspot.com/2007/01/dynamic-jsf-subviews.html]

  • Are not interpreted JSTL tags in a JSP page including in a servlet.

    Hi people,
    I have a project where una page (index.jsp) includes a servlet (MyServlet), that consult a persistence class and get a List of objects (Users),      
    then the servlet passes the List to a Request object and includes another JSP page (showUsers.jsp). And this is conceptually correct, but don´t works, the JSTL tags are not interpreted in showUsers.jsp.
    This is my code...
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <c:out value="Show me some things index.jsp"/>
    <div style="border-color:red; border:solid; padding-left:60px">
          <jsp:include flush="true" page="pepe/MyServlet"/>
    </div>
    </body>
    </html>...and the Servlet...
    public class MyServlet extends HttpServlet
         public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
              UserManager um = new UserManager();
              List users = um.getUsers(); //This use Hibernate to return a Users List
              request.setAttribute("users", (ArrayList) um.getUsers());
              request.getRequestDispatcher("/showUsers.jsp").forward(request, response);
    }...Finally, we have the showUsers.jsp file....
    <c:out value="Show me some thing showUsers.jsp"/>
    <table>
         <tr>
              <th>ID</th>
              <th>Name</th>
              <th>e-Mail</th>
              <th>Type</th>
         </tr>
         <tr>
              <c:foreach items="${requestScope.users}" var="user">
                   <td><c:out value="${user.id}" /></td>
                   <td><c:out value="${user.name}" /></td>
                   <td><c:out value="${user.email}" /></td>
                   <td><c:out value="${user.type}" /></td>
              </c:foreach>
         </tr>
    </table>This i get as result page...
    ID       Name       e-Mail       TypeFinally, this is the code of showUsers.jsp...
    <c:out value="Show me some thing showUsers.jsp"/>
    <table>
         <tr>
              <th>ID</th>
              <th>Name</th>
              <th>e-Mail</th>
              <th>Type</th>
         </tr>
         <tr>
              <c:foreach items="[src.User@18f729c, src.User@ad97f5, src.User@d38976, src.User@1e5c339, src.User@17414c8, src.User@7a17]" var="user">
                   <td><c:out value="" /></td>
                   <td><c:out value="" /></td>
                   <td><c:out value="" /></td>
                   <td><c:out value="" /></td>
              </c:foreach>
         </tr>
    </table>Somebody can help me?
    Many thanks,
    Gonzalo

    Thanks you all guys,
    I appreciate very much your help. In response to everyone ...
    BalusC wrote:
    Is JSTL taglib declared in top of that JSP page? I don't see it back in the posted code snippet. In this example I stuck...
    request.getRequestDispatcher("/showUsers.jsp").forward(request, response);By mistake, but this is just a test, the original line of my servlet is...
    request.getRequestDispatcher("/showUsers.jsp").include(request, response);As you can see, both (the servlet and the showUser.jsp file) are included in the index.jsp file. So the header...
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>...in the index.jsp file should works (I hope so).
    njb7ty wrote:
    I assume in your web.xml, you have ''pepe/MyServlet' defined as a servlet tag and servlet map tag? Without that, I don't think your JSP will find the servlet. I'm >not sure you need it in web.xml since I never call a servlet from a JSP page.
    I suggest putting System.out.println() throughout your servlet code and out.println() in your JSP pages to see exactly what is called and when.
    As a general rule, JSP files are to display data only, and submit back to a servlet. The servlet does all the business logic and dispatches to the appropriate >JSP page. The JSP shouldn't have any business logic. Including the servlet looks kinda like including business logic. Actually, in a MVC design, your >presentation, control, busines, and database layers have their own isolated responsibilities.
    I suggest the servlet put data as one java bean in request scope via request.setAttribute() and dispatch to the JSP page. The JSP page gets the data via ><useBean> tag. The JSTL gets the variables from the useBean tag and uses the data from there to display it. Really, this is my web.xml file...
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
         <servlet>
              <servlet-name>MyServlet</servlet-name>
              <servlet-class>src.MyServlet</servlet-class>
         </servlet>
         <servlet-mapping>
              <servlet-name>MyServlet</servlet-name>
              <url-pattern>/pepe/MyServlet/*</url-pattern>
         </servlet-mapping>
    </web-app>Regarding putting System.out.println() and out.println(), i did it and thats works.
    Respect of your last comment, I am not a expert in MVC, but I understand that the view layer can make calls to the Controller layer, I am wrong?
    evnafets wrote:
    It's not. However thats not code, but the generated HTML.
    As Balusc pointed out it's the result of running this JSP page without importing the tag library at the top.
    Because the tag library is not declared, it treats the <c:forEach> and other tags as template text, and basically ignores them.
    It then evaluates the ${items} attribute as an expression in template text, calling toString() on it.
    Cheers,
    evnafets      The file showUsers.jsp are included into the index.jsp page, that's have the header taglib. Could this works?
    BalusC wrote:
    njb7ty wrote:
    By the way, I dont think this is the correct format for the foreach tag:
    <c:foreach items="[src.User@18f729c, src.User@ad97f5, src.User@d38976, src.User@1e5c339, src.User@17414c8, src.User@7a17]" var="user">You're right friend.
    And that's my problem. Any ideas?
    Thanks everyone,
    Gonzalo

  • Jsp include tag 1.1

    Hi all,
    i am getting the following error if my code snippet has a <jsp:include> tag
    org.apache.jasper.JasperException: Invalid jsp:include tag
    i am using VAJ3.5.3 and i modified the hosts file to support jsp1.1.
    But when i change to jsp1.0 the code works fine and fetching me the page contents.
    Need inputs.
    Regards
    Ramesh Kesavanarayanan

    Hi kesavramesh,
    I think some time ago, you're looking for something like this
    - Can I use jsp includes inside the taglib ?-
    See point 1 and 2.
    -- Paul
    1. link:
    http://www.jguru.com/faq/view.jsp?EID=115446
    2. text from the link:
    Normally when you call one JSP page from another you have no significant access to the result of the called JSP. So if I have
    Hi, I like nice clean JSP output
    <jsp:include page="gobbledegook.jsp" flush="true"/>
    Doh!
    When it would be much nicer to be able to do this:
    <%@ taglib uri="blahblah" prefix="myTags"%>
    Hi, I like nice clean JSP output
    <myTags:cleanUp>
    <jsp:include page="gobbledegook.jsp" flush="true"/>
    </myTags:cleanUp>
    Wahoo!
    This can be accomplish with the tag class:
    public class TakeOutTheTrashTag extends BodyTagSupport
    public int doStartTag() throws JspTagException
    return EVAL_BODY_TAG;
    public int doAfterBody() throws JspTagException
    try {
    if (bodyContent != null) {
    String messy = bodyContent.getString();
    String clean = takeOutTheTrash(messy);
    bodyContent.getEnclosingWriter().print(clean);
    } catch (IOException ie) {}
    return EVAL_PAGE;
    public int doEndTag() throws JspTagException
    return EVAL_PAGE;
    private String takeOutTheTrash(String messy)
    String cleanedUp = messy;
    return cleanedUp;

  • Jsp include tag giving exception in Jboss

    Hi,
    I have a jsp page with the include tag
    <jsp:include name="w.jsp" />
    When the jsp is compiled in Jboss 3 its givin an exception as
    \Temp\Jetty_0_0_0_0_7001__blsapp\jsp\agent\A_jsp.java:220: cannot resolve symbol [javac] symbol : method include (javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.String,javax.servlet.jsp.JspWriter,boolean) [javac] location: class org.apache.jasper.runtime.JspRuntimeLibrary [javac] JspRuntimeLibrary.include(request, response, "w.jsp", out, false);
    Have any one come across this error pls help me in solving this

    check ur sytnax dear its page not name

  • Jsp:include tag does not preserve encoding of the text

    hi
    I am using Apache web server and Tomcat 4.1.
    I have a jsp:include tag (in say test1.jsp) and I am trying to include a data1.html file. This html file contains UTF-8 encoded text. When this JSP is executed, the UTF-8 text is displayed as garbage characters.
    I have included following in the JSP
    <%@ page contentType="text/html; charset=UTF-8" %>
    I have included following in the info.html
    <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
    However if I rename the info.html to data1.jsp and then include, the UTF-8 text gets displayed correctly.
    I suppose, the JSP engine does not interprete the content of the html file correctly according to its encoding.
    Where as if it is a .jsp, then it is int erpreting it appropriately?
    Can anybody throw some lite on this?
    regards
    haresh
    sample files.......
    test1.jsp..........
    <%@ page contentType="text/html; charset=UTF-8" %>
    <%
    response.setContentType("text/html; charset=UTF-8");
    %>
    <HTML>
    <HEAD>
    <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
    </HEAD>
    <BODY>
    <jsp:include page="data1.html" flush="true"/>
    </BODY>
    </HTML>
    data1.html.....
    <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
    <br>
    &#2361;&#2366;&#2340;&#2368; &#2328;&#2381;&#2351;&#2366;&#2354; &#2340;&#2375; &#2340;&#2337;&#2368;&#2360; &#2344;&#2381;&#2351;&#2366;.
    <br>

    Your generated HTML will be flawed.
    The output of the included JSP is included in the outputstream of the enclosing one.
    Browsers will normally skip any illegal tags when encountered. This included your complete html headers from the included jsp and may (depending on the browser) include all code after the html close tag from that jsp.
    Stricter browsers may simply refuse to display anything at all.
    Read up on what the include mechanism exactly does and on wellformed HTML.

  • JSP include tag not showing page content

    Below is my code for a JSP page that uses JSP:include tag to include footer.jsp. The footer page does not display on show_email_entry.jsp. The error I am getting is variable emailAddress cannot be resolved. What am I doing wrong?
    Here is my code.
    show_email_entry.jsp
    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <%@ include file="../includes/header.htm" %>
    <body>
    <%
    String firstName = request.getParameter("firstName");
    String lastName = request.getParameter("lastName");
    String emailAddress = request.getParameter("emailAddress");
    %>
    <h1>Thanks for joining our email list</h1>
    <p>Here is the information that you entered:</p>
    <table cellspacing="5" cellpadding="5" border="1">
    <tr>
    <td align="right">First name:</td>
    <td><%= firstName %></td>
    </tr>
    <tr>
    <td align="right">Last name:</td>
    <td><%= lastName %></td>
    </tr>
    <tr>
    <td align="right">Email address:</td>
    <td><%= emailAddress %></td>
    </tr>
    </table>
    <jsp:include page="footer.jsp" flush="true" />
    <p>To enter another email address, click on the Back <br>
    button in your browser or the Return button shown <br>
    below.</p>
    <form action="../email6/join_email_list.html" method="post">
    <input type="submit" value="Return">
    </form>
    </body>
    </html>
    FOOTER.JSP page:
    <%@ page import="java.util.Date" %>
    <p><i><%= emailAddress %> was added on <%= new Date() %>.</p>

    The [url http://java.sun.com/products/jsp/syntax/2.0/syntaxref209.html#1003408]@include directive and the [url http://java.sun.com/products/jsp/syntax/2.0/syntaxref2020.html#8828]<jsp:include> have two different functionalities.
    From the [url http://java.sun.com/products/jsp/syntax/2.0/syntaxref20.html] reference page:
    Include Directive : Includes a resource of text or code when the JSP page is translated.
    <jsp:include>
    Includes a static resource or the result from another web component
    (italic emphasis is mine)
    Basically the @include directive is like "copy and paste this file into the JSP and then compile"
    jsp:include is "run this web page, and add the result of that to my current response"
    So jsp:include can only include complete standalone JSPs.
    Do you see now why it would throw an error message with your example?
    Cheers,
    evnafets

  • Nesting a BC4J JSP data tag within a Struts tag?

    Has anybody tried nesting a BC4J JSP data tag within a Struts JSP tag? I can't get it to work. For example,
    <logic:equal name="sort" value="<jbo:ShowDefinition datasource='ds' dataitem='CustomerName' definition='ColumnName' />">
    blah blah blah
    </logic:equal>

    Try using single quotes in the value=' ' and double quotes in the jbo tag usage.

  • JSP error page does not displayed on its own, includes in the original JSP

    Problem Description: - Exceptions in a Condition cause pages to fail to render.
    The actual issue is, the JSP error page does not displayed on its own, included in the original JSP Page when exception occurs.
    Problem Cause: As per the JSP specification when jsp content reached the buffer size (default 8KB) the page being flushed (Part of condent displays). The default �autoFlush� value is true.
    When the page buffer value is default size (8KB), and if any exception occurs after flushing the part of the content, instead of redirecting into error page, the error page content included in the original page.
    If i specify autoFlush="false" and with default buffer size, at the runtime if the buffer size is reached, i am getting stackoverflow error.
    To solve the above problem we can make it autoFlush=�false� and buffer=�100KB�. But we can�t predict the actual size of the page.
    I found in one of the weblogic forum as no solution for this issue. Ref.
    http://support.bea.com/application?namespace=askbea&origin=ask_bea_answer.jsp&event=link.view_answer_page_clfydoc&answerpage=solution&page=wls/S-10309.htm
    Please provide me any solution to resolve the problem.

    Error-Page tags work best with an error.html pages. If you have an error.jsp page what I would do, and I have, is wrap my classes and jsp pages in a try catch block where you forward to the error jsp page and display anything you want. YOu can also do this with if else statements. I have used the tomcat error pages before but when I've implemented them I used java.lang.Exception as the error to catch not Throwable. I don't know if this would make a difference or have anything to do with your problem.

  • How to use GX tags in a JSP?

    Hi, everybody!
    Our company have got a NAS 2.1 application (with AppLogic's and HTML templates) that it needs to be migrated to J2EE model. In this moment, we can't cover a complete migration from AppLogic to a J2EE, and furthermore, we have to maintain all the presentation layout and keep GX tag's in our templates.
    From my point of view, AppLogic's must be translated into Servlets and HTML-template files could be renamed in JSP files, keeping GX tags.
    Nowadays we are making this partial migration, but we've realized that JSP engine doesn't evaluate correctly JSPs with GX tag's.
    Example
    Servlet:
    TemplateMapBasic tmpMap = (TemplateMapBasic) GX.CreateTemplateMapBasic();
    tmpMap.putString("nombre","Alberto");
    req.setAttribute("GX_MAP",tmpMap);
    // Call JSP
    getServletContext().getRequestDispatcher("/example.jsp").forward(req,res);
    JSP:
    %gx type=cell id=nombre%
    %/gx%
    Result:
    %gx type=cell id=nombre%
    %/gx%
    Insteof of appearing Alberto, it shows the tag.
    Does anybody know what it is happening?
    Do I need to include some tag library into the JSP?
    Thanks in advance.
    Alberto
    PD: We are using iPlanet Application Server 6.0 SP4.

    Hello again,
    Just tried this and got the same problem, copy the file into your actual web application so the link becomes
    continue
    Or put in a sub directory as you see fit.
    I have never tried to open files on the pc in this way, only files under my web site, but it seems as though the browser will not allow the execution of files on your computer.
    Thinking about it, javascript is not able to execute such processes either, it is a security constraint.
    Would you want websites to be able to execute jobs on your local PC??
    I think not!!

  • Catching an exception within a JSP

    Hi all,
    I need to catch an exception in a JSP before the errorPage handles it. Have tried with try-catch but it seems like the exception is thrown to the errorPage anyway. Do you know of any technique to handle this.
    In other words: I would like to specify program logic for one exception within the JSP itself. Let us say Customer.Authenticate() throws an Exception when the given e-mail is not included in customer-DB. I would like to append logic instead of being redirected to errorPage.
    Anyone done this, can it even be done?
    ,Chr

    I would try keeping the error page as it is and still adding the logic in the catch block. If that does not help, then remove the error page and use <jsp:forward> (after your logic) in the catch block to explicitly go to the error page.
    or if you want to process that logic anyways in case of an exception, try adding your logic in the finally block.

  • How to use Flex tag library in jsp

    Hi All,
    We are planning to use flex in our J2EE application in order
    to show some charts. Our j2ee application is in JSP,Struts. I have
    searched on google about this and found that I can include flex 3
    tab library in the jsp in order to use mxml tag in the jsp. I have
    written a sample program in the jsp which uses flex.
    <%@ taglib uri="FlexTagLib" prefix="mm" %>
    <SCRIPT LANGUAGE="JavaScript">
    function showMessage() {
    message.value = "Use this application wisely";
    function hideMessage() {
    message.value = "";
    </SCRIPT>
    <mm:mxml border="5" onmouseover="showMessage();"
    onmouseout="hideMessage();">
    </mm:mxml>
    <TABLE>
    <TR>
    <TD><input type="text" name="message"
    size="50"></TD>
    </TR>
    </TABLE>
    I have added entry inweb.xml. See below,
    <taglib>
    <taglib-uri>FlexTagLib</taglib-uri>
    <taglib-location>/WEB-INF/tlds/flex-bootstrap.jar</taglib-location>
    </taglib>
    Following are the problems that I am currently facing.
    1) Could anybody let me know whether the entry of
    <taglib> tag is correct in the web.xml. Is it the correct
    jar(flex-bootstrap.jar) that I should include?
    2) If yes, from where to download it? or
    If no, what is correct jar that i should include and how?
    your response will be highly appriciated as this is the major
    requirement in our project?
    Thanks ,
    Chandan

    Hi,
    Pleas find details at the URL below.
    http://labs.adobe.com/wiki/index.php/Flex_2_Tag_Library_for_JSP
    Hope this helps.

Maybe you are looking for