Java code in jsp comment. How it works? - java

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.

Related

Can you change the text content of a JSP file by code (on the fly)

Lets say I have a jsp page in my webapp. The code inside it displays some info.
Can I change the code (text) inside this jsp using a Rest Controller. I want to dynamically replace its code by opening the file and changing its content.
I want to change file content via http request:
"/api/change-jsp?newcontent=sometext&file=example.jsp"
--> The corresponding rest controller now does his job.
Will that work or not ?
Ps: 'newcontent=sometext' contains jsp code (EL)
It won't work — jsps are compiled at runtime by the servlet container when the app starts. The JSP compiler will not expect the jsp to change.
But why change it in the first place? Just make the jsp ask for the information it needs.

JSP page included in servlet not rendering satisfactorily

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" %>

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 correctly call my Java code from a JSP project in Eclipse

All I'm trying to do is make calls to my Java code from my .jsp pages. I have written some .jsp pages for a webapp, but as the project grows I want to start putting some of the code into .java classes or servlets.
Why doesn't Eclipse "see" everything in my /src or /lib /web or /WebContent or /WebContent/src or /WebContent/WEB-INF/lib or /Webcontent/WEB-INF/src as usable? After all, it "sees my sql jdbc .jar files and I can use them as soon as they are in /WebContent/WEB-INF/src (and not any other folder). But I wrote classes which are in packages, my code isnt in .jar file form, so eclipse is not picking up on them.
You need to have the Java EE version of Eclipse installed, and create a Java Web Application, which will allow you to set up a web.xml.
Eclipse JEE which I have installed to set up web projects is:
http://eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplerr
Your web.xml will definte your web servlets, which can be JSP's or Java classes that extend a servlet implementation.
This has less to do with your IDE (eclipse) than it does with Java Web Applications.
You should read this documentation for starters from Oracle about web apps, and go from there on setting up an IDE:
http://docs.oracle.com/javaee/1.4/tutorial/doc/WebApp.html
Edit based on user feedback
Import should be to class level:
<%# page import="fully.qualified.SomeClass" %>
Then from your JSP code:
<%
SomeClass someClass = new SomeClass();
someClass.helloWorld();
%>
Edit 2 based on feedback
Try this link to do using page include: http://www.coderanch.com/t/286168/JSP/java/Calling-Java-classes-JSP-page
Alternative Approach
Add a new class to your web.xml, where you want to send your request/form data to. So your JSP would be 1 servlet, your other java class would be your other servlet.
On your JSP, create a form that has an action of your new "TestProgram" servlet
This answer to this question has nothing to do with my setup or import statements. The correct answer is that eclipse doesn't make it clear what code it "sees" (or that its compiling) when it runs the project. On my JSP page, it showed it as recognizing the call to my java code. However, when I ran the page, it was still compiling the older version of my java code, that didn't have my latest changes. I've found the best way to guarantee that the newest version of my referenced java code is compiled when I run my .jsp program (that calls the java) is to go to "Project->Build All" then go to "Servers", right click "Publish". If Publish isnt an option, and it thinks its already synchronized, then you may have to change the .jsp page, save it, then "Publish" will become active again.

Categories

Resources