JavaServer Pages

  • Declarations

  • In order to simplify website developement and maintenance, a multideveloper approach requires that a developer never place Java programming code in a JSP page. The page should only contain HTML, (or XHTML or XML), JavaBeans and /or JSP tag libaries.

    An alternative approach to proceed in any event, the best course of action in any JSP project is often to first do all of your programming directly in the JSP page, and once everything works, move the programming into JavaBeans, Servlets, or a tag libary.

    • You use a JSP declaration to declare variables or methods for use within a JSP page.
    • The declaration must be a legal statement for the scripting language used in the JSP page.
    • JSP expressions are Java expressions, not statements, that are evaluated every time the JSP page is requested.
    • JSP scripting elements enable you to place multiple lines of java in your JSP page.

    You can write declarations in two ways: JSP syntax or XML syntax.

    JSP Syntax

    < %! int index = 10 ; % >


    XML Syntax

    < jsp:declaration >int index = 10 ; < /jsp:declaration >

    One Important point to notice is that a semicolon (;) terminates these declarations. The semicolon is necessary, becaue the declarations must be complete legal statements.

  • Implicit Objects

  • Free objects that the JSP container provides, which encapsulate specific concepts related to web applications, they provide access to JSP's runtime environment.(i.e. _jspService() method)

    When your building your website using JSP technology, you will use Java technology, you will work with Java objects, intentionaly or not. Any object in a JSP or Servlet has an erea within the web application in which it is visible to other objects, expressions, or scriplets. This visiblity is formally known as scope.

    Web applications have four different levels of scope:

    • Page
    • The first level is page scope which is restricted to a single JSP page. Any object that is created with page scope is only accessible within the page where it is created. Once the HTTP response has been sent to the requesting client or the original HTTP request has been forwarded to another web component. all references to objects with page scope are released and the objects become candidates for garbage collection. (Garbage collection is when the system destroys unused objects, thus removing them from memory.

    • Request
    • Request scope refers to all processing need to satisfy a single HTTP request. For example, if a website requires you to log in, you might interact with multiple JSP pages. Your request will probably be processed by a controlling Servlet, the data stored in multiple JavaBeans, and a JSP page will present the results to the client(usually a web browser.) Objects with request scope are valid until the requst has been completed. This means objects with request scope, that are stored in the request object, can be shared among multiple JSP pages.

      • Request Attributes
      • Request object stores those objects that have request scope. This is accomplished by request attributes. The ServletRequest class has setAttribute() and getAttribute() methods, which allow you to associate any Java object to a name, so the object can be shared between different JSP pages that are participating in handling a particular request.

        The following two pages demonstrate this feature.

        AttributeCheck.jsp Sample Code            CallAttributeCheck.jsp Sample Code            CallAttributeCheck.jsp

      • Request parameters
      • Anytime you fill out a form on a website and click submit, your browser transfers the information to the server as parameters. The two mechanisms for sending this information are the HTTP GET and the HTTP PUT operations

        • The GET method appends the data to the URL, which you may have seen before.
        • The PUT method, encodes the data in the HTTP message body.

      • Session
      • You can associate objects with a HTTP session, which allows different requests to share information. Objects with session scope are valid until throughout the duration of a session.

        A session object can be initialized using the _jspService() method.

        HttpSession session = null;

      • Application
      • In order to share information between sessions, you can use the application object which provides a higher level of application scope and enables different sessions to share data or objects.

        An application object can be initialized using the _jspService() method.

        ServletContent applcation = null;