You are on page 1of 36

JSP basic grammar(part 1)

1. Introduction of JSP page elements


2. The page directive
3. The JSP comment
4. The JSP script
5. JSP declaration
6. JSP expressions
7. The JSP page life cycle
1.JSP page elements

Comment

Static Script
content

Directive Declaration

Expression
1.JSP page elements -instance
directive

html

script
2.Jsp directive
1. Use page instructions --page
2. Use the file contains instructions --include
3. Using a reference tag library instruction--
taglib
2.Jsp directive--Page directive
the syntax format of the Page directive:
<%@ page property1=value1"
property2=value2" %>

<%@ page language="java" %>


<%@ page language="java"
contentType="text/html; charset=UTF-8"%>
The charset is refers to the content encoding of
what the server sends back to the client.
2.Page directive
<%@ page pageEncoding="UTF-8" %>
The encoding of JSP file itself.
<%@ page import="java.util.Date"%>
<%@ page buffer="16kb" %>
The buffer size of the object out. The default value is 8kb.
<%@ page autoFlush="false" %>
The default value is true, When the buffer is full will automatically
output the contents of the buffer to the client. If set to false, when the
Buffer is full will throw the JSP Buffer overflow exception.
<%@ page isErrorPage="true" %>
<%@ page errorPage="error.jsp" %>
session
isELIgnored
isThreadSafe
3.The JSP comment
HTML basic comment
<!--mmmmmmmmm-->
Eg.
<!-- now<%=new java.util.Date().toLocaleString()%> -->
3.The JSP comment
Jsp hidden comment
<%--mmmmmmmmm-->
Eg.
<%-- hidden user idallocated by system --%>
<input type="hidden" name="userId" value="1" />
<!-- -->
<input type="text" name="userName"
value="admin"/>
3.The JSP comment
Java comment
// single line
/* multiline */
/** java doc comment
*
*/
4.JSP script
<%
int num = 1;
for(int i=1; i<=10; i++){
num *= i;
}
out.print("10!=" + num);
%>
5.Declaration

<%!
int count = 0;
public int getCount(){
count ++;
return count;
}
%> Hello ! You are the vistor No.
<%=getCount() %>

6.Expressions
<%
String name = "admin";
String sex = male";
String photo = "1.gif";
%>
User name<%=name %>
sex<%=sex %>
Photo:<img alt=photo" src="<%=photo %>">
7.The four key stages of running Jsp

Jsp file
request

Servlet
file
client server
response
Class file

Servlet
instance
JSPServlet

out.write("<body>\n");
<body> out.write("The page count is:\r\n");
The page count is: int count = 0;
<% int count=0; %> out.write('\r');
<% count++; out.println(count); % > out.write('\n');
<br> count++;
<% foo.Counter c = new foo.Counter(); out.print(count);
out.print(c.getCount()); out.write("\r\n");
%> out.write("<br>\r\n");
</body> foo.Counter c = new foo.Counter();
out.print(c.getCount());
out.write("\n");
out.write("</body>\n");
out.write("</html>");
JSP script
public final class BasicCounter_jsp
extends
{
<body>

The page count is:
public void _jspService(){
<% int count=0; %>
<% count++; out.println(count); % >
put script here
<br>
</body>
}

}
JSP declaration

<%! int count=0; %>

public class basicCounter_jsp extends SomeSpecialHttpServlet {


int count=0;
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
<html><body> throws java.io.IOException {
<%! int count=0; %> PrintWriter out = response.getWriter();
The page count is now: response.setContentType(text/html);
<%= ++count %> out.write(<html><body>);
</body></html> out.write(The page count is now:);
out.print( ++count );
out.write(</body></html>);
}
}
Declare function

public class basicCounter_jsp extends SomeSpecialHttpServlet {


int doubleCount() {
<html>
count = count*2;
<body>
<%! int doubleCount() { return count;
count = count*2; }
int count=1;
return count;
public void _jspService(HttpServletRequest request,
} HttpServletResponse response)
%> throws java.io.IOException {
<%! int count=1; %> PrintWriter out = response.getWriter();
The page count is now: response.setContentType(text/html);
<%= doubleCount() %>
out.write(<html><body>);
</body>
out.write(The page count is now:);
</html>
out.print( doubleCount() );
out.write(</body></html>);
}
}
JSP expression

<% out.println(Counter.getCount()); %

<%= Counter.getCount() %>

=
Stage exercise
Respectively using expressions and scripts for
the output of the multiplication tables
JSP basic grammar(part 2)
1. The include directive
2. The taglib directive
3. JSP action
Include Directives:
The include directive is used to includes a file during the translation
phase. This directive tells the container to merge the content of
other external files with the current JSP during the translation
phase. You may code include directives anywhere in your JSP page.

Syntax:

<%@ include file="relative url/file" >


The taglib Directive:
The JavaServer Pages API allows you to define custom JSP tags
that look like HTML or XML tags and a tag library is a set of
user-defined tags that implement custom behaviour.

The taglib directive declares that your JSP page uses a set of
custom tags, identifies the location of the library, and provides
a means for identifying the custom tags in your JSP page.

syntax:
<%@ taglib uri="uri" prefix="prefixOfTag" >
JSP Actions
Basics & Types
Processed during the request processing phase.
As opposed to JSP directives which are processed during translation
Standard actions should be supported by J2EE compliant web servers
Custom actions can be created using tag libraries
The different actions are
Include action
Forward action
Param action
useBean action
getProperty action
setProperty action
plugIn action
JSP Actions
Basics & Types

Type: the standard action/ the custom action


<jsp:include>
<jsp:forward>
<jsp:plugin>
<jsp:useBean>, < jsp:setProperty >, <
jsp:getProperty >
JSP Actions
Include

Include action used for including resources in a JSP page


Include directive includes resources in a JSP page at translation time
Include action includes response of a resource into the response of
the JSP page
Same as including resources using RequestDispatcher interface
Changes in the included resource reflected while accessing the page.
Normally used for including dynamic resources

Example
<jsp:include page=inlcudedPage.jsp>
Includes the the output of includedPage.jsp into the page where this
is included.
JSP Actions <jsp:include>
Include

<jsp:include page= " Copyright.html" />


The <jsp:include> element is processed when a
JSP page is executed.
The include action allows you to include either a
static or a dynamic resource in a JSP file.
If the resource is static, its content is inserted into
the calling JSP file.
If the resource is dynamic, the request is sent to
the included resource, the included page is
executed, and then the result is included in the
response from the calling JSP page.
JSP Actions <jsp:include>
Include

Difference with the include directive <%@


include file= file% > :
The include directive: during compile time,
other page content(source) are added, faster
than include action;
Include standard action: during runtime, use
RequestDispatcher to add other page content
(result) to the output stream.
JSP Actions
Forward

Forwards the response to other web specification resources


Same as forwarding to resources using RequestDispatcher interface

Forwarded only when content is not committed to other web


application resources
Otherwise an IllegalStateException is thrown
Can be avoided by setting a high buffer size for the forwarding jsp
page

Example
<jsp:forward page=Forwarded.html>
Forwards the request to Forwarded.html
JSP Actions
Param

Used in conjunction with Include & Forward actions to


include additional request parameters to the included or
forwarded resource
Example
<jsp:forward page=Param2.jsp>
<jsp:param name=FirstName value=Sanjay>
</jsp:forward>
This will result in the forwarded resource having an additional
parameter FirstName with a value of Sanjay
JSP Actions
useBean
Creates or finds a Java object with the defined scope.
Object is also available in the current JSP as a scripting variable

Syntax:
<jsp:useBean id=name
scope=page | request | session | application
class=className type=typeName |
bean=beanName type=typeName |
type=typeName />
At least one of the type and class attributes must be present
We cant specify values for bith the class and bean name.

Example
<jsp:useBean id=myName scope=request class=java.lang.String>
<% firstName=Sanjay; %>
</jsp:useBean>
JSP Actions
get/setProperty
getProperty is used in conjunction with useBean to get property values of
the bean defined by the useBean action
Example (getProperty)
<jsp:getProperty name=myBean property=firstName />
Name corresponds to the id value in the useBean
Property refers to the name of the bean property

setProperty is used to set bean properties


Example (setProperty)
<jsp:setProperty name=myBean property=firstName value=Sanjay/>
Sets the name property of myBean to SanjayExample (setProperty)
<jsp:setProperty name=myBean property=firstName param=fname/>
Sets the name property of myBean to the request parameter fname
<jsp:setProperty name=myBean property=*>
Sets property to the corresponding value in request
JSP Actions
plugIn
Enables the JSP container to render appropriate HTML (based on the
browser type) to:
Initiate the download of the Java plugin
Execution of the specified applet or bean
plugIn standard action allows the applet to be embedded in a browser
neutral fashion
Example
<jsp: plugin type=applet code=MyApplet.class codebase=/>
<jsp:params>
<jsp:param name=myParam value=122/>
</jsp:params>
<jsp:fallback><b>Unable to load applet</b></jsp:fallback>
</jsp:plugin>
<jsp:forward>
<jsp:forward page= " foo.jsp" />
Forward the current JSP requests to another
resource
Tell the container to deal with the body, and
the results added to the response

You might also like