Make delicious recipes!

JSP

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:

  1. Comments :

    1. <!-- This comment is copied to HTML file too -->

    2. <%- - This is a hidden comment visible only in JSP file - -%>


  1. Directives : Directives are used to set global values such as class declaration, methods to be implemented, output content type, etc. They do not produce any output to the client. All directives have scope of the entire JSP file. Their syntax is <%@directive attribute="value" %> . The three directives are

    1. page : Provides info about current page.

Example: <%@page language="java" session="true" errorPage="error.jsp" %>

    1. include

Example: <%@ include file="/header.jsp" %>

    1. taglib : taglib is for using custom tags in 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>


  1. Declarations - between <! and %>

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.


  1. Scriptlets - between <% and %>

  2. Expressions - anything between <%= and %> is printed as such.

However, a semicolon should be contained in the output a expression.

  1. Standard Actions

    1. jsp:include

    2. jsp:param - adds/modifies a param to current request.

    3. jsp:forward

    4. jsp:useBean

    5. jsp:get/setProperty

  2. Custom Tags


JSP Page Directive

  • language=”java/javascript” : Mostly java is used as value in this directive.

  • extends : Used to specify a superclass other than that used by default by Servlet Engine. This should also be done rarely since each Servlet Engine provides some important functionalities for its superclass which are essential for that Engine to work.

  • isThreadSafe :

<%@ 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.

  • false means code is not thread safe. So separate instance will be created. The generate class will then implement SingleThreadModel Interface.

  • Error Page Specfication:

<%@ 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:

Object

Type


Object

Type

out

JSPWriter


config

ServletConfig

request

HTTPServletRequest


response

HTTPServletResponse

application

ServletContext

(Used to set properties for all servlets)


session

HTTPSession

page

JSPBase


pagecontext

PageContext

exception

Exception

(available only on error page)






Calling Java function from JSP page: A java function can be called in JSP using <% and %> tags.
For example: foo() in class Bar can be called as <% Bar.foo() %>

Normally, its a good idea to pass 'pageContext' or 'request' variables when calling any Java function.
This is so because pageContext.getRequest() or 'request' can be used in the function to get several attributes about the page.

Some useful functions in the variable 'request' are:

  • request.getServletPath()
  • request.getRequestURI()
  • request.getRequestURL().toString()



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


Internally, JSP uses cookies to manage sessions. However, if cookies are disabled by browser, then it reverts back to URL rewriting.




Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal