Please style sheet are not equal in internet explorer browser Firefox, Chrome, Safari, Apple and Opera browser please visit this website.

Thank for Visit My Site and Please sent me Shayari, Status and Quotes post request.

Java Server Pages - JSP Scripting Element, Comments and Character Quoting Convention, Actions in JSP

JSP Scripting Element
 
Introduction
 
JSP scripting elements let us insert Java code into the servlet that will be generated from the current JSP page.
 
There are three forms:
1. Expressions of the form <%= expression %> that are evaluated and inserted into the output,
2. Scriptlets of the form <% code %> that are inserted into the servlet's service method, and
3. Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.
 
 
 
 
JSP Expressions
 
A JSP expression is used to insert Java values directly into the output.
 
It has the following form:
<%= Java Expression %>
 
The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time (when the page is requested), and thus has full access to information about the request.
 
For example, the following shows the date/time that the page was requested:
Current time: <%= new java.util.Date() %>
To simplify these expressions, there are a number of predefined variables that we can use.
 
These implicit objects are discussed in more detail later, but for the purpose of expressions, the most important ones are:
 
. request, the HttpServletRequest;
. response, the HttpServletResponse;
. session, the HttpSession associated with the request (if any); and
. out, the PrintWriter (a buffered version of type JspWriter) used to send output to the client.
 
example:
hostname: <%= request.getRemoteHost() %>
 
Finally, note that XML authors can use an alternative syntax for JSP expressions:
 <jsp:expression>
     Java Expression
</jsp:expression>
Remember that XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.
 
 
 
JSP Scriptlets
 
If we want to do something more complex than insert a simple expression, JSP scriptlets let us insert arbitrary code into the servlet method that will be built to generate the page.
 
Scriptlets have the following form:
<% Java Code %>
 
Scriptlets have access to the same automatically defined variables as expressions.
So, for example, if we want output to appear in the resultant page, we would use the out variable.
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
 
Note that code inside a scriptlet gets inserted exactly as written, and any static HTML (template text) before or after a scriptlet gets converted to print statements. This means that scriptlets need not contain complete Java statements, and blocks left open can affect the static HTML outside of the scriptlets.
 
For example, the following JSP fragment, containing mixed template text and scriptlets
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
 
will get converted to something like:
if (Math.random() < 0.5)
{
out.println("Have a <B>nice</B> day!");
} else {
out.println("Have a <B>lousy</B> day!");
}
 
If you want to use the characters "%>" inside a scriptlet, enter "%\>" instead.
 
Finally, note that the XML equivalent of <% Code %> is
<jsp:scriptlet>
Code
</jsp:scriptlet>
 
Remember that XML elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.
 
 
 
JSP Declarations
 
A JSP declaration lets us define methods or fields that get inserted into the main body of the servlet class (outside of the service method processing the request).
 
It has the following form:
<%! Java Code %>
 
Since declarations do not generate any output, they are normally used in conjunction with JSP expressions or scriptlets.
 
For example, here is a JSP fragment that prints out the number of times the current page has been requested since the server booted (or the servlet class was changed and reloaded):
<%! private int accessCount = 0; %>
Accesses to page since server reboot:
<%= ++accessCount %>
 
As with scriptlets, if we want to use the characters "%>", enter "%\>" instead.
 
Finally, note that the XML equivalent of <%! Code %> is :
<jsp:scriptlet>
Code
</jsp:scriptlet>
 
 
 
Comments and Character Quoting Convention
 
Comments and Character Quoting
 
There are a small number of special constructs you can use in various cases to insert comments or characters that would otherwise be treated specially.
 
Here's a summary:
 
Syntax Purpose
<%--
comment
--%>
A JSP comment. Ignored by JSP-to-scriptlet translator. Any embedded JSP scripting elements, directives, or actions are ignored.
<!--
comment
-->
An HTML comment. Passed through to resultant HTML. Any embedded JSP scripting elements, directives, or actions are executed normally.
<\% Used in template text (static HTML) where we really want "<%".
%\> Used in scripting elements where we really want "%>".
\' A single quote in an attribute that uses single quotes. Remember, however, that you can use either single or double quotes, and the other type of quote will then be a regular character.
\" A double quote in an attribute that uses double quotes. Remember, however, that you can use either single or double quotes, and the other type of quote will then be a regular character.
%\> %> in an attribute.
<\% <% in an attribute.
 
 
 
Actions in JSP
 
Introduction
 
JSP actions use constructs in XML syntax to control the performance of the servlet engine. We can dynamically introduce a file, reprocess JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.
 
Available actions include:
 
. jsp:include
Include a file at the time the page is requested.
 
. jsp:useBean
Find or instantiate a JavaBean
 
. jsp:setProperty
Set the property of a JavaBean
 
. jsp:getProperty
Insert the property of a JavaBean into the output.
 
. jsp:forward
Forward the requester to a new page.
 
. jsp:plugin
Generate browser-specific code that makes an OBJECT or EMBED tag for the Java plugin..
 
 
 
The jsp:include Action
 
This action lets us insert files into the page being generated.
 
The syntax looks like this:
<jsp:include page="relative URL" flush="true" />
 
Unlike the include directive which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested. This pays a small penalty in efficiency, and precludes the included page from containing general JSP code (it cannot set HTTP headers, for example), but it gains significantly in flexibility.
 
For example, here is a JSP page that inserts four different snippets into a "What's New?" Web page. Each time the headlines change, authors only need to update the four files, but can leave the main JSP page unchanged.  
 
 
What's New in JSP
 
<HTML>
<HEAD>
<TITLE>What's New</TITLE>
<LINK REL=STYLESHEET
HREF="My-Style-Sheet.css"
TYPE="text/css">
</HEAD>

<BODY BGCOLOR="#abcdef" TEXT="#000fff" LINK="#0000EE"
VLINK="#551A8B" ALINK="#FF0000">
<CENTER>
<TABLE BORDER=5 BGCOLOR="#EF8429">
<TR><TH CLASS="TITLE">
What's New at Jsptutorial.com in MKDTutorials</TABLE>
</CENTER>
<P><h1>
Here is a summary of our four most recent tutorial example in JSP
</h1>
<OL>
<LI><jsp:include page="welcome.jsp" flush="true"/>
<LI><jsp:include page="form.jsp" flush="true"/>
<LI><jsp:include page="111.jsp" flush="true"/>
</OL>
</BODY>
</HTML>
 
Here's a result:
 
Output:
 
 
 
The jsp:useBean Action
 
This action lets us load in a JavaBean to be used in the JSP page. This is a a very useful capability because it lets you exploit the reusability of Java classes without sacrificing the convenience that JSP adds over servlets alone.
 
The simplest syntax for specifying that a bean should be used is:
<jsp:useBean id="name" class="package.class" />
 
This usually means "instantiate an object of the class specified by class, and bind it to a variable with the name specified by id." However, as we'll see shortly, we can specify a scope attribute that makes the bean associated with more than just the current page. In that case, it is useful to obtain references to existing beans, and the jsp:useBean action specifies that a new object is instantiated only if there is no existing one with the same id and scope.
 
Now, once we have a bean, we can modify its properties via jsp:set Property, or by using a scriptlet and calling a method explicitly on the object with the variable name specified earlier via the id attribute. Recall that with beans, when we say "this bean has a property of typeX called form", we really mean "this class has a method called getForm that returns something of type X, and another method called setForm that takes an X as an argument."
 
The jsp:set Property action is discussed in more detail in the next section, but for now note that we can either supply an explicit value, give a param attribute to say that the value is derived from the named request parameter, or just list the property to indicate that the value should be derived from the request parameter with the same name as the property.
 
We read existing properties in a JSP expression or scriptlet by calling the appropriate getXxx method, or more commonly, by using the jsp:getProperty action.
 
Note that the class specified for the bean must be in the server's regular class path, not the part reserved for classes that get automatically reloaded when they change.
 
For example, in the Java Web Server, it and all the classes it uses should go in the classes directory or be in a jar file in the lib directory, not be in the servlets directory.
 
Here is a very simple example that loads a bean and sets/gets a simple String parameter.
 
BeanTest.jsp
 
</HTML><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
<LINK REL=STYLESHEET
    HREF="My-Style-Sheet.css"
     TYPE="text/css">
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
   <TR><TH CLASS="TITLE">
     Reusing JavaBeans in JSP</TABLE>
</CENTER>
<P>
<jsp:useBean id="test" class="hall.SimpleBean" />
<jsp:setProperty name="test"
    property="message"
    value="Hello , how is this JSP tutorial from MKDTutorials" />
<H1>Message: <I>
<jsp:getProperty name="test" property="message" />
</I></H1>
</BODY>
 
SimpleBean.java
Here's the source code for the bean used in the BeanTest JSP page.
 
package hall;
public class SimpleBean
{
private String message = "No message specified";
public String getMessage()
{
    return(message);
}

public void setMessage(String message)
{
    this.message = message;
}
}
 
Here's a typical result: try it out yourself
 
 
 
More jsp:useBean Details
 
The simplest way to use a bean is to use
<jsp:useBean id="name" class="package.class" />
 
to load the bean, then use jsp:setProperty and jsp:getProperty to modify and retrieve bean properties.
However, there are two other options.
 
First, we can use the container format, namely
<jsp:useBean ...>
Body
</jsp:useBean>
to indicate that the Body portion should be executed only when the bean is first instantiated, not when an existing bean is found and used.
 
Beans can be shared, so not all jsp:useBean statements result in a new bean being instantiated.
 
Second, in addition to id and class, there are three other attributes that we can use:
scope,
type,
and beanName.
 
These attributes are summarized in the following table.
 
Atribute Usage
id Gives a name to the variable that will reference the bean. A previous bean object is used instead of instantiating a new one if one can be found with the same id and scope.
class Designates the full package name of the bean.
scope Indicates the context in which the bean should be made available. There are four possible values: page, request, session, and application. The default, page, indicates that the bean is only available on the current page (stored in the PageContext of the current page). A value of request indicates that the bean is only available for the current client request (stored in the ServletRequest object). A value of session indicates that the object is available to all pages during the life of the current HttpSession. Finally, a value of application indicates that it is available to all pages that share the same ServletContext. The reason that the scope matters is that a jsp:useBean entry will only result in a new object being instantiated if there is no previous object with the same id and scope. Otherwise the previously existing object is used, and any jsp:setParameter elements or other entries between the jsp:useBean start and end tags will be ignored.
type Specifies the type of the variable that will refer to the object. This must match the classname or be a superclass or an interface that the class implements. Remember that the name of the variable is designated via the id attribute.
beanName Gives the name of the bean, as you would supply it to the instantiate method of Beans. It is permissible to supply a type and a beanName, and omit the class attribute.
 
 
 
The jsp:setProperty Action
 
We use jsp:setProperty to give values to properties of beans that have been referenced earlier. We can do this in two contexts. First, we can use jsp:setProperty after, but outside of, a jsp:useBean element, as below:
<jsp:useBean id="myName" ... />
...
<jsp:setProperty name="myName"
property="someProperty" ... />
 
In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found.
 
A second context in which jsp:setProperty can appear is inside the body of a jsp:useBean element, as below:
<jsp:useBean id="myName" ... >
...
<jsp:setProperty name="myName"
property="someProperty" ... />
</jsp:useBean>
 
Here, the jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.
 
There are four possible attributes of jsp:setProperty :
 
Attribute Usage
name This required attribute designates the bean whose property will be set. The jsp:useBean element must appear before the jsp:setProperty element.
property This required attribute indicates the property you want to set. However, there is one special case: a value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
value This optional attribute specifies the value for the property. String values are automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character via the standard valueOf method in the target or wrapper class. For example, a value of "true" for a boolean or Boolean property will be converted via Boolean.valueOf, and a value of "42" for an int or Integer property will be converted via Integer.valueOf. You can't use both value and param, but it is permissible to use neither. See the discussion of param below.
param This optional attribute designates the request parameter from which the property should be derived. If the current request has no such parameter, nothing is done: the system does not pass null to the setter method of the property. Thus, you can let the bean itself supply default values, overriding them only when the request parameters say to do so. For example, the following snippet says "set the numberOfItems property to whatever the value of the numItems request parameter is, if there is such a request parameter. Otherwise don't do anything."
<jsp:setProperty name="orderBean"
property="numberOfItems"
param="numItems" />

If you omit both value and param, it is the same as if you supplied a param name that matches the property name. You can take this idea of automatically using the request property whose name matches the property one step further by supplying a property name of "*" and omitting both value and param. In this case, the server iterates through available properties and request parameters, matching up ones with identical names.
 
 
 
The jsp:getProperty Action
 
This element retrieves the value of a bean property, converts it to a string, and inserts it into the output.
The two required attributes are name, the name of a bean previously referenced via jsp:useBean, and property, the property whose value should be inserted.
 
Here's an example:
<jsp:useBean id="itemBean" ... />
...
<UL>
<LI>Number of items:
<jsp:getProperty name="itemBean" property="numItems" />
<LI>Cost of each:
<jsp:getProperty name="itemBean" property="unitCost" />
</UL>
 
 
 
The jsp:forward Action
 
This action lets us forward the request to another page. It has a single attribute, page, which should consist of a relative URL.
This could be a static value, or could be computed at request time, as in the two examples below.
 
Example:
<jsp:forward page="/utils/errorReporter.jsp" />
<jsp:forward page="<%= someJavaExpression %>" />
 
 
The jsp:plugin Action
 
This action lets us insert the browser-specific OBJECT or EMBED element needed to specify that the browser run an applet using the Java plugin.
 
 

SHARE THIS PAGE

0 Comments:

Post a Comment

Circle Me On Google Plus

Subject

Follow Us