In this JPS tutorial, you will learn how to program using JSP, JSP expressions and Implicit Objects, JSP Session Object, methods of session object, getAttribute(String name), getAttributeNames and isNew().
If a programmer wants to insert data into an HTML page, then this is achieved by making use of the JSP expression.
The general syntax of JSP expression is as follows:
The expression is enclosed between the tags <%= %>
For example, if the programmer wishes to add 10 and 20 and display the result, then the JSP expression written would be as follows:
Implicit Objects in JSP are objects that are automatically available in JSP. Implicit Objects are Java objects that the JSP Container provides to a developer to access them in their program using JavaBeans and Servlets. These objects are called implicit objects because they are automatically instantiated.
There are many implicit objects available. Some of them are:
request: The class or the interface name of the object request is http.httpservletrequest. The object request is of type Javax.servlet.http.httpservletrequest. This denotes the data included with the HTTP Request. The client first makes a request that is then passed to the server. The requested object is used to take the value from client’s web browser and pass it to the server. This is performed using HTTP request like headers, cookies and arguments.
response: This denotes the HTTP Response data. The result or the information from a request is denoted by this object. This is in contrast to the request object. The class or the interface name of the object response is http.HttpServletResponse. The object response is of type Javax.servlet.http. >httpservletresponse. Generally, the object response is used with cookies. The response object is also used with HTTP Headers.
Session: This denotes the data associated with a specific session of user. The class or the interface name of the object Session is http.HttpSession. The object Session is of type Javax.servlet.http.httpsession. The previous two objects, request and response, are used to pass information from web browser to server and from server to web browser respectively. The Session Object provides the connection or association between the client and the server. The main use of Session Objects is for maintaining states when there are multiple page requests. This will be explained in further detail in following sections.
Out: This denotes the Output stream in the context of page. The class or the interface name of the Out object is jsp.JspWriter. The Out object is written: Javax.servlet.jsp.JspWriter
PageContext: This is used to access page attributes and also to access all the namespaces associated with a JSP page. The class or the interface name of the object PageContext is jsp.pageContext. The object PageContext is written: Javax.servlet.jsp.pagecontext
Application: This is used to share the data with all application pages. The class or the interface name of the Application object is ServletContext. The Application object is written: Javax.servlet.http.ServletContext
Config: This is used to get information regarding the Servlet configuration, stored in the Config object. The class or the interface name of the Config object is ServletConfig. The object Config is written Javax.servlet.http.ServletConfig
Page: The Page object denotes the JSP page, used for calling any instance of a Page's servlet. The class or the interface name of the Page object is jsp.HttpJspPage. The Page object is written: Java.lang.Object
The most commonly used implicit objects are request, response and session objects
The previous section listed implicit objects available in JSP and detailed each of the methods of request object and response object. This section details the Session Object and the methods available with syntax and explanation on each.
Session Object denotes the data associated with a specific session of user. The class or the interface name of the object session is http.HttpSession. The object session is written as:
The previous two objects, request and response, are used to pass information from web browser to server and from server to web browser respectively.
The Session Object provides the connection or association between the client and the server. The main use of Session Objects is to maintain states when there are multiple page requests.
The main feature of session object is to navigate between multiple pages in a application where variables are stored for the entire user session. The session objects do not lose the variables and the value remains for the user’ session. The concept of maintenance of sessions can be performed by cookies or URL rewriting. A detailed approach of session handling will be discusses in coming sections.
There are numerous methods available for session Object. Some are:
Detailed usage with syntax and example with explanation of each of these methods.
The getAttribute method of session object is used to return the object with the specified name given in parameter. If there is no object then a null value is returned.
General syntax of getAttribute of session object is as follows:
The value returned is an object of the corresponding name given as string in parameter. The returned value from the getAttribute() method is an object written: java.lang.Object.
In the above statement, the value returned by the method getAttribute of session object is the object of name given in parameter of type java.lang. Object and this is typecast to String data type and is assigned to the string exforsys.
The getAttributeNames method of session object is used to retrieve all attribute names associated with the current session. The name of each object of the current session is returned. The value returned by this method is an enumeration of objects that contains all the unique names stored in the session object.
The returned value by this method getAttributeNames() is Enumeration of object.
The above statement returns enumeration of objects, which contains all the unique names stored in the current session object in the enumeration object exforsys.
The isNew() method of session object returns a true value if the session is new. If the session is not new, then a false value is returned. The session is marked as new if the server has created the session, but the client has not yet acknowledged the session. If a client has not yet chosen the session, i.e., the client switched off the cookie by choice, then the session is considered new. Then the isNew() method returns true value until the client joins the session. Thus, the isNew() method session object returns a Boolean value of true of false.
General syntax of isNew() of session object is as follows:
The returned value from the above method isNew() is Boolean