Docsity

J2EE Training: Servlets, JSP, and Java Code in JSP Pages - Prof. Stefan A. Gremalschi, Study notes of Computer Science

Toggle sidebar
bg1
2
© 2007 Marty Hall
Customized J2EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF, EJB3, Ajax, Java 5, Java 6, etc. Ruby/Rails coming soon.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Customized J2EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF, Hibernate, Ajax, Java 5, Java 6, etc.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Invoking Java Code
with JSP Scripting
Elements
3
© 2007 Marty Hall
Customized J2EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF, EJB3, Ajax, Java 5, Java 6, etc. Ruby/Rails coming soon.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Customized J2EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF, Hibernate, Ajax, Java 5, Java 6, etc.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
For live Java training, please see training courses
at http://courses.coreservlets.com/. Servlets, JSP,
Struts, JSF, Ajax, Java 5, Java 6, and customized
combinations of topics. Ruby/Rails coming soon.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP, and this tutorial. Available at
public venues, or customized versions can be held
on-site at
y
our or
g
anization.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download J2EE Training: Servlets, JSP, and Java Code in JSP Pages - Prof. Stefan A. Gremalschi and more Study notes Computer Science in PDF only on Docsity!

2

© 2007 Marty Hall

Customized J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, EJB3, Ajax, Java 5, Java 6, etc. Ruby/Rails coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Customized J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, Hibernate, Ajax, Java 5, Java 6, etc. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Invoking Java Code

with JSP Scripting

Elements

3

© 2007 Marty Hall

Customized J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, EJB3, Ajax, Java 5, Java 6, etc. Ruby/Rails coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Customized J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, Hibernate, Ajax, Java 5, Java 6, etc. Developed and taught by well-known author and developer. At public venues or onsite at your location.

For live Java training, please see training courses

at http://courses.coreservlets.com/. Servlets, JSP,

Struts, JSF, Ajax, Java 5, Java 6, and customized

combinations of topics. Ruby/Rails coming soon.

Taught by the author of Core Servlets and JSP , More

Servlets and JSP , and this tutorial. Available at

public venues, or customized versions can be held

on-site at your organization.

(^4) J2EE training: http://courses.coreservlets.com

Agenda

  • Static vs. dynamic text
  • Dynamic code and good JSP design
  • JSP expressions
  • Servlets vs. JSP pages for similar tasks
  • JSP scriptlets
  • JSP declarations
  • Predefined variables
  • Comparison of expressions, scriptlets, and declarations

Uses of JSP Constructs

  • Scripting elements calling servlet code directly
  • Scripting elements calling servlet code indirectly (by means of utility classes)
  • Beans
  • Servlet/JSP combo (MVC)
  • MVC with JSP expression language
  • Custom tags
  • MVC with beans, custom tags, and a framework like Struts or JSF

Simple

Application

Complex

Application

(^8) J2EE training: http://courses.coreservlets.com

Types of Scripting Elements

  • Expressions
    • Format: <%= expression %>
    • Evaluated and inserted into the servlet’s output. I.e., results in something like out.print(expression)
  • Scriptlets
    • Format: <% code %>
    • Inserted verbatim into the servlet’s _jspService method (called by service)
  • Declarations
    • Format: <%! code %>
    • Inserted verbatim into the body of the servlet class, outside of any existing methods

JSP Expressions

  • Format
    • <%= Java Expression %>
  • Result
    • Expression evaluated, converted to String, and placed into HTML page at the place it occurred in JSP page
    • That is, expression placed in _jspService inside out.print
  • Examples
    • Current time: <%= new java.util.Date() %>
    • Your hostname: <%= request.getRemoteHost() %>
  • XML-compatible syntax
    • Java Expression
    • You cannot mix versions within a single page. You must use XML for entire page if you use jsp:expression.

(^10) J2EE training: http://courses.coreservlets.com

JSP/Servlet Correspondence

  • Original JSP A Random Number <%= Math.random() %>
  • Representative resulting servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); JspWriter out = response.getWriter(); out.println("A Random Number"); out.println(Math.random()); ... }

JSP Expressions: Example

…

JSP Expressions

Current time: <%= new java.util.Date() %> Server: <%= application.getServerInfo() %> Session ID: <%= session.getId() %> The testParam form parameter: <%= request.getParameter("testParam") %>

(^14) J2EE training: http://courses.coreservlets.com

Reading Three Params

(Servlet): Result

(^15) J2EE training: http://courses.coreservlets.com

Comparing Servlets to JSP:

Reading Three Params (JSP)

Reading Three Request Parameters

Reading Three Request Parameters

param1: <%= request.getParameter("param1") %> param2: <%= request.getParameter("param2") %> param3: <%= request.getParameter("param3") %>

(^16) J2EE training: http://courses.coreservlets.com

Reading Three Params

(Servlet): Result

JSP Scriptlets

  • Format
    • <% Java Code %>
  • Result
    • Code is inserted verbatim into servlet's _jspService
  • Example
    • <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %>
    • <% response.setContentType("text/plain"); %>
  • XML-compatible syntax
    • Java Code

(^20) J2EE training: http://courses.coreservlets.com

JSP Scriptlets: Example

Color Testing

<% String bgColor = request.getParameter("bgColor"); if ((bgColor == null)||(bgColor.trim().equals(""))){ bgColor = "WHITE"; } %>

Testing a Background of "<%= bgColor %>"

JSP Scriptlets: Result

(^22) J2EE training: http://courses.coreservlets.com

Using Scriptlets to Make Parts

of the JSP File Conditional

  • Point
    • Scriplets are inserted into servlet exactly as written
    • Need not be complete Java expressions
    • Complete expressions are usually clearer and easier to maintain, however
  • Example
    • <% if (Math.random() < 0.5) { %> Have a nice day! <% } else { %> Have a lousy day! <% } %>
  • Representative result
    • if (Math.random() < 0.5) { out.println("Have a nice day!"); } else { out.println("Have a lousy day!"); }

JSP Declarations

  • Format
    • <%! Java Code %>
  • Result
    • Code is inserted verbatim into servlet's class definition, outside of any existing methods
  • Examples
    • <%! private int someField = 5; %>
    • <%! private void someMethod(...) {...} %>
  • Design consideration
    • Fields are clearly useful. For methods, it is usually better to define the method in a separate Java class.
  • XML-compatible syntax
    • Java Code

(^26) J2EE training: http://courses.coreservlets.com

JSP Declarations: Example

JSP Declarations

JSP Declarations

<%! private int accessCount = 0; %>

Accesses to page since server reboot:

<%= ++accessCount %>

JSP Declarations: Result

(^28) J2EE training: http://courses.coreservlets.com

JSP Declarations: the

jspInit and jspDestroy Methods

  • JSP pages, like regular servlets, sometimes want to use init and destroy
  • Problem: the servlet that gets built from the JSP page might already use init and destroy - Overriding them would cause problems. - Thus, it is illegal to use JSP declarations to declare init or destroy.
  • Solution: use jspInit and jspDestroy.
    • The auto-generated servlet is guaranteed to call these methods from init and destroy, but the standard versions of jspInit and jspDestroy are empty (placeholders for you to override).

JSP Declarations and

Predefined Variables

  • Problem
    • The predefined variables (request, response, out, session, etc.) are local to the _jspService method. Thus, they are not available to methods defined by JSP declarations or to methods in helper classes. What can you do about this?
  • Solution: pass them as arguments. E.g.

private void someMethod(HttpSession s) {

doSomethingWith(s);

<% someMethod(session); %>

  • Note that the println method of JspWriter throws IOException - Use “throws IOException” for methods that use println

(^32) J2EE training: http://courses.coreservlets.com

Task 1: JSP Expressions

(Code)

Random Numbers

Random Numbers

<%= coreservlets.RanUtilities.randomInt(10) %> <%= coreservlets.RanUtilities.randomInt(10) %> <%= coreservlets.RanUtilities.randomInt(10) %> <%= coreservlets.RanUtilities.randomInt(10) %> <%= coreservlets.RanUtilities.randomInt(10) %>

Task 1: JSP Expressions

(Result)

(^34) J2EE training: http://courses.coreservlets.com

Task 2: JSP Scriptlets

(Code: Version 1)

Random List (Version 1)

Random List (Version 1)

<% int numEntries = coreservlets.RanUtilities.randomInt(10); for(int i=0; i" + coreservlets.RanUtilities.randomInt(10)); } %>

Task 2: JSP Scriptlets

(Result: Version 1)

(^38) J2EE training: http://courses.coreservlets.com

Task 3: JSP Declarations

(Code)

Semi-Random Number

<%! private int randomNum = coreservlets.RanUtilities.randomInt(10); %> Semi-Random Number:<%= randomNum %>

Task 3: JSP Declarations

(Result)

(^40) J2EE training: http://courses.coreservlets.com

Summary

  • JSP Expressions
    • Format: <%= expression %>
    • Wrapped in out.print and inserted into _jspService
  • JSP Scriptlets
    • Format: <% code %>
    • Inserted verbatim into the servlet’s _jspService method
  • JSP Declarations
    • Format: <%! code %>
    • Inserted verbatim into the body of the servlet class
  • Predefined variables
    • request, response, out, session, application
  • Limit the Java code that is directly in page
    • Use helper classes, beans, servlet/JSP combo (MVC),

JSP expression language, custom tags

41

© 2007 Marty Hall

Customized J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, EJB3, Ajax, Java 5, Java 6, etc. Ruby/Rails coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Customized J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, Hibernate, Ajax, Java 5, Java 6, etc. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Questions?