Monday, October 5, 2009

JSP

4 UNIT

JSP FUNDAMENTALS

JSP termed as Java Server Pages is a technology introduced by Sun Microsystems Inc. to develop the web application in more efficient way than Servlets. It has got many advanced features than servlets, one of them itself define the JSP i.e. JSP separates the presentation logic from the business logic and provide the designer and developer of the web application to work independently without any hassle.

Lets start with the basic of the JSPs, JSP is flavour of Cold fusion and ASP and hence provide the flexibility to embed the business logic efficiently within the HTML content (presentation logic).

JSP page is built using components such as :
• Directives
• Declarations
• Scriplets
• Expressions
• Standard Actions
• Custom Tags

Directives :
Listing some of them to start off :
1) Page:
Syntax : < %@ page Language=”Java” extends=”” import=” or ” %>
Attributes:
a. Language = “Java”
b. Import = “Class”
c. Buffersize = “”
d. Scope = “REQUEST/PAGE/SESSION/APPLICATION”
e. And etc….
Page directive is aimed to define certain attribute of a JSP page for e.g. Language of the page in which the page content should be written , which class to be imported so that it can be used within the JSP page.
2) Include:
Syntax: <%@ include file=”” %>
Attributes:
a. file = “
This directive is to include the a HTML, JSP or Sevlet file into a JSP file. This is a static inclusion of file i.e. it will be included into the JSP file at the time of compilation and once the JSP file is compiled any changes in the included the file will not the reflected.
Declarations:
Syntax: <%! Declare all the variables here %>
Scriplets:
Syntax: <% All your scripts will come here %>
Expressions:
Syntax: <%= expression evaluation and display the variable %>
Standard Action:
Syntax:
Include ” />
This inclusion of file is dynamic and the specified file is included in the JSP file at run-time i.e. out put of the included file is inserted into the JSP file.
Forward ” />
This will redirect to the different page without notifying browser.
And many more.
Custom Tags:
taglib
Syntax: <%@ taglib uri=”” prefix=”” %>
Attributes:
a. uri = “
b. prefix = “
prefix is alias name for the tag library name.

JSP provides certain Implicit Objects listed below.
Object Of Kind
Out
Request
Response
Application
Config
Page Object
PageContext
Page Context => is responsible for generating all other implicit objects.

Out: This object is instantiated implicitly from JSP Writer class and can be used for displaying anything within delimiters.
For e.g. out.println(“Hi Buddy”);
Request:
It is also an implicit object of class HttpServletRequest class and using this object request parameters can be accessed.
For e.g. in case of retrieval of parameters from last form is as follows:
request.getParameters(“Name”);
Where “Name” is the form element.

Response: It is also an implicit object of class HttpServletResponse class and using this object response(sent back to browser) parameters can be modified or set.
For e.g. in case of modifying the HTTP headers you can use this object.
Response.setBufferSize(“50”);

Session: Session object is of class HttpSession and used to maintain the session information. It stores the information in Name-Value pair.
For e.g.
session.setValue(“Name”,”Jakes”);
session.setValue(“Age”,”22”);


Application: This object belongs to class SevletContext and used to maintain certain information throughout the scope of Application.
For e.g.
Application.setValue(“servername”,”www.myserver.com”);
PageContext:
This object is of class pageContext and is utilized to access the other implicit objects.

Sharing data between JSP pages- Sharing Session and Application Data :

With This concept, we can track the session between different JSP pages. In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user.
In this tutorial we will create an application that takes the user name from the user and then saves into the user session. We will display the saved data to the user in another page.
Here is the code of the JSP file (savenameform.jsp) that takes the input from user:

<%@ page language="java" %>


Name Input Form



Enter Your Name:








The above page prompts the user to enter his/her name. Once the user clicks on the submit button, savenametosession.jsp is called. The JSP savenametosession.jsp retrieves the user name from request attributes and saves into the user session using the function session.setAttribute("username",username);. Here is the code of savenametosession.jsp:

<%@ page language="java" %>
<%
String username=request.getParameter("username");
if(username==null) username="";

session.setAttribute("username",username);
%>



Name Saved


Next Page to view the session value





The above JSP saves the user name into the session object and displays a link to next pages (showsessionvalue.jsp). When user clicks on the "Next Page to view session value" link, the JSP page showsessionvalue.jsp displays the user name to the user. Here is the code of showsessionvalue.jsp:

<%@ page language="java" %>
<%
String username=(String) session.getAttribute("username");
if(username==null) username="";
%>


Show Saved Name


Welcome: <%=username%>




The function session.getAttribute("username") is used to retrieve the user name saved in the session.

Starting a Session
A session is a way to store information (in the form of variables) to be used across multiple pages. Unlike a cookie, specific variable information is not stored on the users computer. It is also unlike other variables in the sense that we are not passing them individually to each new page, but instead retrieving them from the session we open at beginning of each page.


The first thing we do with this code, is open the session using session_start(). We then set our first session variables (color, size and shape) to be red, small and round respectively.
Just like with our cookies, the session_start() code must be in the header and you can not send anything to the browser before it. It's best to just put it directly after the

So how will it know it's me? Most sessions set a cookie on your computer to uses as a key... it will look something like this: 350401be75bbb0fafd3d912a1a1d5e54. Then when a session is opened on another page, it scans your computer for a key. If there is a match, it accesses that session, if not it starts a new session for you
Wr:

<%@ page language="java" %>


Name Input Form



Enter Your Name:








The above page prompts the user to enter his/her name. Once the user clicks on the submit button, savenametosession.jsp is called. The JSP savenametosession.jsp retrieves the user name from request attributes and saves into the user session using the function session.setAttribute("username",username);. Here is the code of savenametosession.jsp:

<%@ page language="java" %>
<%
String username=request.getParameter("username");
if(username==null) username="";

session.setAttribute("username",username);
%>



Name Saved


Next Page to view the session value



































No comments:

Post a Comment