Solved

J2ME MIDlet & JSP question

Posted on 2006-06-19
20
527 Views
Last Modified: 2013-11-23
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
Comment
Question by:blue-genie
  • 9
  • 7
  • 3
20 Comments
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933067
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
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933084
Ah, no a "&" must be present:

name=xxx&password=yyy
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16933285
what would the exact syntax be?
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 39

Author Comment

by:blue-genie
ID: 16933303
ok, sorted,
os.write(("name="+myname).getBytes());
os.write(("&password="+mypassword).getBytes());
did the trick.
Any ideas about the flush()?
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933330
flushing is good
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16933335
Make sure instead of the platform default encoding you use the encoding which the web-server/ servlet container hosting the JSP uses.
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933344
flushing should not have any differences on the output, does it still occur?
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16933350
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
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933361
instead of flushing try closing the stream, let's see what that does
0
 
LVL 39

Author Comment

by:blue-genie
ID: 16933401
i am closing the stream. as long as i don't flush() it's fine.
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933416
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
 
LVL 2

Expert Comment

by:tbeernot
ID: 16933440
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16934148
>>  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
 
LVL 39

Author Comment

by:blue-genie
ID: 16934553
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
 
LVL 2

Accepted Solution

by:
tbeernot earned 500 total points
ID: 16934632
Try passing
    "" + name
    "" + password
to the setString

Does the flush work now?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16934698
>> 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
 
LVL 39

Author Comment

by:blue-genie
ID: 16935268
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
 
LVL 2

Expert Comment

by:tbeernot
ID: 17121784
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
 
LVL 39

Author Comment

by:blue-genie
ID: 17121822
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
Apps blocked by Java 9 79
map related example 6 48
Chrome and Firefox Java 5 31
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question