JSP page included in servlet not rendering satisfactorily - java

I have two jsp files (header.jspf and footer.jspf) that I want to include in a page produced by the doGet method of a servlet (Menu.java). I'm doing this by use of the RequestDispatcher.include() method. The header.jspf file only contains html so it renders fine. The footer.jspf file, however, contains both EL and JSP tags, neither of which render on the servlet page produced by Menu.java.
Looking at the source code of the page produced by Menu.java I understand that the reason for this problem is that the EL and JSP is not evaluated prior to being included in the servlet so it is just presumed by the browser that it is html.
While I'm guessing that what I'm trying to do may be poor (and deprecated) practice, I'd just like to find out if there is a way to get an included jsp file to render in a servlet just as it would in a jsp page when it contains EL and jsp tags?
I'm thinking that this question is general enough not to require my code to be posted, however if I'm wrong please tell me so and I will update with my code.

Not sure if there is a way to have RequestDispatcher.include() files processed, but including the footer explicitly will very likely work:
<%#include file="footer.jspf" %>

Related

If I have html elements in JSP then what is order of execution?

What will execute first body elements or head elements...
Head
Body
scriplet
If I get what you're asking, each element in the JSP file is processed in the same order as it appears from top to bottom of your code.
Obviously, the scriptlets embedded in your JSP are executed to create the HTML. The scriptlets, and other server-side-executable stuff in the JSP is executed top to bottom.
(The JSP is not aware of the HTML elements it is generating. It processes the JSP / JSTL syntax embedded in the JSP file, and treats the rest as text to be copied into the document sent to the browser. That's why you can, in theory, use JSPs to generate any text-based document.)
The generated HTML is then sent to the browser ... which is where any client-side javascript embedded in the HTML will be executed.
JSPs are servlets that add syntactic sugar to facilitate developers. All JSP compile to servlet first and at runtime class files of that compiled JSP.
Following diagram explains the JSP compilation/execution in detail:
HTML/JS are executed on browser(client side) once server side script finalize the response as HTML.

Syntax Error in my JSP Eclipse

Why I have this error in my JSP file
My JSP:
Error Message:
Syntax error, insert "}" to complete MethodBody
A JSP has been parsed incorrectly by Eclipse. It has mistreated curly braces used by some javascript code that was rendered without <script> tag.
The <script> tag should be placed inside the <head> or <body> tags.
JSP files are compiled to a servlet. If you declare a method inside the JSP page using scriptlets, the method body is opened with { and should be closed with }, but somwhere in the code you might find /*}*/, or even worse missing <%}%>. The example of such errors (redundant }; in the Java code) you can find here.
To resolve this and other possible errors caused by spaghetti code inside the JSP, recommended way is to not use scriptlets and move Java code to a servlet. Struts is MVC framework that provided additional to servlets features for rendering JSP pages. You have to move Java code from JSP page to Struts controllers and access it by expression languages like JSTL, OGNL, etc. Return JSP page as a result/forward of the action/controller execution/ method call. You can also call methods of the model/controller directly while the page is rendered. This behaviour is out of the scope in which MVC pattern is used.
If you need more information about separation of concerns while developing a web application, particularly related to how to avoid Java code in JSP see How to avoid Java code in JSP files.

How to include an html fragment in a jsp that loads at run-time?

I need to include content in a JSP without using the include directive.
It is easy to do a server-side include with with <%#include file="includeMe.htm" %> but the content of includeMe.htm gets added to the JSP before the JSP is converted into a Servlet by the container. This means that if includeMe.htm gets modified, the changes are not reflected in the generated .java Servlet file. I'm tired of going into Tomcats generated files directory to manually delete the generated java and class files or re-deploy my app every time the included file changes.
Do I need to write a code block to just read in data from the text file line by line and then write it like this?
<%
while( not done reading from file ) {
String line = scanner.nextLine();
response.getWriter().println(line);
} %>
Is there an easier or cleaner way?
Take a look at answers here.
You can also use <c:import>:
2) The <jsp:include> standard action
<jsp:include page="header.jsp" />
Dynamic: adds the content from the value of the page attribute to the current page at request time. Was intended more for dynamic
content coming from JSPs. 3) The <c:import>
JSTL tag:
<c:import url=”http://www.example.com/foo/bar.html” />
Dynamic: adds the content from the value of the URL attribute to the current page, at request time. It works a lot like
<jsp:include>, but it’s more powerful and flexible: unlike the
other two includes, the <c:import> url can be from outside the
web Container!
Regarding reading a file into string. If the file is small do it one line:
String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
You can use .tag files for reusable content or do <jsp:include> (INCLUDE dispatch) to another JSP.
However Tomcat is able to re-compile JSPs when any of the included files got changed:
Recompile JSP when included page changes - Jasper 2 can now detect when a page included at compile time from a JSP has changed and then recompile the parent JSP.
Also note, that the common practice is to use .jspf (JSP fragment) extension for files included via <%#include %>.

Does Spring MVC 3.x support partial views (html/jsp) like that of ASP.NET MVC3/4?

Q1: Does Spring or any opensource java UI framework support partial views like that in asp.net mvc?
For example in my main index.html (or _layout.cshtm per asp.net mvc3 spec)
I would have the folllowing code:
<span id="logindisplay">#Html.Partial("_LogOnPartial")</span>
where #Html is a helper to display a partial view for _LogonPartial.cshtml which just injected it's html view contents into the page?
Q2: If this is supposed If I want to display a bunch of partial views it would be helpful to display them concurrently in parallel to improve performance. Very similar to what linkedin is doing using dust and fizzy? http://engineering.linkedin.com/profile/engineering-new-linkedin-profile
Q3: Is fizzy available as open source like that of dust?
If you want to include content of a page into another page, by adding some code on the page itself, you should compare asp with jsp, not ASP.NET MVC* with JEE - Spring MVC
So, an equivalent to <span id="logindisplay">#Html.Partial("_LogOnPartial")</span> on a jsp would be one / all of the following
On your jsp, include content from another jsp using <%# include file="../includes/inner-content.jsp" %>. This is what is called a static include. The source of the included jsp is included and made part of the parent jsp, before the jsp is compiled. If you use an IDE, it will check to ensure the included jsp does infact exist at the path specified, relative to the location of the jsp in which you are adding the include. Technically this is a JSP Directive. The jsp being included could just be a fragment, and not addressable from the outside world (could be hidden inside WEB-INF)
You can also use what's called a Dynamic include <jsp:include page="someJSP.jsp" />. In this case, the included JSP should be addressable from the browser and should be capable of being rendered independently. When the server is executing the servlet to render the parent JSP, it stops when this tag is seen, and starts executing the servlet for the included jsp, the output obtained from the inner jsp execution is then merged to the output of the parent jsp, and processing of the parent jsp is resumed.
A third option would be to use Core JSTL taglib's <c:import url=""/>. This works just like option 2 above, except it also allows you to import a page / content from a url that lives outside your application. Basically you can mention a path to a jsp, or relative URI to a servlet mapping in your application, or a URL to an external page.
Now, I suspect this is not really what you want to do, if you are comparing with what Linkedin is doing. You want to mashup content from sources in your own application, and compose your page. You also want to do this in an asynch manner so as to keep load times in check. In this case, you HAVE to use JavaScript and Ajax. All the mechanisms described above are for server rendered pages (All HTML is created before the page is rendered in the browser). Just like #HTML. You need to create a simple framework / use an existing one, where once a page loads, it fires asynch ajax calls to the server to get content for specific areas on the page and renders the returned HTML in those specific areas.
Hope this helps.
Do let me know if I've misunderstood your question.

Java code in jsp comment. How it works?

Theres the code structure:
jsp code
<%
java code
%>
jsp code
So, how java-code works in jsp? Can i implement chenges without rebuild?
The answer depends on what you mean by "rebuild." Clearly, for something to run on the JVM, it has to be compiled. However, the JSP container does that for you automatically: it sees that the file has changed, and recompiles it.
Actually, it does a little more than recompile: it first translates the JSP into pure Java code that implements the Servlet API, then it compiles that Java code. The static text in the JSP (eg: <html> is turned into println() calls. The JSTL and other tag references are transformed into Java code that instantiates and invokes a tag handler. Scriptlets are inserted verbatim.
You don't say what JSP container you're using, but most of them will let you examine the generated servlet. Tomcat, for example, stores the generated code under the work directory.
http://www.exampledepot.com/egs/javax.servlet.jsp/code.html
Changes will be applied just by reloading the page on browser, since it's JSP, which means that the page will be compiled everytime it's loaded.
If you are using Tomcat you can see generated java codes of the JSP in folder: TOMCAT_HOME/work/localhost
It's not wise to use java code on a JSP code because if there's hard to identify syntax error and if there is one, the entire page won't be able to be loaded at all. Unlike PHP which will load until the point where there is syntax error.
<% and %> are not comments. They signal the beginning of a scriptlet, i.e. java code. If you're using Tomcat, any changes to the jsp files are noted and rebuilt without you having to do anything else.

Categories

Resources