Search This Blog

Friday, 28 October 2011

Server-side Scripting using JSP!

1 comments
SERVER-SIDE SCRIPTING USING JSP!
JSP often present dynamically generated content as part of an HTML document sent to the client in response to a request. In some cases, the content is static, but is output only if certain conditions are met during a request. JSP programmers can insert Java code and logic in a JSP using script.
Scripting Components:
JSP scripting components include: scriptlets, comments, expressions, declarations and escape sequence characters. Scriptlets are blocks of code delimited by <% and %>. They contain Java statements that the container places in method –jspService at translation time.
JSPs supports three comment styles: JSP comments, HTML comments and comments from the scripting language like JavaScript. JSP comments are delimited by <%-- and --%>. HTML comments are delimited with <!-- and -->. Scripting language comments are Java comments and are delimited by / and /. JSP comments and scripting-language comments are ignored and do not appear in the response to a client.
A JSP expression, delimited by <%= and %> contains Java code that is evaluated when a client requests the JSP containing the expression. The container converts the result of a JSP expression to a String object, and then outputs the string as part of the response to the client.
Declarations (delimited by <%! and %>) enable a JSP programmer to define variables and methods. The variables become instance variables of the Servlet class and the methods become members. Declarations of variables and methods in a JSP use Java syntax. Hence, declaring a variable without using a semicolon is a syntax error.
Escape sequences are sequence of characters used in JSP to make use of the special characters like <% and %> as part of the output. The escape sequences are as follows:
<\% %\> \’ \” \\
The following is an example for JSP code written using two of the JSP components - scriptlets and expression:
<? xml version = “1.0”>
<html>
<head>
<title> Processing “get” request with data </title>
</head>
<body>
<%
String name = request.getParameter(“fname”);
if (name != null)
{
%>
<h1>
Hello <%= name %> <br/>
Welcome to Java Servlet Pages!
</h1>
<%
}
else {
%>
<form action = “welcome.jsp” method = “get”>
<p>Type your first name and press Submit</p>
<p> <input type = “text” name = “fname” />
<input type = “submit” value = “Submit” />
</p>
</form>
<%
}
%>
</body>
</html>
 Standard Actions:
Standard actions are special tags provided in JSP for performing some of the most common tasks in a JSP program, such as including content from other resources, forwarding requests to other resources and interacting with Java Beans. JSP containers process actions at request time.
Actions are delimited by <jsp:action> and </jsp:action> tags, where action is one of the standard actions defined in JSP as listed below:


Action
Description
<jsp:include>
Dynamically includes another resource in a JSP. As the JSP executes, the referenced resource is included and processed.
<jsp:forward>
Forwards request processing to another JSP, servlet or static page. This action terminates the current JSP’s execution.
<jsp:plugin>
Allows a plug-in component to be added to a page in the form of a browser-specific object or embed HTML element.
<jsp:param>
Used with the include, forward and plug-in actions to specify additional name/value pairs of information for use by these actions.
JavaBean Manipulation
<jsp:useBean>
Specifies that the JSP uses a JavaBean instance. This action specifies the scope of the bean and assigns it an ID that scripting components can use to manipulate the bean.
<jsp:setProperty>
Sets the property in the specified JavaBean instance.
<jsp:getProperty>
Gets a property in the specified JavaBean instance and converts the result to a String for output in the response.

Table: JSP actions and their description