Professional Documents
Culture Documents
<I>
<%= request.getParameter("title") %>
</I>
You normally give your file a .jsp extension, and
typically install it in any place you could place a normal
Web page
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 4
Client and Server with JSP
1. When the user loads the page for the first time, the files that make up
the application are all translated together, without any dynamic data,
into one Java source file (a .java file)
<HTML>
<H1> First JSP Page </H1>
<BODY>
<% out.println(“Welcome to JSP world”); %>
</BODY>
</HTML>
Server
File
Create Source from JSP
Changed
Syntax
Example:
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %>
-->
Examples
<%! String destin; %>
<%! Public String getDestination()
{return destin;}%>
<%! Circle a = new Circle(2.0); %>
You can declare any number of variables or methods within one declaration
element, as long as you end each declaration with a semicolon.
The declaration must be valid in the Java programming language.
Description:
An expression element contains a scripting language expression that is evaluated,
converted to a String, and inserted where the expression appears in the JSP file.
Because the value of an expression is converted to a String, you can use an
expression within a line of text, whether or not it is tagged with HTML, in a JSPfile.
Expressions are evaluated from left to right.
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 14
Expression Example
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
</HEAD>
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date() %>
<LI>Your hostname: <%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %>
</UL>
</BODY>
</HTML>
Syntax
<% code fragment %>
<%
String var1 = request.getParameter("name");
out.println(var1);
%>
</BODY>
</HTML>
Copyright @ 2000 Jordan Anastasiade. All rights reserved. 17
JSP Lifecycle
Servlet from JSP
Request
Response
jspService()
XML form:
<jsp:directive.directiveType attribute=“value” />
The <%@ include %> directive inserts a file of text or code in a JSP file
at
translation time, when the JSP file is compiled.
<%@ include %> process is static. A static include means that the text of
the included file is added to the JSP file.
The included file can be:
1. JSP file,
2. HTML file,
3. text file.
{ property="*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value="{string | <%= expression
%>}"
}
/>
Examples
<jsp:setProperty name="mybean" property="*" />
<jsp:setProperty name="mybean" property="username" />
<jsp:setProperty name="mybean" property="username" value="Steve" />
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>.
Example:
<jsp:useBean id="calendar" scope="page" class="employee.Calendar" />
<h2>
Calendar of <jsp:getProperty name="calendar" property="username" />
</h2>
<jsp:setProperty name="firstBean"
property="message"
value="This is a message from a bean" />
<H1>Message:
<I><font color="#0000FF" size=+3>
<jsp:getProperty name="firstBean" property="message" />
</I></font>
</H1>
public NameBean() {
name = null;
}
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
} Copyright @ 2000 Jordan Anastasiade. All rights reserved. 32
JSP Form
<jsp:useBean id='nb' scope='session' class=‘packBeans.NameBean'/>
<jsp:setProperty name='nb' property="*"/>
<HTML>
<BODY>
<H1>Please enter your name to be registered to JSP Course?</H1>
<FORM method="get">
<INPUT type="text" name="name" size="25">
<INPUT type="submit" value="Submit">
</FORM>
<% if ( request.getParameter("name") != null ) } %>
<%=
"Click<a href=GetName.jsp> here</a> to confirm your registration"
%>
<% } %>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Registered Name</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>