Groups > Multimedia > Macromedia rich int apps > Re: mxml to servlet communication




mxml to servlet communication

mxml to servlet communication
Wed, 3 May 2006 05:44:18 +0000
Hi i got one problem while commnicating servlet with mxml. 
 I am writing A.mxml and i am checking user validity in that. i am able to 
communicate servlet from a.mxml.
 My problem is from the servlet, if the user is valid i want to rediredt the 
user to B.mxml. Can someone help me on this. 
 When the last statement is executed in servlet.. the data is fwding to the jsp

file also. How to redirect to B.mxml from main.jsp??
  
 my sample code looks like:
 A.mxml
 <?xml version="1.0" encoding="utf-8"?>
 <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" 
styleName="heading" xmlns="*"
initialize="Object.registerClass('LoginTO', 
LoginTO)" height="100%" width="100%">
 <mx:Script source="AS/LoginSend.as" />
  
    <!-- CONNECT TO JSP
  <mx:HTTPService id="mylogin" method="GET"
url="test.jsp" protocol="http" 
resultFormat="object" showBusyCursor="true">
         <mx:request>
             <username>{username.text}</username>
    <password>{password.text}</password>
         </mx:request>
     </mx:HTTPService>
  -->
  <!-- CONNECT TO SERVLET -->
  <mx:HTTPService url="http://localhost:8080/satishfirst/login"
id="mylogin" 
method="GET" resultFormat="object">
  <mx:request>
  <username>{username.text}</username>
  <password>{password.text}</password>
  </mx:request> </mx:HTTPService>
  
  
  <mx:Script>
  import LoginTO;
  function validate() {
   login.username = username.text;
   login.password = password.text;
   log.addUser(login);
  }
  /*
  function validate2() {
   loginjsp.send();
  } */
  </mx:Script>
    <!-- graph -->
    <mx:HTTPService id="srv"
url="linechartdata.xml"/>
    <mx:Model
id="results">{srv.result.data.result}</mx:Model>
    <!-- graph -->
    
  <mx:RemoteObject id="log" named="Logins"
protocol="http" 
showBusyCursor="true">
  </mx:RemoteObject>
  <LoginTO id="login"></LoginTO>
  
  <mx:Model id="registration">
         <username>{username.text}</username>
         <password>{password.text}</password>
     </mx:Model>
  <mx:EmailValidator field="registration.username"/>
  <mx:StringValidator field="registration.password"/>
  
   <mx:Form width="708" height="400">
     <mx:Grid height="329" width="679">
       <mx:GridRow>
         <mx:GridItem width="100">
         </mx:GridItem>
         <mx:GridItem height="150"
horizontalAlign="center" 
verticalAlign="bottom">
           <mx:Label text="User Login" height="31"
styleName="heading" 
color="red" fontSize="16" fontWeight="bold"
width="118"/>
           </mx:GridItem>
         <mx:GridItem width="200" height="150"
horizontalAlign="left">
    <!-- graph -->
    <mx:LineChart id="chart" dataProvider=""
showDataTips="true" 
width="200" height="150">
     <mx:horizontalAxis>
      <mx:CategoryAxis dataProvider=""
categoryField="month"/>
     </mx:horizontalAxis>
     <mx:series>
      <mx:Array>
       <mx:LineSeries yField="apple" name="Apple"/>
       <mx:LineSeries yField="orange" name="Orange"/>
       <mx:LineSeries yField="banana" name="Banana"/>
      </mx:Array>
     </mx:series>
    </mx:LineChart>
    <!-- graph -->
         </mx:GridItem>
       </mx:GridRow>
       <mx:GridRow>
         <mx:GridItem width="100">
         </mx:GridItem>
         <mx:GridItem>
           <mx:Grid>
             <mx:GridRow>
               <mx:GridItem>
                 <mx:Label text="User Name:" />
               </mx:GridItem>
               <mx:GridItem>
       <mx:FormItem required="true">
                 <mx:TextInput text="" id="username"
/>
     </mx:FormItem>
               </mx:GridItem>
             </mx:GridRow>
             <mx:GridRow>
               <mx:GridItem>
                 <mx:Label text="Password:" />
               </mx:GridItem>
               <mx:GridItem>
       <mx:FormItem required="true">
                 <mx:TextInput text="" id="password"
/>
     </mx:FormItem>
               </mx:GridItem>
             </mx:GridRow>
             <mx:GridRow>
               <mx:GridItem>
                 <mx:Button label="Add USer"
click="validate()" />
               </mx:GridItem>
               <mx:GridItem>
                 <!--<mx:Button label="Login"
click="mylogin.send();" />-->
     <!--<mx:Button label="Login" 
click="mx.validators.Validator.isStructureValid(this,'registration');"
/>-->
     <mx:Button label="Login" click="send()" />
               </mx:GridItem>
             </mx:GridRow>
           </mx:Grid>
         </mx:GridItem>
         <mx:GridItem width="100">
         </mx:GridItem>
       </mx:GridRow>
       <mx:GridRow>
         <mx:GridItem width="100" height="100">
         </mx:GridItem>
         <mx:GridItem>
         </mx:GridItem>
         <mx:GridItem width="100">
         </mx:GridItem>
       </mx:GridRow>
     </mx:Grid>
   </mx:Form>
   <mx:Style>
   .heading {
  background-color: #99CC99;
 }
   </mx:Style>
 </mx:Application>

 servlet:
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import javax.servlet.*;
 import java.io.*;
 import java.util.List;
 import java.util.ArrayList;
  

 /**
  * @author satish
  *
  */
 public class LoginAction extends HttpServlet {
  ServletContext context = null;
  public void init(ServletConfig config) throws javax.servlet.ServletException
{
   System.out.println("---------Servlet init()--------------");
   super.init();
         context=config.getServletContext();
  }
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws 
ServletException,IOException {
      System.out.println("the doget method is entered now --->");  
 
   System.out.println("USER:
"+req.getParameter("username"));    
   System.out.println("PWD:
"+req.getParameter("password"));
   String user = req.getParameter("username");
   String pwd = req.getParameter("password");
   if(user.equalsIgnoreCase("balu") &&
pwd.equalsIgnoreCase("seenu")) {
    System.out.println("VALID USER");
   }
   else  {
    System.out.println("INVALID USER");
   }
   req.setAttribute("user",user);
   HttpSession session = req.getSession();
   session.setAttribute("user",user);
   System.out.println("-------Completing");
   req.getRequestDispatcher("main.jsp").forward(req,res);
   
  }
  public void doPost(HttpServletRequest req, HttpServletResponse res)throws 
ServletException,IOException {
   doGet(req,res);
  }
 }
  
 main.jsp:
 <%@ taglib uri="FlexTagLib" prefix="mm" %> 

 <mm:mxml source="main.mxml"/>

  
 B.mxml:
 <?xml version="1.0" encoding="utf-8"?>
 <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" 
styleName="main" xmlns="*">
 <mx:Style source="main.css" />
  
   <mx:Form width="562" height="400">
     
   </mx:Form>
   <mx:Style>
   .heading {
  background-color: #99CC99;
 }
   </mx:Style>
   <mx:RemoteObject id="sessionObj" source="servlet"
showBusyCursor="true"
         fault="mx.controls.Alert.show(event.fault.faultstring,
'Error')"/>
 </mx:Application>


  
 Thanks
 Satish KC.
Post Reply
Re: mxml to servlet communication
Thu, 4 May 2006 06:17:40 +0000
Hi satish ,

i am also trying the same thing.

i am a newbie for the flex development .

can u able to communicate b/w mxml to servlet , if yes please send me the
details.

Post Reply
about | contact