Complete Guide on Java Server Pages 4

Embed Size (px)

Citation preview

  • 8/19/2019 Complete Guide on Java Server Pages 4

    1/12

    Java Server Pages

    B.Ramamurthy

  • 8/19/2019 Complete Guide on Java Server Pages 4

    2/12

    Java Server PagesServlets are pure Java programs. They introducedynamism into web pages by using programmaticcontent.JSP technology is an extension/wrapper over the Java

    servlet technology.JSP are text based documents.We will focus only on JSP since it subsumes theservlet technology.Two major components of JSP:

    Static content: provided by HTML or XMLDynamic content: generated by JSP tags andscriplets written in Java language to encapsulatethe application logic.

  • 8/19/2019 Complete Guide on Java Server Pages 4

    3/12

    JSP compilation into Servlets

    WebBrowser

    WebServer

    J2EE WebContainer

    JavaServlets

    JSP

    translation

    Initialrequest

    Subseqrequest

  • 8/19/2019 Complete Guide on Java Server Pages 4

    4/12

    More on JSP syntax andcontents

    HTML code for user interface lay outJSP tags: declarations, actions, directives,expressions, scripletsJSP implicit objects: a request object,response object, session object, config objectJavabeans: for logic that can be taken care ofat the JSP level.We will examine only JSP tags here.

  • 8/19/2019 Complete Guide on Java Server Pages 4

    5/12

    JSP TagsDeclaration: variable declaration

    Directive: ex: import classes

    Scriplet: Java code

    Welcome

    Expression: regular expression using variablesand constants

    Action:

  • 8/19/2019 Complete Guide on Java Server Pages 4

    6/12

    Java Server Pages byExamples

    JSPs combine static markup (HTML, XML)with special dynamic scripting tags.Each JSP is translated into a servlet the firsttime it is invoked. Then on, the requests areserviced by the servlets.Lets understand the building blocks of a JSP,namely, directives, scripting elements, andactions through a series of examples.

  • 8/19/2019 Complete Guide on Java Server Pages 4

    7/12

    How to prepare and run theexamples?

    Simple JSPs can be typed into .jsp type filesusing your favorite editor.Create a directory called JSPExamples in thepublic_html directory of J2EE. Store theexample JSPs here.Start the J2EE server.Run the JSP from your browser using thecommand:

    http://localhost:8000/JSPExamples/xyz.jspFor complex examples with actions and beansyou will have to create web component(WAR).

  • 8/19/2019 Complete Guide on Java Server Pages 4

    8/12

    Examples: Directives

    A directive configures the code generationthat container will perform in creating a

    servlet.Simple JSP showing access to a Java APIclass Date: simple.jspUsing Page directives to define various pageattributes: pageDirective.jspDirective to include other JSPs:includeDirective1.jsp, includeDirective2.jsp

  • 8/19/2019 Complete Guide on Java Server Pages 4

    9/12

    Examples: Scripting Elements

    Declaration:

    A declaration is a block of code in a JSPthat is used to define class-widevariables and methods in the generatedservlet.

    Declaring a piece of code:declaration.jsp

  • 8/19/2019 Complete Guide on Java Server Pages 4

    10/12

    Examples: Scripting Elements(contd.)

    Scriplets: A scriplet is a block of Java code that isexecuted during the request-processing time.See scriplet.jspExpressions: sends a value of a Javaexpression back to the client.

    See expression.jsp

  • 8/19/2019 Complete Guide on Java Server Pages 4

    11/12

    Examples: Standard ActionsStandard actions are well known tags thataffect the run time behavior of the JSP andthe response sent back to the client.Some commonly used tag actions types are:

  • 8/19/2019 Complete Guide on Java Server Pages 4

    12/12

    Example: JSP Action andUsing beans (not EJB)

    Create beans.html files that displays to the user a choiceof programming languages to choose from. Store itpublic_html/JSPExamples/beans.htmlCreate the file beans.jsp that deals with the request thathas two parameters name of the user and the language.Store it in public_html/JSPExamples/beans.jspCreate the beans file that is a java bean with just setand get properties: call it LanguageBean.java. Store itinpublic_html/JSPExamples/src/com/wrox/beans/LanguageBeans.java

    Compile this using javac command. javac LangaugeBeans.javaCreate the web application and run it as a webapplication.