Professional Documents
Culture Documents
Scripting Elements
Scriptlet
<% %>
Declaration
<%! %>
Expression
<%= %>
Biswabrata Banerjee
Multiple <% %> tags can appear on the same JSP page. So this is perfectly legal <html ><head></head> <body> <% out.print(scriptlet tag 1); %> <% out.print(scriptlet tag 2); %> </body> </html>
Consecutive <% %> tags has the same effect as a single <% %> tag that contains all the code fragments of them. Thus the previous code is same as:
<%
out.print(scriptlet tag 1); out.print(scriptlet tag 2); %> All the code appearing between <% and %> get put into service() method of the generated servlet as it is.
Biswabrata Banerjee
Lab Work 2. Execute a for loop through Scriptlet that will calculate the sum of multiples of 3 numbers from 1 t0 100 and Display with expression tag.
Biswabrata Banerjee
Declarations
Declarations allow you to declare methods and variables that can be used from any point in the JSP page. A declaration starts with a <%! and ends with a %> and can appear anywhere throughout the page. Each variables declaration statement must be terminated with a semicolon. The variable and method are added as a class-level variable and method in the servlet class. Remember : Do not place a space between <% and !. Do not try to write anything to the output stream.
html body tags open
%>
Expression
An expression starts with a <%= and ends with a %>. When the JSP page is requested (i.e., at HTTP request processing time) the results are converted into a String and fed to the print method of the out implicit object.
Remember : Don't add a semicolon at the end of an expression . Dont place any white-space characters between <% and = .
<html> <head><title>expression Tag</title></head> <body>
<%! int count =1; >
Output
</html>
Biswabrata Banerjee
Comments
A JSP comment is of the form: <%-- anything but a closing --%> --%>
HTML or XML comments are still supported in a JSP page: <!-template texts or static contents arent translated but dynamic contents are still processed -->
<head></head> Will not increment <body> counter2 <%! int count 1= 1,count2=1; %> <!- - <% count1++; %> - -> Will increment <%-- <% count2++; %> --%> counter1 <%= "I'm accessed "+count1+" times"%> <%= "I'm accessed "+count2+" times"%> </body> Will always print 1 </html> <html>
Biswabrata Banerjee
JavaBean Overview
A constructor that takes no parameters Properties
Read/write, read-only, or write-only Simple, which means it contains a single value, or an array of values
For each readable property, the bean must have a method of the form:
PropertyClass getProperty() { ... }
For each writable property, the bean must have a method of the form
setProperty(PropertyClass pc) { ... }
jsp:useBeans
The steps that should be followed
1. Write a bean class and save it as a .java file; say, mybean.java that would contain one or more public methods . 2. Compile the bean to obtain a class file called mybean.class. 3. Copy the bean class file to the classes directory under WEB-INF under your application directory. The deployment must take into account the package name ( if any ). 4. Create a JSP page that will call the bean you just created. 5. Restart Tomcat. 6.Open and direct your browser to the URL of the JSP page your wrote in Step 4.
Biswabrata Banerjee
jsp:useBean Illustrated
myBean.java
package com; public class myBean { private int i; public myBean(){ i=0; } public void setI(int val){ i=val; } public int getI(){ return i; } public int doubleIt(int number) { return 2 * number; } }
firstBean.jsp
<jsp:useBean id="theBean class="com.myBean"/>
<HTML> <HEAD> </HEAD> <BODY>
Biswabrata Banerjee
jsp:getProperty
jsp:getProperty
The <jsp:getProperty> element gets a Bean property value using the property's getter methods and displays the property value in a JSP page. You must create or locate a Bean with <jsp:useBean> before you use <jsp:getProperty>. <jsp:getProperty name="name property="propertyName" />]
jsp:setProperty
jsp:setProperty
The <jsp:setProperty> element sets the value of one or more properties in a Bean, using the Bean's setter methods. You must declare the Bean with <jsp:useBean> before you set a property value with <jsp:setProperty>. Because <jsp:useBean> and <jsp:setProperty> work together, the Bean instance names they use must match (that is, the value of name in <jsp:setProperty> and the value of id in <jsp:useBean> must be the same).
<jsp:setProperty name="beanName" prop_expr /> prop_expr has one of the following forms: property="*" | property="propertyName"| property="propertyName" param="parameterName"| property="propertyName" value="propertyValue
package com; public class myBean{ private int num; public void setNum(int m){ num = m; } public int getNum() { return num; } public int doubleIt(int number){ return 2 * number; } } myBean.java
create a java bean file named NameBean that has a private variable name; Now from you jsp page initialize name and then print it in Uppercase in the same page.
Lab work :
jsp:forward
forward action tag:
The forward action tag is used to transfer control to a static or dynamic resource, which is represented as a URL. Calls one JSP page from another. Execution of the calling page is terminated by the call. The user can have the target file as an HTML file, another JSP file, or a servlet. Any of the above is permitted. We can pass the parameter names and values to the forwarded file by using a <jsp: param> tag (we will see in the next slide). General syntax
<jsp:forward page="{relativeURL | <%= expression %>}" /> ----------------------------------------------------------------------------------------------------------------------------<jsp:forward page ="{relativeURL | <%= expression %>}" /> <jsp:param name ="parameterName value="parameterValue" | <%= expression %>}" /> </jsp:forward>
Biswabrata Banerjee
jsp:param
param action tag:
request parameters can be passed by using <jsp: param> This tag contains two attributes: 1) name 2) value. This tag is mostly used as a child tag enclosed inside any other action tag.
<html><head></head> <body> <%! int count=1;%> <%count++;%> <jsp:forward page="forwardTo.jsp"> <jsp:param name="Count" value="<%= count %>"/> </jsp:forward> </body> </html> <html> <head></head> <body> <% String count=(String)request.getParameter("Count"); %> <%= "I'm accessed "+count+" times"%> </body> </html>
forwardFrom.jsp
Example
forwardTo.jsp
Biswabrata Banerjee
jsp:include
Call one JSP page from another. Upon completion, the destination page returns control to the calling page. This action element is similar to the include directive, but jsp:include provides greater flexibility because you can pass information to the included resource. The syntax for the jsp:include action element has two forms. For the jsp:include element that does not have a parameter name/value pair, the syntax is as follows: <jsp:include page="relativeURL" flush="true"/> If you want to pass information to the included resource, use the second syntax: <jsp:include page="relativeURL" flush="true"> <jsp:param name="paramName value="paramValue" /> </jsp:include>
Biswabrata Banerjee
jsp:include illustrated
<html> callee.jsp <head></head> <body> <jsp:include page=target.jsp"> <jsp:param name="Creator" value="Biswabrata Banerjee" /> </jsp:include> </body> </html> <html><head></head> <body> <% String str=(String)request.getParameter("Creator"); %> <input type="text" name="Creator" value="<%=str%>"/> </body> </html> target.jsp
Biswabrata Banerjee
jsp:plugin
JSP Syntax
<jsp:plugin type="bean|applet" code="classFileName" codebase="classFileDirectoryName * name="instanceName" ] [ archive="URIToArchive, ..." ] [ align="bottom|top|middle|left|right" ] [ height="displayPixels" ] [ width="displayPixels" ] [ hspace="leftRightPixels" ] [ vspace="topBottomPixels" ] [ jreversion="JREVersionNumber | 1.1" ] [ nspluginurl="URLToPlugin" ] [ iepluginurl="URLToPlugin" ] > [ <jsp:fallback> text message for user </jsp:fallback> ] </jsp:plugin>
Example
<jsp:plugin type=applet code=Circle.class" codebase="/html"> <jsp:fallback> <p>Unable to load applet</p> </jsp:fallback> </jsp:plugin>
Next Day
Detailed Discussion on
JSP Objects
Biswabrata Banerjee