Pages

jsp

Wednesday, 28 March 2012

jsp : java server page  

JSP Comments:
JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out" part of your JSP page.
Following is the syntax of JSP comments:
<%-- This is JSP comment --%>


jsp tags :
   1.directives
   2. scriptlet
   3. declarations
  4. expressions
     1.directives    

JSP directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP processing.
A JSP directive affects the overall structure of the servlet class. It usually has the following form:
<%@ directive attribute="value" %>


scriptlet tag 



A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.
Following is the syntax of Scriptlet:

<% code fragment %>
You can write XML equivalent of the above syntax as follows:
<jsp:scriptlet>
   code fragment
</jsp:scriptlet>
Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP:

<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>

</html>

declaration


A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file.
Following is the syntax of JSP Declarations:
<%! declaration; [ declaration; ]+ ... %>
You can write XML equivalent of the above syntax as follows:
<jsp:declaration>
   code fragment
</jsp:declaration>

Following is the simple example for JSP Comments:
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
expression



A JSP 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 JSP file.
The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression.
Following is the syntax of JSP Expression:
<%= expression %>
You can write XML equivalent of the above syntax as follows:
<jsp:expression>
   expression

</jsp:expression>
Following is the simple example for JSP Expression:
<html>
<head><title>A Comment Test</title></head>
<body>

<p>
   Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body>
</html>




SP inclusion is the way in which one can avoid the duplicacy of code at various points in a web application. Sometimes, one needs to include some static and dynamic content across web pages in the application and the use of JSP include methods can really help in this. In this JSP tutorial, we will see how to include one JSP to another by using static as well as dynamic inclusion methods.

JSP STATIC INCLUDE

<% include file="test.jsp" %>


The static inclusion is used for static content like header, footer and sidebar in web applications. The header usually contains the logo and the footer contains the copyright information.

JSP DYNAMIC INCLUDE


<jsp:include page="test.jsp"/>


The dynamic inclusion of files is used for content which keeps on changing with the user data. Usually the body of web pages is constructed by using the dynamic inclusion of files.


JSP Form Processing
You must have come across many situations when you need to pass some information from your browser to web server and ultimately to your backend program. The browser uses two methods to pass this information to web server. These methods are GET Method and POST Method.
GET method:
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows:
http://www.test.com/hello?key1=value1&key2=value2

The GET method is the defualt method to pass information from browser to web server and it produces a long string that appears in your browser's Location:box. Never use the GET method if you have password or other sensitive information to pass to the server.
The GET method has size limtation: only 1024 characters can be in a request string.
This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable which can be handled using getQueryString() and getParameter() methods of request object.
POST method:
A generally more reliable method of passing information to a backend program is the POST method.
This method packages the information in exactly the same way as GET methods, but instead of sending it as a text string after a ? in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing.
JSP handles this type of requests using getParameter() method to read simple parameters and getInputStream() method to read binary data stream coming from the client.
Reading Form Data using JSP

JSP handles form data parsing automatically using the following methods depending on the situation:
getParameter(): You call request.getParameter() method to get the value of a form parameter.
getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
getParameterNames(): Call this method if you want a complete list of all parameters in the current request.
getInputStream(): Call this method to read binary data stream coming from the client.
GET Method Example Using URL:
Here is a simple URL which will pass two values to HelloForm program using GET method.
http://localhost:8080/main.jsp?first_name=ZARA&last_name=ALI
Below is main.jsp JSP program to handle input given by web browser. We are going to use getParameter() method which makes it very easy to access passed information:

 

Most Reading