Meherchilakalapudi.. writes for u….

Just another WordPress.com weblog

Archive for the ‘Jsp(java server Pages)’ Category

Jsp(java Server Pages)

Posted by meherchilakalapudi on August 17, 2009

 

 

 

JSP

                                [Java Server Page]

 

JavaServerPage is a simple,yet powerful technology for creating and maintaining dynamic webpages.A JSP is a HTML page with extension .jsp. we can include java executable code  in JSP pages.a JSP is nothing but a servlet after compilation.This enables the webapplication programmer to create dynamic content by using predifined components and by interacting with components using server side scripting . JSP can reuse javabeans and custom tag libraries for complex and dynamic functionality.

 

programmer tend to use JSPs most of the content sent to the client is fixed template data and only a small portion of the content is generated dynamically with java code.

 

Comparisions with CGI

 

  • JSP can maintain state on the server between requests(since it can ue Servlet sessions)
  •  Spawns a new thread for each request
  •  Does not have to be loaded each time, once it has been initiated
  •  Runs in a ready-loaded JVM as an extension to the web server

 

 

 

 

 

Comparing  with ASP:

 

           JSP           ASP
Platforms All major web paltforms Microsoft
Base Language Java  Jscript or VBScript
Components JSP tags,JavaBeans or Enterprise JavaBeans COM/DCOM
Code Interpretation Once Each instance

 

 

Comparing with Servlets:

 

  • While writing servlet programming logic is simple but output format is lengthy,

Where as in JSP we write logic and execute directly on the server

  • In servlets we can’t separate dynamic content from java code

Where as in JSP clear separation of dynamic content from java code is possible

  • In servlet we can’t generate complex HTML page,

     Where as in JSP we can directly include HTML

 

 

 

 

 

 

 

 

 

 

 

The Server component that executes the JSPs is referred as JSPContainer.

 

when JSP enabled sever receives the first request for a jsp, the jsp container translates that jsp into a java servlet that handles the current request and future requests to the jsp. If there are any errors in transalting into  the new servlet these errors result in translation-time errors.The jsp container places the java statements that implements the JSPs response in method _jspService() at translation time. If the new servlet compiles properly, the jspcontainer invokes method _jspService()  to process  the request .the jsp may respond directly to the request or invoke other webapplication components to assists the processing the request. Any errors that occur during the request processing are known as requesttime errors;

 

JSP Elements

 

  • Scripting Elements
  • Directives
  • Actions

 

 

Scripting Elements

 

JSP scripting elements allow java code -variable or method declarations,scriplets, and expressions to be inserted into our JSP page.

 

 

 

 

1)Declarations:

       

        A declarations is block of java code in a JSP that is used to defiene class-wide variables and methods in generated servlet. Declarations are initialized when the jsp page is initialized, and have instance scope in the generated servlet,so that anything defined in a declarations is available throughout the JSP to other declarations,expressions,and code.Adeclaration block is enclosed between <%! and %> and do not write anything to the outputstream. the syntax is:

 

<%! Java variable and method declarations %>

Ex:

        <%! int no=10;

                String %>

 

2)Scriptlets:

 

        A scriptlet is a block of java code that is executed during the request procesing time, and is enclosed between <% and %> tags.It produces output for the client.All the code apperaring between the <% and  %> tags in the jsp gets put into the service() method of the servlet as is, in the order in which it appears.It’s syntax is:

 

        <% valid java code statements %>

        Ex:

                <% out.println(new java.util.Date().toString()); %>

 

 

 

 

 

3)Expressions:

 

        An expression is a shorthand notation for a scriplet that sends the value of a java expression back to the client.The expression is evaluated at HTTP request processing time, and the result is converted into string and displayed.An expression is enclosed between <%= and %> tags.It’s syntax is

        <%= java expression to be evaluated %>

        Ex:

                <%=”Java Expression” %>

 

JSPDirectives:

 

A JSP directive affects the overall structure of the servlet class.Directives begin with <%@ and end with %>, and the general syntax is:

 

<%@ directivename attribute=”value” attribute=”value” %>

 

There are three main directives that can be used in a JSP:

 

1)The page directive

2)The include directive

3)The taglib directive

 

The page directive:

 

        The page directive is used to define and manipulate a number of important attributes that affect the whole jsp page.A page contains any number of page directives in any order, any where in the  JSP page.The General syntax of page directive is:

<%@ page ATTRIBUTES %>

where the valid attributes are the following name value pairs

 

  • language :D efines the scripting language to be used. default value is Java
  • extends: The value is a fully qualified class name of the superclass that the generated class,into which this jsp page is translated, must extended.omitted by default
  • import: Comma separated list of packages or classes, with the same meaning as import statements in java classes
  • session: Specifies whether the page participates in an HTTP session.when true the implicit object named session is available and can be used to  access the current/new session for the page.

If false,the page does not participate in a session and the implicit session object is unavailable     .

  • buffer: Specifies the buffering model for the output stream to the client. if the value is none, no buffering occurs and all output is written directly through to the ServletResponse by a PrintWriter.

if a buffer size is specified,out is buffered witha buffer size not less than that value.

  • autoFlush: If true, the output buffer to the client is flushed qutomatically when it is full.   

If false, a runtime exception is raised to indicate a buffer overflow

  • isThreadSafe: Defines the level of thread safety implemented in the page. if the value is true the JSPengine may send multiple client requests tothe page at the same time.

If the value is false then the JSP container queues up client requests sent to the page for processing, and process them one at a time, in the order in which they are recieved.

 

  • errorPage: Defines a URL to another JSP page within the current webapplications,which is invoked if a checked or unchecked exception is thrown.The page implementation catches the instance   of the throwable object and passes it to the error page.
  • isErrorPage: Indicates if the current JSP page is intended to another JSP page’s error page.
  • contentType: Defines the character encoding for the JSP and the MIME type for the response of the JSP page.The default value of MIME  type is text/html.

       

The include directive:

 

The include directive instructs the container to include the contents of a resource in the current JSP, inserting it inline, in the JSP page.The specified file must be accessible and available to the JSP container.The syntax of the include directive is:

 

        <%@ include file=”Filename” %>

 

        Ex: <%@ include file=”/jsp-examples/hello.jsp” %>

       

the contents of the included file is parsed by the JSP only at translation time, that is when the JSP page is compilied into a servlet.

 

       

Implicit Objects:

 

  • request: The request object represents the HttpServletRequest instance to access the incoming request.
  • response: The response object is the HttpServletResponse instance that represents the servlets’s response to the request.
  • pageContext: the pageContext provides a single point of access to many of the page attributes and is convenient place to put shared data within the page.
  • session: The session object represents the session created for the requesting client.
  • application: The application object represents the servlet context,obtained from the servlet configurations object.
  • out: The out object is the object that writes into the outputstream to the client.
  • config: The config object is the ServletConfig for this JSP page, and has page scope

 

 

Standard Actions(XMLTags):

 

JSPTags                                                           XMLTags

 

<%! declarations ; %>                                                                                                                  <jsp:declarations> 

                                                                                …….

                                                                                                                                                        </jsp:declarations>

 

<%= expressions %>                                                                                                                    <jsp:expression>

                                                                        …….

                                                                                                                                                        </jsp:expression>

 

 

 

 

 

<% scriplets %>                                  <jsp:scriplets >

                                                                        …….

                                                                                                                                                        </jsp:scriplets>

 

<%@ directive %>               <jsp:directive attribute=”value” />

       

<%@ include %>          <jsp:include page=”URL”   />

 

<% response.sendRedirect %>      <jsp:forward   page=”URL” />

                                <jsp:param name=”attribute” value=”value”>

                                                <jsp:plugin>

                                                <jsp:useBean>

                                                <jsp:setProperty>

                                                <jsp:getProperty>

 

 

 

 

 

 

<jsp:include>

 

        It is so convenient to be able to include the contents of othre files into a jsp page.This makes it easier to use standard HTML headers and footers or to share common code among multiple JSP pages.

Ex:

<jsp:include page=”url” />

 

 

 

 

<jsp:forward>

       

        This action is used to forward the  request to another JSP page.It has a single attribute i.e.,page which shared consists of a related URL.

Ex:

a)     <jsp:forward page=”url” />

 

b)     <jsp:forward page=”url” >

        <jsp:param value=”value” />

        </jsp:forward>

 

<jsp:useBean>

 

Syntax:

 

<jsp:useBean   id=”name”  scope=”page/request/session/application” />

 

id: This usually means instantiate an object of the class specified by class, and bind it to a varaible with the name specified by id.

 

class:It designates the full package name of the bean

 

scope:It indicates the context in which the bean should be made available

 

 

 

 

 

 

                                                                                                                 1.page: Bean is only available to the current page

                                                                                                                 2.request: Bean is only available for the current client request

                                                                                                                 3.session: Bean is available to all the pages during the life of the current

 

         HttpSession

                                                                                                                

 

4.application: Bean is available to all the pages that share the same servlet

                                                                                          context

 

<jsp:setProperty>

 

This action give values to properties of the beans

 

syntax:

                                                                                                                 <jsp:setProperty property=”propertyname”

                                                                                                                                  property=”propertyName”

   value=”propertyValue” />

 

<jsp:getProperty>                                                                                                                                                                                                                                           

 

This action retrieves the values of a bean property,converts it into a string and insert it into the output.

 

syntax:

                                                                                                                 <jsp:getProperty property=”propertyname”>

 

 

 

<jsp:plugin>

 

The <jsp:plugin> action provides easy support for including Java applets in JSP

Generated page. It is used to generate browser-specific HTML tags that results in the download of the Java Plugin-in software,if required ,followd by the execution of the applet or JavaBean component that is specified in the tag.

 

Syntax:

 <jsp:plugin type=”bean/applet” code=”objectcode”

 

codebase=”objectcodebase”

height=”height”

width=”width”>

<jsp:params>

     <jsp:param name=”paramname” value=”paramvalue” />

          <jsp:param name=”paramname” value=”paramvalue” />

        …….

</jsp:params>

<jsp:fallback >Alternative text to display</jsp:fallback>

</jsp:plugin>

 

                                                                                                  

                                                                                                  

                                                                                                                

 

 

 

                                                                                                                 

                                                                                                                                           property=”propertyof the bean” />

                                                                                                                                                                                                                                                       

 

 

 

       

 

 

 

 

 

 

Posted in Jsp(java server Pages) | Leave a Comment »