Elements of Jsp
Elements of jsp are specified like an element in html or xml, i.e. with opening and closing tags. There are many types of elements in jsp. Here is the list of these elements.
- Directives
- Scriptlets
- Actions
- Expressions
- Comments
- Declarations
- Template Text
These elements change the servlet generated by the container. Definitely these are used to change the behavior of dynamic contents of a jsp. Let us see each of these elements in detail:
Directives:
These affect the overall structure and behavior of a jsp. There are three types of directives
- Page: Usually specified at the beginning of page and affects the entire page. Examples are import, extends, session, info, isErrorPage, language, errorPage, autoFlush, contentType, buffer, isThreadSafe.
- Syntax : <%@page attributename=”value”%>
e.g. <%@page import=”java.lang.io”%>
- Include: Include another file in this jsp. During translation phase of jsp, this file is included and complete jsp is prepared. This is a static include, the dynamic include is discussed later. In static include, the file is inserted in the jsp, while in dynamic include the request is forwarded to the included page to include execution response.
Syntax : <%@include file=”filename”%>
e.g. <%@include file=”com.constants”%>
- Taglib: This directive is used to make custom tag library use possible. In this directive, the prefix and uri are identified.
Syntax : <%taglib Uri=”path” prefix=”prefixToBeUsed”%>
e.g. <%taglib Uri=”WEB-INF/mytaglib.tld” prefix=”mytaglib”%>
Scriptlets:
Scriptlets are the Java statements included in jsp. To differentiate these contents, the statements are included in <%…………%>. When the translation activity happens, these snippets are inserted in the jspService() method. As the service method is executed on each request, these snippets are also executed for each request. When you are using the jsp variables in these scriptlets, you need to keep this thing in mind that this code is executed at server side. Hence those variables needs to be available at the server side. Any mix up of client side variable in scriptlet will result in compilation problem or garbage value processing.
Actions:
Actions are taken when a request hits a jsp page. So, the actions are request processing instructions to the web/servlet container. Below are the jsp action examples with their use.
- <jsp:include>: Dynamic inclusion of a page. The response of the included page is included in final response.
- <jsp:forward>: Redirecting request to n different page. In include, the response is not committed, while here, the response is committed and a new request is sent.
- <jsp:param>: Parameters to be passed inside include and forward tags to make available of the new jsp page through request object.
- <jsp:useBean>: Integrating java beans with jsp page to transfer data through these beans and request objects.
- <jsp:getProperty> and <jsp:setProperty>: Along with useBean, these are getter/setter methods of the attributes.
- <jsp:plugin>: Browser will execute an applet or a bean based on this tag.
Expressions:
Evaluate the expression and send the value as a part of response to client. The expression inside this tag <%……%> is evaluated and just the result is sent back to the client as a string.
Comments:
Used to comment code in jsp. There are two options. First option tag used is <%– Comment –%>. This comment will be ignored by the web container and no code will be sent in response. That means comments are not transferred over network, and the amount of data sent over network is reduced. Second syntax is html comments <!-comment –>. Container treats it as an html and sends it to the client.
Declarations:
Using this, we can declare the class level variables, methods and classes. These declarations are initialized when the JSP is initialized. The scope of initialization is class level. Class level variable means the translation phase keeps these variables outside the service method at class level. Hence once set these attributes are available in each request to service method. The syntax used is -
<%! String name = “Jsp Tutorial”; %>
Template Text:
This is other than the jsp code. The fillers that are used sometimes to define the look and feel of the page presented. This code can be html, xml, wml or text.
<H1>ABC</H1>
To Summarize, above elements are to be used when we write a jsp page. There are different uses of each element. We need to use the correct element after understanding the response they will generate.
<<Previous Home Next>>
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
