Beruflich Dokumente
Kultur Dokumente
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
FPT Aptech
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
Page 2 of 10
FPT Aptech
Steps to create the web application using flex taglib LoginFlex Right click on WebContent New JSP
Page 3 of 10
FPT Aptech
Page 4 of 10
FPT Aptech
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>
Page 5 of 10
FPT Aptech
<mx:FormItem> <mx:Button </mx:FormItem> </mx:Form> </mx:Panel>
<%-- 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 {
Page 6 of 10
FPT Aptech
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>
Page 7 of 10
FPT Aptech
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)
Page 8 of 10
FPT Aptech
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
Page 9 of 10
FPT Aptech
Otherwise, it shows
END
By Kiu Trng Khnh, M.Eng Page 10 of 10