Does JSP provide the dynamic behaviour to a web-page? - java

Although I know JSP (basic understanding), I have some doubts on the JSP technology.
Consider the below simple jsp:
<html>
<head>
<title> This is demo</title>
</head>
<body>
<h1> The current time is <%=new java.util.Date()%> </h1>
</body>
</html>
With respect to this, I have some doubts (which I have been keeping at the back of my mind):
1) The basic text of this jsp is same, is it dynamic page because it has JAVA code in it?
2) When a user accesses this jsp page, does the container first execute the java code and replaces the output of java code inside the page?
3) What makes this a jsp page? Do mixing of html and java code make it a jsp?
4) Can the java code (within <% %> ) live independent of html? or they are coupled (the java code has to be present in html page).
They might be basic questions, can anyone help me in understanding them?

Yes it is dynamic because it is computed (even partially) at run time - here the java scriptlet is the dynamic part
Not exactly. JSP is not a template engine. JSP pages are fully translated to java source and then compiled to class files by the servlet container. Then those classes are executed at run time.
the extension .JSP makes it a JSP file. Then it must respect the JSP syntax to be correctly processed by a servlet container.
Reverse the question. Java code normally lies in java source files. A Java class implementing the HttpServlet interface can be directly map to an URL by the servlet container. And Java code can lie in a scriptlet in a JSP
But as you were said in comment, you really should read documentation on that before asking questions here.

1) it's dynamic if it contains any JSP elements, like code snippets, JSP tags etc. If it contains only HTML, then it's pretty static, although if it is processed as a JSP, then the constant response is computed dynamically on each call (safe Caching).
2) yeah much like that. Actually the static text of the JSP goes into out.write() Statements in a Java class; the whole JSP is transpiled to a Java class.
3) funny question. It's all a question of Interpretation. If you name it .jsp or have your web container process it as JSP in some other ways (depends), then you may call it a JSP.
4) this question is not quite clear. The snippets are executed after the static text has been output up to this point.

Related

Should we remove HTML attributes while using Thymeleaf?

I'm studying Thymeleaf and have found out that in almost all examples there are Thymeleaf's tag values as well as standard HTML values like:
<title th:text="#{product.page.title}">Page Title</title>
<link href="../static/css/bootstrap-3.3.7-dist/bootstrap.min.css" rel="stylesheet"
th:href="#{/css/bootstrap-3.3.7-dist/bootstrap.css}"/>
<script src="../static/js/jquery-3.1.1.js"
th:src="#{/js/jquery-3.1.1.js}"></script>
These standard tag values like Page Title or href="../static/css/bootstrap-3.3.7-dist/bootstrap.min.css" etc. are ignoring by controller and don't rendering on the page.
I'm wondering – is it just a good practice to leave them to improve code readability or it is better to remove them to clean up code?
Because for the compiler they are useless and have not any affect to the rendering result.
This depends entirely on your development process.
You could keep the HTML attributes around in the early phases, while you are still trying to lay out the page using just your browser.
But, once you get to a point where you are using automated unit / web testing, you can safely remove the HTML attributes because this testing should always be using a prod-like environment (which would include thymeleaf).

Asynchronous JSP Tag

Is there a JSP tag that allows that part of the page to process asynchronously? If not is there a way to create one?
Something like the following:
<Util:Asynchronous>
<%
DataSource source = ...
%>
</Util:Asynchronous>
... rest of page
I don't want to have to wait for the data source to finish before processing the rest of the page.
Thanks.
You need to use AJAX. Also it is bad coding practice to put your data access commands in your JSP pages. Very susceptible to hacks, not to mention performance hits.
The servlet container must wait that you are page is entirely rendered before sending it back to the client. Thus your requirement makes no sense for me.
If a part of the page takes too much time to process, you could use Ajax to load a portion of your page. There is already pages at StackOverflow about Ajax.

render xhtml file from servlet

I wonder if there is a way to load, interpret and render a .xhtml file from a servlet.
What I want is to have a xhtml file witch contain for example a custom row within a list, to be loaded from a servlet or a bean, to be interpreted, and to obtain the html result.
Sounds to me like you're not in need of Servlets or JSF, but rather JSP (should look familiar, coming from PHP).
Notice that people here will tumble over themselves telling you to not put the logic into the page (and for good reason), but I think with your background, you'll be helped by starting from a JSP only approach. Than start moving the logic to a servlet. You can use a number of techniques to communicate the data between the servlet and the jsp. I like to use
request.setAttribute("someString", valueObject);
In your jsp you than can use:
<%= request.getAttribute("someString") %>
Calling your jsp from the servlet is done via
RequestDispatcher rd = req.getRequestDispatcher("/path.to.your.jsp");
If you really need a .xhtml file (your original question), you can use this last line as well, but it'll be real xhtml, like it would be served from a plain webserver (!= appserver)

How to render HTML form fields and run Java code in background simultaneously in JSP

I have made a JSP page that has form fields and java code in scriplets .I have imported the java code in JSP page and made object to call the function of that java class.
When i execute the JSP, the JSP page remains blank and only when all the Java code has been executed then the html form fields are rendered on the view.
I have two questions:
1.)How can i quick this process and how can i show something like "Loading..." until that html is rendered
OR
2) How can I make HTML fields to appear as soon as JSP page is invoked while letting all the java code simultaneously running in background.(The java class whose object is made in JSP calls webservices and this may take time, meanwhile i want user should start inputing in the form fields).But user cannot see any form fields until the whole Java code has executed.
KINDLY HELP!
You can display a page first and then to load other content through Ajax.

JSP Performance using jsp:include

I am beginning to break apart a large JSP file into a some smaller JSP pages so I can reuse this across other areas of the site.
I can take the approach of leaving as a large Monolithic JSP file that takes params and adjusts it's behavior accordingly. The other approach I can take is breaking it apart so that it's called via jsp:include.
What is the performance concern in creating additional request calls that are dispatched from within the server? Is it better performance wise to keep it as one jsp page?
The jsp:include is a runtime directive unlike the <%# include ... %> directive which happens to be a compile time directive (translation time, actually). A compile time include directive is relatively harmless since JSPs are usually precompiled for production, or in the worst case compiled for every deployment of the application. For this reason, it is preferable to use the compile time directive for static files, since the files are not bound to change at runtime.
The runtime include directive on the other head, if misused, can result in some performance hit, especially for static files being included. This is primarily because the JSP container would have to then fetch the contents of the static file and include them in the response. Hence, reserve the usage of the runtime directive for the really useful scenarios where another servlet or jsp's response is to be included in the response, and not merely to make the code look good.
You should be using:
<%# include file="page.jsp" %>
This adds the content of page.jsp at translation time and there is no overhead.
<jsp:include page="page.jsp" />
Your approach adds the content at runtime and adds a lot of overhead.
Static vs Dynamic include
The include directive, makes a copy of the included page and copies it into a JSP page (the "including page") during translation. This is known as a static include (or translate-time include) and uses the following syntax:
<%# include file="/jsp/userinfopage.jsp" %>
The jsp:include action, dynamically includes output from the included page within the output of the including page, during runtime. This is known as a dynamic include (or runtime include) and uses the following syntax:
<jsp:include page="/jsp/userinfopage.jsp" flush="true" />
Performance Considerations
Static includes affect page size; dynamic includes affect processing overhead. Static includes avoid the overhead of the request dispatcher that a dynamic include necessitates, but may be problematic where large files are involved.
A dynamic include does increase processing overhead, with the necessity of the additional call to the request dispatcher.
http://docs.oracle.com/cd/A97336_01/buslog.102/a83726/keydev1.htm#1015959
http://docs.oracle.com/cd/A97336_01/buslog.102/a83726/genlovw3.htm
Should not create a scenario to load lots of data to UI at a time, that will affect the performance whatever way you implement the jsp. Keep it simple. Understand , how much data a user probable to read in that specific scenario. Keep UIs userfriendly ,businessfriendly and techno friendly.
Calculate mow much static/dynamic contents are there and use include as per it's contexts. Use pagination with 10 - 50 records if you want to display records.Better use any framework to address the basic issues instead of addressing it from scratch.
if you don't need to use session in any jsp s make it as session false
<%# page session="false" %>
For include directive, JSP Engine adds the content of the inserted page at translation phase, so it does not have an impact on performance.
For include action, JSP Engine adds the content of the inserted page at run time which imposes extra overhead.

Categories

Resources