• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

J2ME MIDlet & JSP question

i'm following an old tutorial that shows the parsing of inputted data from a mobile device to a jsp, however, I have 2 problems and I'd appreciate if anyone can shed some light: Here's the code.

public void invokeJSP(String url) throws IOException {
            HttpConnection c = null;
            InputStream is = null;
            OutputStream os = null;
            OutputStream os2 = null;
            StringBuffer b = new StringBuffer();
            TextBox t = null;
            try {
                  c = (HttpConnection)Connector.open(url);
                  c.setRequestMethod(HttpConnection.POST);
                  c.setRequestProperty("IF-Modified-Since", "25 Nov 2001 15:17:19 GMT");
                  c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
                  c.setRequestProperty("Content-Language", "en-CA");
                  c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                  
                  os = c.openOutputStream();
                  os.write(("name="+myname).getBytes());
                  os.write(("password="+mypassword).getBytes());

                                                 /*this seems a problem so i'm commenting it out.
                    need to find out what it actually does and what the implications are of
                    not flushing.*/
                  //os.flush();
                  
                  is = c.openDataInputStream();
                  int ch;
                  while ((ch = is.read()) != -1) {
                        b.append((char)ch);
                        System.out.print((char)ch);
                  }
                  t = new TextBox("Date", b.toString(), 1024, 0);
                  t.setCommandListener(this);
            } finally {
                  if (is != null){
                        is.close();
                  }
                  if (os != null) {
                        os.close();
                  }
                  
                  if (c != null) {
                        c.close();
                  }
            }
                  display.setCurrent(t);
            }
1. if i do the os.flush() - i get null in my jsp, if i don't flush it, the variable gets returned.
2. if i do a second variable, i.e password, it appends it to the first variable

my jsp looks as follows.
<%! String name; String password; %>
<%
name = request.getParameter("name");
password = request.getParameter("password");
out.println("Got: " + name);
out.println("Password: " + password);

%>

In the emulator, the 2 variables return as
Got: superpassword=null
Password: null

any ideas?
thanks.
blu.
0
blue-genie
Asked:
blue-genie
  • 9
  • 7
  • 3
1 Solution
 
tbeernotCommented:
The first thing I notice is that there is no separator in the parameters:

               os.write(("name="+myname).getBytes());
               os.write(("password="+mypassword).getBytes());

This would result in a byte stream like:
name=xxxpassword=yyy

I suspect there should be a \n after each variable.


0
 
tbeernotCommented:
Ah, no a "&" must be present:

name=xxx&password=yyy
0
 
blue-genieAuthor Commented:
what would the exact syntax be?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
blue-genieAuthor Commented:
ok, sorted,
os.write(("name="+myname).getBytes());
os.write(("&password="+mypassword).getBytes());
did the trick.
Any ideas about the flush()?
0
 
tbeernotCommented:
flushing is good
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Make sure instead of the platform default encoding you use the encoding which the web-server/ servlet container hosting the JSP uses.
0
 
tbeernotCommented:
flushing should not have any differences on the output, does it still occur?
0
 
blue-genieAuthor Commented:
tbeernot, yes, i'm aware flushing is good, but if i flush i get a null returned.
mayankeagle, i have not clue what u're referring to .... :-(
0
 
tbeernotCommented:
instead of flushing try closing the stream, let's see what that does
0
 
blue-genieAuthor Commented:
i am closing the stream. as long as i don't flush() it's fine.
0
 
tbeernotCommented:
an alternative would be to place the parameters in the URL and use GET. The JSP container will interprete that. This approach has a limit on the length of the URL, but with a few parameters this should work ok:

 ?name=xxx&password=yyy
0
 
tbeernotCommented:
I meant closing instead of flushing. BTW, I found the article which you probably used as a base (code is very very similar ;-) but I can't see what the problem with that code in this case would be. The stream really needs to be flushed or closed, otherwise the behaviour is unpredictable.

Can you capture the network traffic so we can see what the MIDP is actually sending (and the difference between flush and no-flush)? (You could use the very handy PlugProxy, http://www.cs.nyu.edu/brw215/plugproxy/).
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>>  i have not clue what u're referring to

getBytes () uses the platform default encoding to convert the string to a byte-array. If you use getBytes ( "ASCII" ), it uses ASCII encoding. getBytes ( "UTF-8" ) uses UTF-8 encoding. You should use the encoding which the JSP server supports.
0
 
blue-genieAuthor Commented:
hmm, ok...
i think i'm using the right thing.
any idea why if my application is parsing 2 varibles back, and they can be written out, but i can't use them in a prepared statement?

out.println("Username:" + name); // these write out - parsed from textfield in application on device
out.println("Password:" + password); // these write out - parsed from textfield in application on device

synchronized(pstmVerifyUser) {
      pstmVerifyUser.setString(1, (String)name); //have also tried toString();
      pstmVerifyUser.setString(2, (String)password);
      rsUserID = pstmVerifyUser.executeQuery();
}

blu
0
 
tbeernotCommented:
Try passing
    "" + name
    "" + password
to the setString

Does the flush work now?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> i think i'm using the right thing.

No, you're using the platform default and it will cause problems if the default encoding is different on both machines :)
0
 
blue-genieAuthor Commented:
ah ok. i figured out why my variables weren't right. for some reason the emulator puts a space before the cursor when you start typing.
0
 
tbeernotCommented:
There are two questions in this question:

1. if i do the os.flush() - i get null in my jsp, if i don't flush it, the variable gets returned.
2. if i do a second variable, i.e password, it appends it to the first variable

My post from 06/19/2006 03:09AM PDT and 06/19/2006 03:12AM PDT answers question 2.
As to question 1; I have never gotten an answer to my post from 06/19/2006 07:24AM PDT and it is unclear weather that still is an issue.
0
 
blue-genieAuthor Commented:
hi sorry - i was so busy trying to get it to work.
in terms of solutions, 1. i am still not flushing the thing, can't get it to work with flushing, but i am closing the stream.
2. the second solution was viable and worked.

thanks peeps.
sorry i took so long to get back to you.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 9
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now