Sie sind auf Seite 1von 10

VIN O TO QUC T FPT

FPT-ACADEMY INTERNATIONAL
7 Nam Quc Cang, Qun 1, TP. HCM - in thoi: 9252552 - Fax: 9252550

Creating the Web application on Eclipse using JSP and flex taglib
Step to create application File New Project Web Dynamic Web Project Next

Put Project name: LoginFlex

Server Apache Tomcat v6.0 Next Finish


By Kiu Trng Khnh, M.Eng Page 1 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

Steps to add Library to web project Copy all library from Flexlib

LoginFlex Right click on the WEB-INF in WebContent Paste (Click Yes to all to overwrite the web.xml file

By Kiu Trng Khnh, M.Eng

Page 2 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

Steps to create the web application using flex taglib LoginFlex Right click on WebContent New JSP

Put the file name: login.jsp Next

By Kiu Trng Khnh, M.Eng

Page 3 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

Checking Use JSP template New JSP File (html) Finish

Replace login.jsp with following the code


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="FlexTagLib" prefix="mm" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Flex Demo</title> </head> <body> <center><h1>Flex Demo</h1></center> <mm:mxml> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:events="flash.events.*" backgroundColor="white"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.events.ValidationResultEvent; import mx.controls.Alert; private function validateEmpName():void {

By Kiu Trng Khnh, M.Eng

Page 4 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

if (empNameValidator.validate().type != ValidationResultEvent.VALID){ Alert.show("User name can not be blank"); }else if (passValidator.validate().type != ValidationResultEvent.VALID){ Alert.show("Password can not be blank"); }else{ submitForm(); } } private function submitForm():void { login.cancel(); login.send(loginModel); } private function onResult(event:Event):void{ if(login.lastResult.people.person1.name1=="notpermission"){ Alert.show("Invalid Password or Username"); } else{ ShowLogin.visible=false; ShowDatabase.visible=true; } trace('Got the result'); // works only in the debug mode return; } private function backToLogin():void{ ShowLogin.visible=true; ShowDatabase.visible=false; } ]]> </mx:Script> <mx:HTTPService id="login" useProxy="false" method="POST" url="result.jsp" result="onResult(event)" /> <mx:StringValidator property="text" <mx:StringValidator property="text" id="empNameValidator" source="{uName}" triggerEvent=""/> id="passValidator" source="{pass}" triggerEvent=""/>

<mx:Model id="loginModel"> <root> <userName>{uName.text}</userName> <passWord>{pass.text}</passWord> </root> </mx:Model> <%-- Login Form --%> <mx:Panel id="ShowLogin" title="ShowLogin" x="27" y="35" verticalAlign="center" horizontalAlign="center" visible="true"> <mx:Form width="100%" height="100%"> <mx:FormItem label="Enter user name:" required="true"> <mx:TextInput id="uName"/> </mx:FormItem> <mx:FormItem label="Enter password:" required="true"> <mx:TextInput id="pass" displayAsPassword="true"/> </mx:FormItem>

By Kiu Trng Khnh, M.Eng

Page 5 of 10

FPT Aptech
<mx:FormItem> <mx:Button </mx:FormItem> </mx:Form> </mx:Panel>

Creating Web Application using flex taglib with jsp


label="Login" click="validateEmpName()"/>

<%-- Form contain DataGrid --%> <mx:Panel id="ShowDatabase" title="Showdata" x="27" y="35" verticalAlign="center" horizontalAlign="center" visible="false" width="70%" > <mx:Form width="100%" height="100%"> <mx:DataGrid dataProvider="{login.lastResult.people.person}" width="100%"> <mx:columns> <mx:DataGridColumn dataField="open" headerText="Open" textAlign="left"/> <mx:DataGridColumn dataField="deposit" headerText="Deposits" textAlign="center"/> <mx:DataGridColumn dataField="withdraw" headerText="WithDraws" textAlign="right"/> <mx:DataGridColumn dataField="total" headerText="Totals" textAlign="right"> </mx:DataGridColumn> </mx:columns> </mx:DataGrid> <mx:FormItem> <mx:Button label="Back" click="backToLogin()"/> </mx:FormItem> </mx:Form> </mx:Panel> </mx:Application> </mm:mxml> </body> </html>

LoginFlex Right click on WebContent New JSP Put the file name: result.jsp Next Checking Use JSP template New JSP File (html) Finish Replace login.jsp with following the code

<%@ page import="java.sql.*" %> <?xml version="1.0" encoding="UTF-8"?> <people> <% String name=request.getParameter("userName"); String pass=request.getParameter("passWord"); //connect DB Connection con=null; Connection con1=null; Statement stat=null; Statement stat1=null; try {

By Kiu Trng Khnh, M.Eng

Page 6 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

String query = "Select * from Registration where username='" + name + "' and password='" + pass + "'"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:SQLSvr", "sa", ""); stat=con.createStatement(); ResultSet rst=stat.executeQuery(query); if(rst.next()) { %> <person1><name1>permission</name1></person1> <% con1=DriverManager.getConnection("jdbc:odbc:SQLSvr", "sa", ""); String query1 = "Select * from Saving where Name='" + name + "'"; stat1=con1.createStatement(); ResultSet rst1=stat1.executeQuery(query1); while(rst1.next()) { %> <person> <open><%= rst1.getInt("Open") %></open> <deposit><%= rst1.getInt("Deposits") %></deposit> <withdraw><%= rst1.getInt("Withdraw") %></withdraw> <total><%= rst1.getInt("Total") %></total> </person> <% }//end while }else { %> <person1><name1>notpermission</name1></person1> <% }//end else } catch(ClassNotFoundException e){ e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { if(stat1!=null){ stat1.close(); } stat.close (); con.close (); if(con1!=null){ con1.close(); } } %> </people>

By Kiu Trng Khnh, M.Eng

Page 7 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

Open the web.xml file in the Login Flex (using double click on this file) click design Click + on welcome-file-list replace the login.jsp with index.html in welcomefile Save file (ctrl + S)

Right click on LoginFlex Run As Run On Server

By Kiu Trng Khnh, M.Eng

Page 8 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

Choose an existing server / [Manually define a new server Apache Tomcat v6.0 Server] Next Next (change/ modify/ deploy again a web application to server) check update the context root for Web module LoginFlex), otherwise Finish

The resulting of login screen

By Kiu Trng Khnh, M.Eng

Page 9 of 10

FPT Aptech

Creating Web Application using flex taglib with jsp

If the authentication is ok, the screen shows

Otherwise, it shows

END
By Kiu Trng Khnh, M.Eng Page 10 of 10

Das könnte Ihnen auch gefallen