Professional Documents
Culture Documents
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" %>
<%!
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
<% out.println(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:
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
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
Example
<jsp:forward page=Forwarded.html>
Forwards the request to Forwarded.html
JSP Actions
Param
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