1 / 15

Writing Enterprise Applications with J2EE (Fourth lesson)

Writing Enterprise Applications with J2EE (Fourth lesson). Alessio Bechini June 2002 (based on material by Monica Pawlan). JSPs let you put segments of servlet code directly into a static HTML page.

Télécharger la présentation

Writing Enterprise Applications with J2EE (Fourth lesson)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WritingEnterprise Applications with J2EE(Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

  2. JSPs let you put segments of servlet code directly into a static HTML page. When the JSP Page is loaded by a browser, the servlet code executes and the application server creates, compiles, loads, and runs a background servlet to execute the servlet code segments and return an HTML page or print an XML report. This lesson changes the WAR file to use a JSP Page instead of BonusServlet. Required steps: Create the JSP Page Change bonus.html Remove the WAR File and create a new one Verify, deploy and run the J2EE Application Using JSPs instead of Servlets

  3. A JSP Page looks like an HTML page with servlet code segments embedded between various forms of leading (<%) and closing (%>) JSP tags. There are no HttpServlet methods such as init, doGet, or doPost. Instead, the code that would normally be in these methods is embedded directly in the JSP Page using JSP scriptlet tags. The following Bonus.jsp is equivalent to BonusServlet. Note that JSP tags cannot be nested. For example, you cannot nest a JSP comment tag within a JSP scriptlet tag. Create the JSP Page

  4. <HTML> <HEAD> <TITLE>Bonus Calculation</TITLE> </HEAD> <%-- Comment Scriptlet for import statements <%@ indicates a jsp directive --%> <%@ page import="javax.naming.*" %> <%@ page import="javax.rmi.PortableRemoteObject" %> <%@ page import="Beans.*" %> <%-- Comment Scriptlet to get the parameters,convert string to Integer to int for bonus calculation, and declare/initialize bonus variable. <% indicates a jsp scriptlet --%> <%! String strMult, socsec; %> <%! Integer integerMult; %> <%! int multiplier; %> <%! double bonus; %> <% strMult = request.getParameter("MULTIPLIER"); socsec = request.getParameter("SOCSEC"); integerMult = new Integer(strMult); multiplier = integerMult.intValue(); bonus = 100.00; %> <%-- Comment Scriptlet to look up session Bean --%> <% InitialContext ctx = new InitialContext(); Object objref = ctx.lookup("calcs"); CalcHome homecalc = (CalcHome) PortableRemoteObject.narrow(objref, CalcHome.class); %> Bonus.jsp (I)

  5. <%-- Comment Scriptlet to create session Bean, call calcBonus method, and retrieve a database record by the social security number (primary key) --%> <% try { Calc theCalculation = homecalc.create(); Bonus theBonus = theCalculation.calcBonus(multiplier,bonus,socsec); Bonus record = theCalculation.getRecord(socsec); %> <%-- Comment HTML code to display retrieved data on returned HTML page. --%> <H1>Bonus Calculation</H1> Social security number retrieved: <%= record.getSocSec() %> <P> Bonus Amount retrieved: <%= record.getBonus() %> <P> <%-- Comment Scriptlet to catch DuplicateKeyException --%> <% } catch (javax.ejb.DuplicateKeyException e) { String message = e.getMessage(); %> <%-- Comment HTML code to display original data passed to JSP on returned HTML page --%> Social security number passed in: <%= socsec %> <P> Multiplier passed in: <%= strMult %> <P> Error: <%= message %> <%-- Comment Scriptlet to close try and catch block --%> <% } %> </BODY> </HTML> Bonus.jsp (II)

  6. The first seven lines of Bonus.jspshow straight HTML followed by a JSP comment. JSP comments are similar to HTML comments except they start with <%--instead of <!--, which is how they look in HTML. You can use either JSP or HTML comments in a JSP file. HTML comments are sent to the client’s web browser where they appear as part of the HTML page, and JSP comments are stripped out and do not appear in the generated HTML. Comments

  7. JSP directives are instructions processed by the JSP engine when the JSP Page is translated to a servlet. The directives used in this example tell the JSP engine to include certain packages and classes. Directives are enclosed by the <%@and %>directive tags. Example of directives in Bonus.jsp: <%@ page import="javax.naming.*" %> <%@ page import="javax.rmi.PortableRemoteObject" %> <%@ page import="Beans.*" %> Directives

  8. JSP declarations let you set up variables for later use in expressions or scriptlets. You can also declare variables within expressions or scriptlets at the time you use them. The scope is the entire JSP Page, so there is no concept of instance variables. That is, you do not have to declare instance variables to be used in more than one expression or scriptlet. Declarations are enclosed by <%! and %>. You can have multiple declarations. For example, <%! double bonus; String text; %>. Declarations

  9. JSP scriptlets let you embed java code segments into the JSP page. The embedded code is inserted directly into the generated servlet that executes when the page is requested. This scriptlet uses the variables declared in the directives described above. Scriptlets are enclosed by the <% and %> scriptlet tags. Example of a scriptlet in Bonus.jsp: <% strMult = request.getParameter("MULTIPLIER"); socsec = request.getParameter("SOCSEC"); integerMult = new Integer(strMult); multiplier = integerMult.intValue(); bonus = 100.00; %> Scriptlets

  10. A scriptlet can use the following predefined variables: session, request, response, out, and in. This example uses the request predefined variable, which is an HttpServletRequestobject. Likewise, responseis an HttpServletResponse object, out is a PrintWriter object, and in is a BufferedReader object. Predefined variables are used in scriptlets in the same way they are used in servlets, except you do not declare them. Example of use of predefined variables in Bonus.jsp: <% strMult = request.getParameter("MULTIPLIER"); socsec = request.getParameter("SOCSEC"); ... Predefined Variables

  11. JSP expressions let you dynamically retrieve or calculate values to be inserted directly into the JSP Page. In this example, an expression retrieves the social security number from the Bonus entity bean and puts it on the JSP page. Example of use of expressions in Bonus.jsp: <H1>Bonus Calculation</H1> Social security number retrieved: <%= record.getSocSec() %> <P> Bonus Amount retrieved: <%= record.getBonus() %> <P> Expressions

  12. The JavaServer Pages 1.1 specification defines JSP-specific tags that let you extend the JSP implementation with new features and hide complexity from visual designers who need to look at the JSP page and modify it. The JSP example in this lesson does not use any JSP-specific tags. The JSP-specific tags defined in the 1.1 specification are the following: jsp:forwardand jsp:includeto instruct the JSP engine to switch from the current page to another JSP page. jsp:useBean, jsp:setProperty, and jsp:getPropertylet you embed and use JavaBeans technology inside a JSP Page. jsp:pluginautomatically downloads the appropriate Java Plug-In to the client to execute an applet with the correct Java platform. JSP-Specific Tags

  13. <HTML> <BODY BGCOLOR = "WHITE"> <BLOCKQUOTE> <H3>Bonus Calculation</H3> <FORM METHOD="GET“ACTION="Bonus.jsp"> <P>Enter social security Number:<P> <INPUT TYPE="TEXT" NAME="SOCSEC"></INPUT> <P>Enter Multiplier:<P> <INPUT TYPE="TEXT" NAME="MULTIPLIER"></INPUT> <P> <INPUT TYPE="SUBMIT" VALUE="Submit"> <INPUT TYPE="RESET"> </FORM> </BLOCKQUOTE> </BODY> </HTML> Change bonus.html The only change you need to make to bonus.html is to have the ACTION parameter in theHTML form invoke Bonus.jspinstead of BonusServlet.

  14. Create a new Web Component Upon the removal of the old WAR file, we have to make a new one containing both Bonus.jspand bonus.html. WAR display name: BonusWar Component file: Bonus.jsp

  15. Run the J2EE Appl. The web server runs on port 8000 by default. To open the bonus.html page point yourbrowser to http://localhost:8000/JSPRoot/bonus.html which is where the Deploytool put the HTML file.

More Related