Link to home
Start Free TrialLog in
Avatar of palehose
palehose

asked on

URLLoader and HttpServletRequest with POST method

I have a problem with the variables of form submitted with POST.  In my doPost function of my servlet, I do not receive the variables, or at least they cannot be accessed in the HttpServletRequest variable.  Everything works fine with GET, but I'm sending a password, so I would rather use POST.

My Action Script code is included.  I'm using Tomcat and SSL as well.
var sendData:String = "command=logIn&user="+user+"&pw="+password;
var vars:URLVariables = new URLVariables(sendData);
var request:URLRequest = new URLRequest();
request.url = getBaseURL()+"ui";
request.method = URLRequestMethod.POST;
request.data = vars;
 
userLoader = new URLLoader();
userLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
userLoader.load(request);
 
userLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
userLoader.addEventListener(Event.COMPLETE, handleLogin);

Open in new window

Avatar of lexxwern
lexxwern
Flag of Netherlands image

Try this:

var sendData:Object = new Object;
sendData.command = 'logIn';
sendData.user = user;
sendData.pw = password;
var vars:URLVariables = new URLVariables(sendData);
var request:URLRequest = new URLRequest();
request.url = getBaseURL()+"ui";
request.method = URLRequestMethod.POST;
request.data = vars;
userLoader = new URLLoader();
userLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
userLoader.load(request);
userLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
userLoader.addEventListener(Event.COMPLETE, handleLogin);

Open in new window

Avatar of palehose
palehose

ASKER

Thanks for the quick response, however, a couple of things:

1)The URLVariables constructor takes a String.
2)Regardless, the problem is not storing the data on the client, it's receiving it at the servlet.  I've checked to make sure that request has the correct data, and it does.  The servlet is not getting the variables.
SOLUTION
Avatar of lexxwern
lexxwern
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Again, this does the same thing I was already doing.  The problem is not storing the data, that's fine.  It has to be on the other end because the method works perfectly fine with GET.
Did you check if it worked or not?

Sending the data as aa URLVariable (i.e. euphemism for query string) will never work for the POST method so the way data is sent needs to be modified.
Sorry, I'm new to this, I did not know that.  I did try it and I'm getting the same results (nothing on the other end).  Is there another way to send the data?
Can you show me the JSP code for reading the POST variable as well?

Also, you should consider using HTTPServices in Flex. Sending POST data should be very easy thru that.

Here's a code sample:
<?xml version="1.0" encoding="utf-8"?>
 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns="*" creationComplete="send_data()">
 
    <mx:Script>
        <![CDATA[
            private function send_data():void {
                userRequest.send();
            }
        ]]>
    </mx:Script>
 
    <mx:Form x="22" y="10" width="493">
        <mx:HBox>
            <mx:Label text="Username"/>
            <mx:TextInput id="username"/>
        </mx:HBox>
        <mx:HBox>
            <mx:Label text="Email Address"/>
            <mx:TextInput id="emailaddress"/>
        </mx:HBox>
        <mx:Button label="Submit" click="send_data()"/>
    </mx:Form>
 
    <mx:DataGrid id="dgUserRequest" x="22" y="128" dataProvider="{userRequest.lastResult.users.user}">
        <mx:columns>
            <mx:DataGridColumn headerText="User ID" dataField="userid"/>
            <mx:DataGridColumn headerText="User Name" dataField="username"/>
        </mx:columns>
    </mx:DataGrid>
 
    <mx:TextInput x="22" y="292" id="selectedemailaddress"
        text="{dgUserRequest.selectedItem.emailaddress}"/>
 
    <mx:HTTPService id="userRequest" url="http://localhost/myproj/request_post2.php" useProxy="false" method="POST">
        <mx:request xmlns="">
            <username>{username.text}</username>
            <emailaddress>{emailaddress.text}</emailaddress>
        </mx:request>
    </mx:HTTPService>
 
</mx:Application>

Open in new window

Here's my doPost function.  As for using HTTPService, all I'm getting is a parse error when I try to use it.
public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
{
	try {
		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager.getConnection(url, "root", "");
		stmt = con.createStatement();
		
		PrintWriter out = response.getWriter();
		BufferedReader reader = request.getReader();
		
		Enumeration e = request.getHeaderNames();
		while(e.hasMoreElements()) {
			String s = (String) e.nextElement();
			System.out.println(s + ": " + request.getHeader(s));
		}
	
		String command = request.getParameter("command");
		//String user = request.getParameter("user");
 
		System.out.println("POST command: "+command);
		//System.out.println("user: "+user);
 
		if (command==null) {
		// No command
		response.setContentType("text/html");
		out.println("<body>");
		out.println("No command <p>");
		out.println("</body>");
		return;
		}
		
		if(command.equals("logIn")) {
			String username = request.getParameter("user");
			String password = request.getParameter("pw");
			String uid = "";
			
			System.out.println(username);
			System.out.println(password);
			
			String query = "SELECT * FROM users WHERE username='" + username + "'";
			ResultSet rs = stmt.executeQuery(query);
 
			while(rs.next()) {
				System.out.println(rs.getString("password"));
				if(password.equals(rs.getString("password"))) {
					uid = rs.getString("userid");
				}
			}
			
			out.println(uid);
			
		} 
		stmt.close();
		con.close();
	} catch(Exception e) {
		e.printStackTrace();
	}
}

Open in new window

So the header does include the Object.

From the header:
Referer: https://localhost:8443/app/app.swf
Content-type: application/x-www-form-urlencoded
Content-length: 15

[object Object]

I don't think it should be sent with that content type, so it sends the Object and not that string.  What content type should it be using, so I can just read the bytes?
Content type looks fine to me.

Did you include you form input valus as an object as the HTTPService.request object?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok.. I understand your time constraints..