Chapter 2: Using Objects - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Chapter 2: Using Objects

Description:

body bgcolor='white' jsp:useBean id='clock' class ... body bgcolor='white' % if (request.getHeader('User-Agent').indexOf ... body bgcolor='white' form ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 40
Provided by: john1388
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2: Using Objects


1
Lecture 12
Introducing JAVA SERVER Pages
2
JSP Application Development
  • Using Scripting Elements

3
Implicit JSP Objects
  • When you use scripting elements in a JSP page,
    you always have access to a number of objects
    that JSP container makes available.
  • Note All variable names listed on next page are
    reserved for the implicit objects. If you declare
    your own variables in a JSP page, you must not
    use these reserved names for other variables.

4
Implicit JSP Objects
5
Using JavaBeans Properties
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltbody bgcolor"white"gt
  • ltjspuseBean id"clock" class"java.util.Date" /gt
  • lt if (clock.getHours() lt 12) gt
  • Good morning!
  • lt else if (clock.getHours() lt 17) gt
  • Good day!
  • lt else gt
  • Good evening!
  • lt gt
  • lt/bodygt
  • lt/htmlgt

6
Using Request Information
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltbody bgcolor"white"gt
  • lt if (request.getHeader("User-Agent").indexOf("MS
    IE") ! -1) gt
  • You're using Internet Explorer.
  • lt
  • else
  • if (request.getHeader("User-Agent").indexOf("
    Mozilla") ! 1)
  • gt
  • You're using Netscape.
  • lt else gt
  • You're using a browser I don't know about.
  • lt gt
  • lt/bodygt
  • lt/htmlgt

7
Working with Arrays
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltbody bgcolor"white"gt
  • ltform action"loop.jsp"gt
  • ltinput type"checkbox" name"fruits"
    value"Apple"gtAppleltbrgt
  • ltinput type"checkbox" name"fruits"
    value"Banana"gtBananaltbrgt
  • ltinput type"checkbox" name"fruits"
    value"Orange"gtOrangeltbrgt
  • ltinput type"submit" value"Enter"gt
  • lt/formgt

8
  • lt
  • String picked request.getParameterValues("
    fruits")
  • if (picked ! null picked.length ! 0)
  • gt
  • You picked the following fruits
  • ltulgt
  • lt
  • for (int i 0 i lt picked.length i)
  • out.println("ltligt" pickedi)
  • gt
  • lt/ulgt
  • lt gt
  • lt/bodygt
  • lt/htmlgt

9
Displaying values
  • A JSP expression element can be used instead of
    the
  • ltjsp getPropertygt action element in some places.
  • An expression starts with lt and ends with
    gt
  • The result of the expression is written to the
    response body.
  • The code in an expression must not end with
    semicolon.

10
userInfo4.java
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lt_at_ page import"com.ora.jsp.util." gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtUser Info Entry Formlt/titlegt
  • lt/headgt
  • ltbody bgcolor"white"gt
  • ltjspuseBean id"userInfo" class"com.ora.jsp.
    beans.userinfo.UserInfoBean"gt
  • ltjspsetProperty name"userInfo"
    property"" /gt
  • lt/jspuseBeangt
  • lt-- Output list of values with invalid
    format, if any --gt
  • ltfont color"red"gt
  • ltjspgetProperty name"userInfo"
    property"propertyStatusMsg" /gt
  • lt/fontgt

11
  • lt-- Output form with submitted valid values --gt
  • ltform action"userinfo4.jsp" method"get"gt
  • lttablegt
  • lttrgt
  • lttdgtNamelt/tdgt
  • lttdgtltinput type"text" name"userName"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getUserName()) gt" gt
  • lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtBirth Datelt/tdgt
  • lttdgtltinput type"text" name"birthDate"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getBirthDate()) gt" gt
  • lt/tdgt
  • lttdgt(Use format yyyy-mm-dd)lt/tdgt
  • lt/trgt

12
  • lttrgt
  • lttdgtEmail Addresslt/tdgt
  • lttdgtltinput type"text" name"emailAddr"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getEmailAddr()) gt" gt
  • lt/tdgt
  • lttdgt(Use format name_at_company.com)lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtSexlt/tdgt
  • lttdgtltinput type"text" name"sex"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getSex()) gt" gt
  • lt/tdgt
  • lttdgt(Male or female)lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtLucky numberlt/tdgt
  • lttdgtltinput type"text"
    name"luckyNumber"

13
  • value"lt StringFormat.toHTMLString(userInfo.get
    LuckyNumber()) gt"gt
  • lt/tdgt
  • lttdgt(A number between 1 and 100)lt/tdgt
  • lt/trgt
  • lttrgt
  • lttd colspan2gtltinput type"submit"gtlt/tdgt
  • lt/trgt
  • lt/tablegt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

14
Checking off Checkboxes Dynamically
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lt_at_ page import"com.ora.jsp.util." gt
  • lthtmlgt
  • ltbody bgcolor"white"gt
  • ltform action"checkbox.jsp"gt
  • ltinput type"checkbox" name"fruits"
    value"Apple"gtAppleltbrgt
  • ltinput type"checkbox" name"fruits"
    value"Banana"gtBananaltbrgt
  • ltinput type"checkbox" name"fruits"
    value"Orange"gtOrangeltbrgt
  • ltinput type"submit" value"Enter"gt
  • lt/formgt

15
  • lt
  • String picked request.getParameterValues("
    fruits")
  • if (picked ! null picked.length ! 0)
  • gt
  • You picked the following fruits
  • ltformgt
  • ltinput type"checkbox" name"fruits"
    value"Apple"
  • lt ArraySupport.contains(picked,
    "Apple") ?
  • "checked" "" gt gtAppleltbrgt
  • ltinput type"checkbox" name"fruits"
    value"Banana"
  • lt ArraySupport.contains(picked,
    "Banana") ?
  • "checked" "" gt gtBananaltbrgt
  • ltinput type"checkbox" name"fruits"
    value"Orange"
  • lt ArraySupport.contains(picked,
    "Orange") ?
  • "checked" "" gt gtOrangeltbrgt
  • lt/formgt
  • lt gt
  • lt/bodygt
  • lt/htmlgt

16
Using more request Methods
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltbody bgcolor"white"gt
  • The following information was received
  • ltulgt
  • ltligtRequest Method lt request.getMethod()
    gt
  • ltligtRequest URI lt request.getRequestURI()
    gt
  • ltligtRequest Protocol lt request.getProtocol(
    ) gt
  • ltligtServlet Path lt request.getServletPath()
    gt
  • ltligtQuery String lt request.getQueryString()
    gt
  • ltligtServer Name lt request.getServerName()
    gt
  • ltligtServer Port lt request.getServerPort()
    gt
  • ltligtRemote Address lt request.getRemoteAddr(
    ) gt
  • ltligtRemote Host lt request.getRemoteHost()
    gt
  • ltligtBrowser Type lt request.getHeader("User-A
    gent") gt
  • lt/ulgt
  • lt/bodygt
  • lt/htmlgt

17
Declaring Variables and Methods
  • Instance variables and methods can be declared by
    using declaration element but this is not
    recommended.
  • Instance variables may cause problem because
    JSPServlet are multithreaded.

18
Using a Declaration element
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtA page with a counterlt/titlegt
  • lt/headgt
  • ltbody bgcolor"white"gt
  • lt!
  • int globalCounter 0
  • gt
  • This page has been visited lt
    globalCounter gt times.
  • ltpgt
  • lt
  • int localCounter 0
  • gt
  • This counter never increases its value lt
    localCounter gt
  • lt/bodygt
  • lt/htmlgt

19
  • You can also declare a method. Only harm is that
    in that case JSP page will contain too much code
    and may be problem to maintain. Better approach
    is use JavaBeans and custom actions.

20
Method declaration and use
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltbody bgcolor"white"gt
  • lt!
  • String randomColor()
  • java.util.Random random new
    java.util.Random()
  • int red (int) (random.nextFloat() 255)
  • int green (int) (random.nextFloat()
    255)
  • int blue (int) (random.nextFloat()
    255)
  • return ""
  • Integer.toString(red, 16)
  • Integer.toString(green, 16)
  • Integer.toString(blue, 16)
  • gt

21
  • lth1gtRandom Colorlt/h1gt
  • lttable bgcolor"lt randomColor() gt" gt
  • lttrgtlttd width"100" height"100"gtnbsplt/tdgt
    lt/trgt
  • lt/tablegt
  • lt/bodygt
  • lt/htmlgt

22
  • Sharing Data Between JSP Pages, Requests, and
    Users

23
Passing Control and Data Between Pages
  • Model-View-Controller (MVC)
  • Pass control from one page to another.
  • Pass data from one page to another.
  • EXAMPLE USER INFO
  • Display the form for user input (presentation)
  • Validate the input (request processing and
    business logic).
  • Display the result of the validation
    (presentation).
  • Lets use a separate jsp page for each aspect.

24
How it will work
  • The userinfoinput.jsp page dislays an input form
  • The user submit the form to userinputvalidate.jsp
    to validate the input.
  • This page will process the request using the
    UserInfoBean and passes control (forwards) to
  • either the userinfoinput.jsp page (if the input
    is invalid) or
  • the userinfovalid.jsp page ( if the input is
    valid).
  • UserInfoBean represents the Model
  • userinputvalidate.jsp page is the controller
  • userinfoinput.jsp userinfovalid.jsp represents
    the Views.

25
Passing control from one page to other
  • ltjspforward pageuserinfovalid.jsp /gt
  • This action stops processing one page and starts
    processing the page specified by the page
    attribute instead, called the target page. The
    control never returns to the original page.
  • The target page has access to all the parameter
    of the request. You can also add additional
    parameters when you pass control to another page
    by using one or more nested ltjspparamgt action
    element.
  • ltjspforward pageuserinfovalid.jspgt
  • ltjspparam namemsg valueinvalid e-mail
    address /gt
  • lt/jspforwardgt

26
  • The page attribute is interpreted relative to the
    location of the current page if it does not start
    with /.
  • page index.jsp //file in same directory
  • page ../foo/index.jsp //file in different
    directory
  • page /foo/index.jsp //context relative path

27
Passing Data from one Page to another
  • The scope defines for how long the object is
    available and whether its available only to one
    user or to all application users.
  • Page Default scope. Object in this scope only
    available to actions and scriptlets within one
    page.)
  • RequestThis scope is for Objects that needs to
    be available to all pages processing the same
    request.
  • Session This scope is for Objects shared by
    multiple requests by the same user
  • Application This scope is for Objects shared by
    all users of the application.

28
  • ltjspuseBean iduserInfo scoperequest
  • class com.ora.jsp.beans.userinfo.UserInfoBean
    /gt
  • If you want to perform an action only when the
    bean is created, place the elements in the body
    of the ltjspuseBeangt
  • ltjspuseBean iduserInfo scoperequest
  • class com.ora.jsp.beans.userinfo.UserInfoBean
    gt
  • ltjspsetProperty nameuserInfo property
    /gt
  • lt/jspuseBeangt

29
Putting it all togather (userinfoinput.jsp)
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lt_at_ page import"com.ora.jsp.util." gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtUser Info Entry Formlt/titlegt
  • lt/headgt
  • ltbody bgcolor"white"gt
  • ltjspuseBean id"userInfo"
  • class"com.ora.jsp.beans.userinfo.UserInfoBe
    an"
  • scope"request" /gt
  • lt-- Output list of values with invalid format,
    if any --gt
  • ltfont color"red"gt
  • ltjspgetProperty name"userInfo"
    property"propertyStatusMsg" /gt
  • lt/fontgt

30
userinfoinput.jsp
  • lt-- Output form with submitted valid values --gt
  • ltform action"userinfovalidate.jsp"
    method"post"gt
  • lttablegt
  • lttrgt
  • lttdgtNamelt/tdgt
  • lttdgtltinput type"text" name"userName"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getUserName()) gt" gt
  • lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtBirth Datelt/tdgt
  • lttdgtltinput type"text" name"birthDate"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getBirthDate()) gt" gt

31
  • lt/tdgt
  • lttdgt(Use format yyyy-mm-dd)lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtEmail Addresslt/tdgt
  • lttdgtltinput type"text" name"emailAddr"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getEmailAddr()) gt" gt
  • lt/tdgt
  • lttdgt(Use format name_at_company.com)lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtSexlt/tdgt
  • lttdgtltinput type"text" name"sex"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getSex()) gt" gt
  • lt/tdgt

32
  • lttdgt(Male or female)lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtLucky numberlt/tdgt
  • lttdgtltinput type"text"
    name"luckyNumber"
  • value"lt StringFormat.toHTMLString(u
    serInfo.getLuckyNumber()) gt"gt
  • lt/tdgt
  • lttdgt(A number between 1 and 100)lt/tdgt
  • lt/trgt
  • lttrgt
  • lttd colspan2gtltinput type"submit"gtlt/tdgt
  • lt/trgt
  • lt/tablegt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

33
Userinfovalidate.jsp
  • lt_at_ page language"java" gt
  • ltjspuseBean id"userInfo" scope"request"
  • class"com.ora.jsp.beans.userinfo.UserInfoBean"
    gt
  • ltjspsetProperty name"userInfo" property""
    /gt
  • lt/jspuseBeangt
  • lt if (userInfo.isValid()) gt
  • ltjspforward page"userinfovalid.jsp" /gt
  • lt else gt
  • ltjspforward page"userinfoinput.jsp" /gt
  • lt gt

34
Userinfovalid.jsp
  • lthtmlgt
  • ltheadgt
  • lttitlegtUser Info Validatedlt/titlegt
  • lt/headgt
  • ltbody bgcolor"white"gt
  • ltfont colorgreen size3gt
  • Thanks for entering valid information!
  • lt/fontgt
  • lt/bodygt
  • lt/htmlgt

35
Sharing Sessions (counter1.jsp
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtCounter pagelt/titlegt
  • lt/headgt
  • ltbody bgcolor"white"gt
  • ltjspuseBean
  • id"sessionCounter"
  • scope"session"
  • class"com.ora.jsp.beans.counter.CounterBean
    "
  • /gt
  • ltjspuseBean
  • id"applCounter"
  • scope"application"
  • class"com.ora.jsp.beans.counter.CounterBean
    "
  • /gt
  • lt String uri request.getRequestURI() gt

36
  • lth1gtCounter pagelt/h1gt
  • This page has been visited ltbgt
  • lt sessionCounter.getNextValue(uri) gt
  • lt/bgt times by the current user in the current
    session, and ltbgt
  • lt applCounter.getNextValue(uri) gt
  • lt/bgt times by all users since the application
    was started.
  • lt/bodygt
  • lt/htmlgt

37
URL rewriting
  • lt_at_ page language"java" contentType"text/html"
    gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtCounter page 1lt/titlegt
  • lt/headgt
  • ltbody bgcolor"white"gt
  • ltjspuseBean
  • id"sessionCounter"
  • scope"session"
  • class"com.ora.jsp.beans.counter.CounterBean
    "
  • /gt
  • lt String uri request.getRequestURI() gt
  • lth1gtCounter page 1lt/h1gt
  • This page has been visited ltbgt
  • lt sessionCounter.getNextValue(uri) gt

38
  • lt/bgt times by the current user in the current
    session.
  • ltpgt
  • Click here to get to
  • lta href"lt response.encodeURL("counter3.jsp"
    ) gt"gt
  • Counter page 2lt/agt.
  • lt/bodygt
  • lt/htmlgt

39
Memory management
  • Place only those objects in session that are
    required
  • Set the time out period for sessions to a lower
    value
  • session.setMaxInactiveInterval( )
  • Provide a way to end session explicitly. (logout)
  • session.invalidate( )
Write a Comment
User Comments (0)
About PowerShow.com