CSE 510 Web Data Engineering The Struts Framework Logon Example - - PowerPoint PPT Presentation

cse 510 web data engineering
SMART_READER_LITE
LIVE PREVIEW

CSE 510 Web Data Engineering The Struts Framework Logon Example - - PowerPoint PPT Presentation

CSE 510 Web Data Engineering The Struts Framework Logon Example UB CSE 510 Web Data Engineering Example The example implements a dummy logon functionality Do not consider this example to be the best way to implement authorization and


slide-1
SLIDE 1

CSE 510 Web Data Engineering

The Struts Framework Logon Example

UB CSE 510 Web Data Engineering

slide-2
SLIDE 2

UB CSE 510 Web Data Engineering 2

Example

  • The example implements a dummy logon

functionality

– Do not consider this example to be the best way to implement authorization and access control – The example is used only to demonstrate the Struts framework – Appropriate authorization and access control will be covered in a separate lecture shortly

slide-3
SLIDE 3

UB CSE 510 Web Data Engineering 3

WorkFlow

welcome ¡ !validate ¡

Welcome.do ¡ Logon.do ¡ LogonSubmit.do ¡ logonForm ¡ Start ¡

forward ¡ Sign ¡out ¡ (hyperlink) ¡ success ¡

Welcome.jsp ¡

Sign ¡in ¡ (hyperlink) ¡

Logon.jsp ¡

forward ¡ success ¡

Logoff.do ¡

Submit ¡ (buBon) ¡

slide-4
SLIDE 4

UB CSE 510 Web Data Engineering 4

Session ¡Scope ¡

user: ¡logonForm ¡

WorkFlow

UserDirectory ¡ isValidPassword(String ¡username, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡password) ¡

get ¡ set ¡ remove ¡ welcome ¡ !validate ¡

Welcome.do ¡ Logon.do ¡ LogonSubmit.do ¡ logonForm ¡ Start ¡

forward ¡ Sign ¡out ¡ (hyperlink) ¡ success ¡

Welcome.jsp ¡

Sign ¡in ¡ (hyperlink) ¡

Logon.jsp ¡

forward ¡ success ¡

Logoff.do ¡

Submit ¡ (buBon) ¡

slide-5
SLIDE 5

UB CSE 510 Web Data Engineering 5

/index.jsp

<%@ ¡taglib ¡uri="hBp://struts.apache.org/tags-­‑logic" ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prefix="logic" ¡%> ¡ <logic:redirect ¡forward="welcome"/> ¡ <%-­‑-­‑ ¡ Redirect ¡default ¡requests ¡to ¡Welcome ¡global ¡AcRonForward. ¡

  • ­‑-­‑%> ¡
slide-6
SLIDE 6

UB CSE 510 Web Data Engineering 6

/pages/Welcome.jsp

<%@ ¡taglib ¡uri="hBp://struts.apache.org/tags-­‑bean" ¡prefix="bean"%> ¡ <%@ ¡taglib ¡uri="hBp://struts.apache.org/tags-­‑html" ¡prefix="html"%> ¡ <%@ ¡taglib ¡uri="hBp://struts.apache.org/tags-­‑logic" ¡prefix="logic"%> ¡ <html:html> ¡ <head> ¡ ¡ ¡ ¡ ¡<Rtle><bean:message ¡key="app.Atle" ¡/></Rtle> ¡ ¡ ¡ ¡ ¡<html:base ¡/> ¡ </head> ¡ <body> ¡ <logic:present ¡name="user"> ¡ ¡ ¡ ¡ ¡<h3>Welcome ¡<bean:write ¡name="user" ¡property="username" ¡/>!</h3> ¡ </logic:present> ¡ … ¡

slide-7
SLIDE 7

UB CSE 510 Web Data Engineering 7

/pages/Welcome.jsp (cont’d)

... ¡ <logic:notPresent ¡scope="session" ¡name="user"> ¡ ¡ ¡ ¡ ¡<h3><bean:message ¡key="welcome.heading" ¡/></h3> ¡ </logic:notPresent> ¡ <html:errors ¡/> ¡ <ul> ¡ ¡ ¡ ¡ ¡<li><html:link ¡forward="logon">Sign ¡in</html:link></li> ¡ ¡ ¡ ¡ ¡<logic:present ¡name="user"> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<li><html:link ¡forward="logoff">Sign ¡out</html:link></li> ¡ ¡ ¡ ¡ ¡</logic:present> ¡ </ul> ¡ </body> ¡ </html:html> ¡

slide-8
SLIDE 8

UB CSE 510 Web Data Engineering 8

Edge Labels In struts-config.xml

<!-­‑-­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:</th> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<td><html:password ¡property="password" ¡/></td></tr> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<tr><td ¡align="right"><html:submit ¡/></td> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<td><html:reset ¡/></td></tr> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡</table> ¡ ¡ ¡ ¡ ¡</html:form> ¡ </body> ¡ … ¡

slide-10
SLIDE 10

UB CSE 510 Web Data Engineering 10

Associating the ActionForm Bean with the HTML Form

<acRon ¡ ¡ ¡ ¡ ¡ ¡path="/LogonSubmit” ¡ ¡ ¡ ¡ ¡type="app.LogonAcAon" ¡ ¡ ¡ ¡ ¡name="logonForm" ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡scope="request" ¡ ¡ ¡ ¡ ¡validate="true" ¡ ¡ ¡ ¡ ¡input="/pages/Logon.jsp"> ¡ ¡ ¡ ¡ ¡<forward ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡name="success" ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡path="/pages/Welcome.jsp"/> ¡ </acRon> ¡

slide-11
SLIDE 11

UB CSE 510 Web Data Engineering 11

Form Beans Also Provide Values

/pages/Logon.jsp … ¡ ¡ ¡ ¡ ¡<h3><bean:message ¡key=”logon.heading" ¡/></h3> ¡ ¡ ¡ ¡ ¡<html:form ¡acAon="/LogonSubmit" ¡focus="username"> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<table ¡width="100%"> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<tr><th ¡align="right">Username:</th> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<td><html:text ¡property="username" ¡/></td></tr> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<tr><th ¡align="right">Password:</th> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<td><html:password ¡property="password" ¡/></td></tr> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<tr><td ¡align="right"><html:submit ¡/></td> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<td><html:reset ¡/></td></tr> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡</table> ¡ ¡ ¡ ¡ ¡</html:form> ¡ … ¡

slide-12
SLIDE 12

UB CSE 510 Web Data Engineering 12

Typical Code Of A LogonForm Bean

public ¡final ¡class ¡LogonForm ¡extends ¡AcRonForm ¡{ ¡ ¡ ¡ ¡ ¡private ¡String ¡password ¡= ¡null; ¡ ¡ ¡ ¡ ¡private ¡String ¡username ¡= ¡null; ¡ ¡ ¡ ¡ ¡public ¡String ¡getPassword() ¡{ ¡return ¡(this.password); ¡} ¡ ¡ ¡ ¡ ¡public ¡void ¡setPassword(String ¡password) ¡{ ¡this.password ¡= ¡password; ¡} ¡ ¡ ¡ ¡ ¡public ¡String ¡getUsername() ¡{ ¡return ¡(this.username); ¡} ¡ ¡ ¡ ¡ ¡public ¡void ¡setUsername(String ¡username) ¡{ ¡this.username ¡= ¡username; ¡} ¡ ¡ ¡ ¡ ¡public ¡void ¡reset(AcRonMapping ¡mapping, ¡HBpServletRequest ¡request) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡setPassword(null); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡setUsername(null); ¡ ¡ ¡ ¡ ¡} ¡ } ¡

slide-13
SLIDE 13

UB CSE 510 Web Data Engineering 13

The Art of Balancing How Many Actions & JSPs to Use

  • Consider the “logon” application
  • We could have one JSP for each kind of login error
  • However, we will see technologies that help

consolidate within a few JSPs

– Form validation features – Logic tag library

  • Deciding the number of actions and JSPs is an art
  • f design – not a science

– Examples, practice, then more practice…

slide-14
SLIDE 14

UB CSE 510 Web Data Engineering 14

Validation

public ¡AcRonErrors ¡validate(AcRonMapping ¡mapping, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡HBpServletRequest ¡request) ¡{ ¡ ¡ ¡ ¡ ¡AcRonErrors ¡errors ¡= ¡new ¡AcRonErrors(); ¡ ¡ ¡ ¡ ¡if ¡((username ¡== ¡null) ¡|| ¡(username.length() ¡< ¡1)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡errors.add("username", ¡new ¡AcRonMessage("error.username.required")); ¡ ¡ ¡ ¡ ¡if ¡((password ¡== ¡null) ¡|| ¡(password.length() ¡< ¡1)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡errors.add("password", ¡new ¡AcRonMessage("error.password.required")); ¡ ¡ ¡ ¡ ¡return ¡errors; ¡ } ¡

slide-15
SLIDE 15

UB CSE 510 Web Data Engineering 15

Resource File & Internationalization

MessageResources.properties

app.Rtle=Struts ¡Logon ¡ApplicaRon ¡ welcome.heading=Welcome ¡User! ¡ logon.heading=Sign ¡in, ¡Please! ¡ errors.header=<h3><font ¡color="red">ValidaRon ¡Error</font></h3>You ¡must… ¡ errors.prefix=<li> ¡ errors.suffix=</li> ¡ errors.footer=</ul><hr> ¡ error.username.required=Username ¡is ¡required ¡ error.password.required=Password ¡is ¡required ¡ error.logon.invalid=Username ¡and ¡password ¡provided ¡not ¡found ¡in ¡user… ¡ error.logon.connect=Could ¡not ¡connect ¡to ¡user ¡directory. ¡ … ¡

slide-16
SLIDE 16

UB CSE 510 Web Data Engineering 16

Action Bean LogonAction.java

package ¡app; ¡ import ¡java.io.IOExcepRon; ¡ import ¡javax.servlet.ServletExcepRon; ¡ import ¡javax.servlet.hBp.HBpServletRequest; ¡ import ¡javax.servlet.hBp.HBpServletResponse; ¡ import ¡javax.servlet.hBp.HBpSession; ¡ import ¡org.apache.struts.acRon.AcRon; ¡ import ¡org.apache.struts.acRon.AcRonErrors; ¡ import ¡org.apache.struts.acRon.AcRonForm; ¡ import ¡org.apache.struts.acRon.AcRonForward; ¡ import ¡org.apache.struts.acRon.AcRonMapping; ¡ import ¡org.apache.struts.acRon.AcRonMessage; ¡ import ¡org.apache.struts.acRon.AcRonMessages; ¡ public ¡final ¡class ¡LogonAcRon ¡extends ¡AcRon ¡{ ¡ … ¡

slide-17
SLIDE 17

UB CSE 510 Web Data Engineering 17

Action Bean LogonAction.java

… ¡ /** ¡ ¡* ¡Validate ¡credenRals ¡with ¡business ¡Rer. ¡ ¡* ¡ ¡ ¡* ¡@param ¡username ¡The ¡username ¡credenRal ¡ ¡* ¡@param ¡password ¡The ¡password ¡credenRal ¡ ¡* ¡@returns ¡true ¡if ¡credenRals ¡can ¡be ¡validated ¡ ¡* ¡@excepRon ¡UserDirectoryExcepRon ¡if ¡cannot ¡access ¡directory ¡ ¡*/ ¡ public ¡boolean ¡isUserLogon(String ¡username, ¡String ¡password) ¡ ¡ ¡throws ¡UserDirectoryExcepRon ¡{ ¡ ¡return ¡(UserDirectory.getInstance().isValidPassword(username, ¡password)); ¡ ¡// ¡return ¡true; ¡ } ¡ … ¡

slide-18
SLIDE 18

UB CSE 510 Web Data Engineering 18

Action Bean LogonAction.java

… ¡ public ¡AcRonForward ¡execute(AcRonMapping ¡mapping, ¡AcRonForm ¡form, ¡ ¡HBpServletRequest ¡request, ¡HBpServletResponse ¡response) ¡throws ¡ExcepRon ¡{ ¡ ¡// ¡Obtain ¡username ¡and ¡password ¡from ¡web ¡Rer ¡ ¡String ¡username ¡= ¡((LogonForm) ¡form).getUsername(); ¡ ¡String ¡password ¡= ¡((LogonForm) ¡form).getPassword(); ¡ ¡// ¡Validate ¡credenRals ¡with ¡business ¡Rer ¡ ¡boolean ¡validated ¡= ¡false; ¡ ¡try ¡{ ¡ ¡ ¡validated ¡= ¡isUserLogon(username, ¡password); ¡ ¡} ¡catch ¡(UserDirectoryExcepRon ¡ude) ¡{ ¡ ¡ ¡// ¡couldn't ¡connect ¡to ¡user ¡directory ¡ ¡ ¡AcRonErrors ¡errors ¡= ¡new ¡AcRonErrors(); ¡ ¡ ¡errors.add(…, ¡new ¡AcRonMessage("error.logon.connect")); ¡ ¡ ¡saveErrors(request, ¡errors); ¡ ¡ ¡// ¡return ¡to ¡input ¡page ¡ ¡ ¡return ¡(new ¡AcRonForward(mapping.getInput())); ¡ ¡} ¡ ¡… ¡

slide-19
SLIDE 19

UB CSE 510 Web Data Engineering 19

Action Bean LogonAction.java

¡… ¡ ¡if ¡(!validated) ¡{ ¡ ¡ ¡// ¡credenRals ¡don't ¡match ¡ ¡ ¡AcRonErrors ¡errors ¡= ¡new ¡AcRonErrors(); ¡ ¡ ¡errors.add(…, ¡new ¡AcRonMessage("error.logon.invalid")); ¡ ¡ ¡saveErrors(request, ¡errors); ¡ ¡ ¡// ¡return ¡to ¡input ¡page ¡ ¡ ¡return ¡(new ¡AcRonForward(mapping.getInput())); ¡ ¡} ¡ ¡// ¡Save ¡our ¡logged-­‑in ¡user ¡in ¡the ¡session, ¡ ¡// ¡because ¡we ¡use ¡it ¡again ¡later. ¡ ¡HBpSession ¡session ¡= ¡request.getSession(); ¡ ¡session.setABribute(Constants.USER_KEY, ¡form); ¡ ¡// ¡Return ¡success ¡ ¡return ¡mapping.findForward(Constants.SUCCESS); ¡ } ¡ } ¡// ¡End ¡LogonAcRon ¡

slide-20
SLIDE 20

UB CSE 510 Web Data Engineering 20

WorkFlow Variance

welcome ¡ !validate ¡

Welcome.do ¡ Logon.do ¡ LogonSubmit.do ¡ logonForm ¡ Start ¡

forward ¡ success ¡

Welcome.jsp ¡

Sign ¡in ¡ (hyperlink) ¡

Logon.jsp ¡

forward ¡ success ¡

Logoff.do ¡

Submit ¡ (buBon) ¡

Main.jsp ¡

Sign ¡in ¡ (hyperlink) ¡ Sign ¡out ¡ (hyperlink) ¡

slide-21
SLIDE 21

UB CSE 510 Web Data Engineering 21

WorkFlow Variance

  • The two variances share the same actions
  • No modification in Actions source files
  • Only need to change “view” (JSPs) and

“workflow” (struts-config.xml)