36
JSP introduction Trainer = Subhasis Nayak CMC

C:\fakepath\jsp01

Embed Size (px)

DESCRIPTION

java server pages introduction

Citation preview

Slide 1

JSP introductionTrainer = Subhasis NayakCMCWhat is JSP ?It is a textual document that describes how to create a response object from a request object for given protocol.The processing of JSP page may involve:Creating objectsUsing objects.The default request & response objects areHttpServletRequestHttpServletResponseWhat it provides?Capability to create both static & dynamic components.Dynamic capability that Servlet can provide.Integration of content.Manipulating data.Information about JSPJava Server pages has these features:Language for developing java server pages.Constructs for accessing server-side objects.Defining extensions to the JSP language.It is a text based document that contains two types of text.Static template dataJSP elements which construct dynamic data.JSP life cycleSome how similar to Servlet life cycle.When any request mapped to a JSP, it is handled by a special Servlet event.Servlet checks the java Server Pages Servlet is older than the java Server Page.If so it translates to java Server Page into a Servlet class & compiles the class.Next the Servlet sends response to the java server page.Advantages of JSPBuild process is performed automatically.Translation phase treat each data type in java Server Pages differently.Template data is transformed into code.This code emits that data stream that returns data to the client. It is faster than Servlet.JSP Architecture ModelAccording to Sun there 2 architectural model for building application using JSP & Servlet technology. Those are:JSP model 1JSP model 2JSP model 1In this model the every request to a JSP page. The requested page is completed responsible for doing all the tasks required for fulfilling the request.EIS/DBBrowserJSPJava Beans1.request4.response2.CreateJava beans3.Retrieve dataSuite able for small application. In big application we put huge logic code to JSP.

Not good for Complex

JSP model 2This architecture follows MVC design pattern.Servlet act as ControllerJSP act as ViewJava Bean act as ModelAll requests to Servlet. They analyze the request and collect data required to generate response into java beans objects. Then Servlet dispatch the request to JSP.JSP use data stored in java beans to generate a presentable response.

Contd EIS/DBBrowserServlet(Controller)Java Beans(Model)4.Invoke JSP6.response2.CreateJava beans3.Retrieve dataJSP(view)2.UseJava beans1.requestHigher securityEasy maintainability JSP life Cycle.For 1st time when we accessed the JSP page server is slower after that it will faster.When we access JSP it is converted to its Servlet class before it can be used to service client side request.For each request the JSP engine checks the timestamps of source JSP page and corresponding Servlet if JSP is newer then it will be again converted to its equivalent Servlet. This process consists of 7-phases.Contd ..Phase NameDescriptionPage transactionA Servlet (java) file is createdPage compilationThe java file compiled.Load classCompiled class loadedCreate InstanceAn instance of Servlet is createdCall jspInit()This method is called before other method to allow initialization.Call _jspService()This method is called for each requestCall jspDestory()This method is called when Servlet decided to take the Servlet out of serviceElements of JSPJSP tagdescription SyntaxdirectiveSpecifies translation time instruction to JSP engine

DeclarationIt declare & define variables , methods.

ScriptletsTags are used to embed java code in JSp page. Allows to write free-form java code in JSP page

ExpressionUsed to print value in the out put html file

ActionProvides request time instructions to the JSp engine.

commentUsed for documentation

A simple JSP page

Scriptlet The processing of JSPWhen the browser asks the Web server for a JSP, the Web server passes control to a JSP container.A container works with the Web server to provide the runtime environment and other services a JSP needs. It knows how to understand the special elements that are part of JSPs. Because this is the first time this JSP has been invoked, the JSP container converts it into an executable unit called a Servlet.Contd The entire page, including the parts that are in HTML, is translated into source code. After code translation, the JSP container compiles the Servlet, loads it automatically, and then executes it.Typically, the JSP container checks to see whether a Servlet for a JSP file already exists .If not it do the translation processIf version not match do the translation processLets see the translated code of a JSP Hello World! out.write("\r\n"); out.write("\r\n"); out.println("Hello World!"); out.write("\r\n"); out.write("\r\n"); out.write("\r\n");JSPServletDont worry about Servlet file of JSP. JSP, why?Lets display Something

Output swill sameThe second one is the short hand codeProgram displaying date

variablesYou can declare your own variables, as usualJSP provides several predefined variablesrequest : The HttpServletRequest parameterresponse : The HttpServletResponse parametersession : The HttpSession associated with the request, or null if there is noneout : A JspWriter (like a PrintWriter) used to send output to the clientExample:Your hostname:

ScriptletsScriptlets are enclosed in tagsScriptlets do not produce a value that is inserted directly into the HTML (as is done with )Scriptlets are Java code that may write into the HTMLExample:

Scriptlets are inserted into the Servlet exactly as written, and are not compiled until the entire Servlet is compiledExample:

Have a nice day!

Have a lousy day!

DeclarationsUse for declarations to be added to your Servlet class, not to any particular methodCaution: Servlet are multithreaded, so nonlocal variables must be handled with extreme careIf declared with , variables are local and OKData can also safely be put in the request or session objectsExample:

Accesses to page since server reboot:

You can use to declare methods as easily as to declare variables

DirectivesDirectives affect the Servlet class itselfA directive has the form: or The most useful directive is page, which lets you import packagesExample:

JSP CommentsDifferent from HTML comments.HTML comments are visible to client.

JSP comments are used for documenting JSP code .JSP comments are not visible client-side.

The include directiveThe include directive inserts another file into the file being parsedThe included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directivesSyntax: The URL is treated as relative to the JSP pageThe include directive is especially useful for inserting things like navigation bars

ActionsActions are XML-syntax tags used to control the Servlet engine

Inserts the indicated relative URL at execution time (not at compile time, like the include directive does)This is great for rapidly changing data

Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL

JSP in XMLJSP can be embedded in XML as well as in HTMLDue to XMLs syntax rules, the tags must be different (but they do the same things)HTML: XML: expressionHTML: XML: codeHTML: XML: declarationsHTML: XML:

Action useBean tagThe useBean action tag is the most commonly used tag because of its powerful features.It allows a JSP to create an instance or receive an instance of a Java Bean.It is used for creating or instantiating a bean with a specific name and scope.Examples