JSP allows mixing of Java code inside HTML using <% and %> tags. Servlet code can also be written inside HTML directly using these tags. JSP files are processed by a JSP engine running inside a web server.
When a request for a .jsp page is generated, the server passes it to JSP engine. JSP engine then generates a class for this jsp page which is compiled into a servlet. This compilation to byte-code happens only the first time, for subsequent requests, the generated byte-code is used (if there are no significant changes in the corresponding jsp file).
Parts of a JSP file:
Example: <%@page language="java" session="true" errorPage="error.jsp" %>
Example: <%@ include file="/header.jsp" %>
Example: <%@ taglib uri="tlds/taglib.tld" prefix="pre1" %>
In the above, if taglib.tld defines a tag called mytag, then due to pre1, it will be used as <pre1:mytag … </pre1:mytag>
Like scriptlets, declarations contain Java language statements, but with one big difference: scriptlet code becomes part of the _jspService() method, whereas declaration code is incorporated into the generated source file outside the _jspService() method. Hence, declarations do not have access to implicit variables lik out, request etc.
Variables declared inside declarations are not thread safe since they will be in class scope and not in _jspService() method scope. Hence all threads will access a single copy of such variables.
However, a semicolon should be contained in the output a expression.
JSP Page Directive
<%@ page isThreadSafe="true" %>
true means that all code is thread safe and servlet engine can create only one copy of instance that will be shared among all threads.
<%@ page errorPage="error_url" %> specifies error_url as error page for this page.
<%@ page errorPage="error_url" %> specifies that the current page is error page.
Include Tags in JSP
The <%@ include %> directive is used to copy static text into the JSP source
code before it is transformed into Java servlet source code and compiled.
The <jsp:include> action causes the servlet engine to invoke another URL
and merge its output with that of the original JSP page.
<%@ include %> directive is performed once, at compilation time, whereas the <jsp:include> action is performed each time a request is made.
JSP:forward vs Redirection
JSP:forward forwards the request to a new page/servlet entirely on the server side whereas the redirection request is first sent to a browser who calls the new page.
JSP keeps references of some commonly used objects. They are:
Calling Java function from JSP page:
A java function can be called in JSP using <% and %> tags.
Session Management is set to true by default in JSP. This can be costly for pages which do not need session because when session management is ON, a session id is sent back and forth for identification during each client server communication. It can be disabled using directives as follows:
<%@ page session=”false” %>
|Email:||(Your email is not shared with anybody)|