Solved

Simple question about parameter passing between WML and database

Posted on 2003-11-19
31
971 Views
Last Modified: 2013-12-27
Here I am again, a simple one this time. I send parameters from a wml file to a jsp file in order to enter them in a database but the parameters dont get there correct. What I mean is :
wml file:
<do type="accept" label="SUBMIT">
  <go method="post" href="insert.jsp">
        <postfield name="name" value="$name"/>
        <postfield name="mobile" value="$mobile"/>
 </go>
</do>

jsp file:

String name = request.getParameter("name");
String mobile = request.getParameter("mobile");
PreparedStatement PStmt = conn.prepareStatement("insert into agenda (name, mobile) values (\"" + name + "\",\"" + mobile + "\")");

The values in the database appear like $name and $mobile instead of the real values of the parameters. What is wrong?


0
Comment
Question by:annasfe
  • 18
  • 13
31 Comments
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Hi again,

  I'm on the boundaries of my knowledge here ;-)...

  I think your problem could be solved by switching from a PreparedStatement to just using a Statement.

  With a PreparedStatement, the idea is that you create a statement that contains "?" symbols.  This allows you to add variables into that statement later.  In this case, you already know everything you need when you create the statement, so you don't really need this functionality.

  For an example of a PreparedStatement (which I haven't used ;-)), look at:

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20799827.html

  You'll need to scroll down the question about half-way.  Jasbir21 has posted some code that uses a PreparedStatement.
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Oops.  Sorry about that.  I read your question (and responded) too quickly :-(

The problem is not with the PreparedStatement, though I would still stick to my original comment about using a Statement instead.

The problem is with the way you have defined the "value" elements in the WML.  I *think* the problem is that you have put inverted commas (") around the $name and $mobile values.  ie. I *think* it should be:

<do type="accept" label="SUBMIT">
  <go method="post" href="insert.jsp">
        <postfield name="name" value=$name/>
        <postfield name="mobile" value=$mobile/>
 </go>
</do>

I'll try to check this now.
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
It looks like it should be $(name) and $(mobile).

Not inside inverted commas.
0
 

Author Comment

by:annasfe
Comment Utility
First, thanks again for your trying to help :)

I am sorry but this doens work :( When I change it and delete the "" the page doesnt load and I get the server error. Apart from that, I tried in the code as it is (without deleting the "")  to print the data sent to the jsp page (skipping the database preparedstatement stuff) and it prints the correct data. So, I guess that there is sth wrong with the database thing. I have been searching around internet but no luck, only for php code which unfortunatelly I cant understand very well or use. I will try to check your comment for the statement and let you know...

I tried:
"$name"
"$(name)"
$name
$(name)

nothing works :(
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
>> I tried in the code as it is ... and it prints the correct data

If you've displayed the data successfully from the JSP page, then you can ignore my comments regarding the $name ;-), because, as you say, the problem must lie with the database update.  Just for interest, how are you displaying this data?

I'd still go witht the Statement instead of PreparedStatement, and to simplify the syntax a bit, I'd use ' instead of " to surround the data:

Statement stmt = conn.createStatement();
stmt.executeUpdate("insert into agenda (name, mobile) values ('" + name + "', '" + mobile + "')");

Hope this helps.
0
 

Author Comment

by:annasfe
Comment Utility
Still no luck :(
I changed the prepared statement with the simple one but still the same. I am sure at the end it will be some stupid "" missing or sth but this thing is driving me nuts! And the wierd thing is that the data get to the jsp page correctly since I can print them ( I use the thing you suggested the previous time <p><%=name%></p>)

Any more ideas?
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
OK ;-)

Let's find out what is being written to the database.  Change the statement code as follows:

Statement stmt = conn.createStatement();
String insertCommand = "insert into agenda (name, mobile) values ('" + name + "', '" + mobile + "')";
out.println("About to do this: " + insertCommand);
stmt.executeUpdate(insertCommand);

That should output the line that is to be executed.

Also (whilst I remember ;-)) are you certain that the faulty data in the database has just been written (eg. are you clearing out the old data before running again)?
0
 

Author Comment

by:annasfe
Comment Utility
Ok..
The ouput I get is the one it should be
"About to do this: insert into agenda (name, mobile) values ('anna','123456789')"
(by the way the out.println was creating a server error if you dont put the <p></p> tag :P)

BUT in the database still the same thing :(
(yes, i delete every time the old entry)
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Well now.  That is strange.

Can you post some more of the JSP code.  I suspect that you might have another line in there (perhaps an old test line or something) that is causing the problem.

Excellent catch on the <p> BTW ;-)
0
 

Author Comment

by:annasfe
Comment Utility
Ok, here is the hole jsp code (sorry I was late, I was abit sick these days)

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//OPENWAVE.COM//DTD WML 1.3//EN"
                "http://www.openwave.com/dtd/wml13.dtd" >
<%@ page import="java.util.*, java.sql.*, java.io.*" %>

<%response.setContentType("text/vnd.wap.wml");%>
<wml>
 <card id="inser_contact" title="Insert Contact">

<%
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn4 = DriverManager.getConnection("jdbc:mysql://...", "user", "password");

String name = request.getParameter("name");
String mobile = request.getParameter("mobile");
String fixed = request.getParameter("fixed");
String groupo = request.getParameter("groupo");
int found=0;
String flag="0";
String userid = request.getParameter("userid");

//CheckMobile checkmobile = new CheckMobile();

if (name.equals("") || mobile.equals(""))
{
%>

<jsp:forward page="agenda.jsp">
<jsp:param name="flag" value="1" />
<jsp:param name="userid" value="<%=userid%>" />
</jsp:forward>

<%
}
else
{
  //mobile = checkmobile.change(mobile);
  if(mobile.equals("error"))
  {
   %>

   <jsp:forward page="agenda.jsp">
   <jsp:param name="flag" value="2" />
   <jsp:param name="userid" value="<%=userid%>" />
   </jsp:forward>

   <%

  }
}

if(!fixed.equals(""))
{
  //fixed = checkmobile.change(fixed);
  if(fixed.equals("error"))
  {
   %>

   <jsp:forward page="agenda.jsp">
   <jsp:param name="flag" value="2" />
   <jsp:param name="userid" value="<%=userid%>" />
   </jsp:forward>

   <%

  }

}


PreparedStatement st = conn4.prepareStatement("select name from agenda where id=?");
st.setString(1,userid);
ResultSet rs = st.executeQuery();

while(rs.next()){

  if(name.equals(rs.getString("name")))
      found=1;
}

if(found==1)
{
%>

<jsp:forward page="agenda.jsp">
<jsp:param name="flag" value="3" />
<jsp:param name="userid" value="<%=userid%>" />
</jsp:forward>

<%
}
Statement stmt = conn4.createStatement();
String insertCommand = "insert into agenda (name, mobile) values ('" + name + "', '" + mobile + "')";
out.println("<p>About to do this: " + insertCommand + "</p>");
stmt.executeUpdate(insertCommand);


conn4.close();
%>

</card>
</wml>


Does it help? (dont pay attention in the checkmobile function, is an old one that returns "error" if the mobile doesnt have the correct format)
0
 

Author Comment

by:annasfe
Comment Utility
I will also increase the points cause it appears to be trickier than i thought :)
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Hi annasfe, I hope you're feeling better.

>>  I will also increase the points cause it appears to be trickier than i thought :)

;-)

Well your code looks nice and clean to me.  I can't see why it doesn't work (at the moment ;-)).  Can I just clarify?

1) The INSERT is actually happening into the agenda database, but the values being added are "$name" and "$mobile", but the println result is displaying:

2) "About to do this: insert into agenda (name, mobile) values ('anna', '123456789')"

If this is the case, then something very strange is happening with the database write.  If this was my app, I would try the following:

Change the insert command to a single literal test string using slightly different text to ensure the updated page is being used, eg:
String insertCommand = "insert into agenda (name, mobile) values ('annatest', '999999999')";

I would expect that to work.  If it did, I would replace the original line and try again.


One other quick point.  Assuming that your database prevents duplicates (eg. if name and mobile are both primary keys), you can replace:

>> while(rs.next()){
>>
>>  if(name.equals(rs.getString("name")))
>>      found=1;
>> }
>>
>> if(found==1)
>> {

with:

if (rs.next())
{
0
 

Author Comment

by:annasfe
Comment Utility
Ok,I tried your comments, when I put normal values like "annatest" and "999999999" it works fine but when i change it back to the variables still the same :(
BUT i have a new idea! Here is what i thought that causes the problem:

When I print the insert command it looks fine in the WAP browser but this is because in WAP the $name part of the command is automatically translated to "the value of the name variable". This doesnt happen in normal jsp-in my case in the real insert database command- where it takes the value of the parameter name as $name which doesnt mean anything in jsp, so it inserts it as it is.
I mean in jsp it replaces the name variable with its value that is $name in WAP but jsp cannot go any further and change this to the real value...or no? Dont know if you understand this, or if I am right, I still didnt figure anyway to try it, but what do you think? If not I give up!
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Hi Anna,

  Sorry for the delay, I've been away for a few days.

  OK.  First I want to say nice one ;-)  Superb catch with the WAP browser interpretation of the returned $ references.

  I have a lot of time for people who learn quick and are genuinely trying, so please don't give up yet.  I'm with you all the way on this until we sort it out :-)

  The second thing is that according to the following reference, the variable references in the WML should definitely appear as $(name) and $(mobile).

http://developer.openwave.com/htmldoc/41/wmlref/intro4.html#575696

  This may or may not make a difference at the moment (hopefully it will).  If this doesn't work, could you confirm whether the database ends up with "$(name)" in it or if it's something else (eg. "$name" without the parentheses).

  If you want to see what the JSP has actually received, you could do one of the following:

  1)  If you have set up the "Logger" inside your "Context" for the web application (in conf/server.xml), you could add the following to the JSP:

<%
    log("name = " + name);
    log("mobile = " + mobile);
%>

      Anywhere after the request.getParameter(...) will do.

  2) If you haven't set up logging, you could do a similar thing with System.out.println(...).  The output from this will go to a file called catalina.out in the Tomcat "logs" directory.

0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Ooh.  I just found another link with a page similar to yours that shows the formatting of the postfield elements.

Listing 2 on this page:

http://www.wirelessdevnet.com/channels/wap/training/wml6.html
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:annasfe
Comment Utility
Ok, first, thanks for keep trying :)

I have the variables as they supposed to be, $(name) with parenthesis, in the database it is written also like that $(name) and tried the System.out thing and again in catalina log file it is written $(name). As always in the WAP browser screen I get the correct thing....
So it has to be this thing that I told you with the variable interpretation of jsp. The problem is how do I solve it? It has to be some way....I also thought (although I dont think it will make any difference) to put the insert.jsp file that does the database insert in the same file that sends the data as a different wml card and just pass the data from card to card. Still didtn try it, I will try it and let you know...

I saw the example that you told me, it is exactly like the one I want, the problem is that in asp the Request.parameter seems to work fine :(
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
>> Still didtn try it, I will try it and let you know...

Hi Anna.  How's it going?
0
 

Author Comment

by:annasfe
Comment Utility
nothing :(
I had some trouble putting both files in the same file-different cards, so I kind of left it there. Anyway, as I saw it I would have had the same problem cause there was not much change really...Maybe I should switch to asp since most of the examples for wml and database stuff that I see is with asp :(
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Hold on for a little while.  I'm just creating some test files to see what I get on my web server.  I'll let you know within the next hour whether it has worked or not (and I'll post the complete WML and JSP source if it turns out to be OK ;-))
0
 

Author Comment

by:annasfe
Comment Utility
Whow! That's good news :) I will be away for the weekend but as soon as I come back I ll check to see if you have succeeded -really hope you will!! Thanks and good luck!
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
I've written the code (wml & jsp), but now my network operator's GPRS isn't working :-(

Probably best for you to check after the weekend.  Have a good one ;-)

Jim.
0
 
LVL 15

Accepted Solution

by:
jimmack earned 200 total points
Comment Utility
Woo Hoo! ;-)

Here are the two files that I used.  It works fine.

test.wml
------------
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
  <card id="card1" title="Input">
    <p>Please input:</p>
    <p>Name:<input name="name" maxlength="10"/> <br/>
    Mobile:<input name="mobile" value="0123456789" maxlength="15"/> <br/>
    </p>
    <p><a href="#card2">ParamTest</a></p>
  </card>
  <card id="card2" title="ParamTest">
    <p>About to send:</p>
    <p>name = $(name)</p>
    <p>mobile = $(mobile)</p>
    <do type="accept" label="SUBMIT">
      <go method="post" href="testParam.jsp">
            <postfield name="name" value="$(name)"/>
            <postfield name="mobile" value="$(mobile)"/>
     </go>
    </do>
  </card>
</wml>


testParam.jsp
-------------------
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//OPENWAVE.COM//DTD WML 1.3//EN"
                "http://www.openwave.com/dtd/wml13.dtd" >
<%response.setContentType("text/vnd.wap.wml");%>
<wml>
  <card id="card1" title="Response">
<%
log("Received: name = " + request.getParameter("name") + ", mobile = " + request.getParameter("mobile"));
%>
    <p>Received:</p>
    <p>Name: <%= request.getParameter("name") %></p>
    <p>Mobile: <%= request.getParameter("mobile") %></p>
  </card>
</wml>


In my log file I get the following:

2003-11-28 18:53:05 jsp: Received: name = Jim, mobile = 0123456789

Which means that in the JSP, the correct values are received :-)  Have a close look at any differences between your WML and mine and your JSP and mine.

If you can't find the problem, don't worry we'll figure it out together ;-)

At least this guarentees that you can do this OK with WAP/JSP ;-)
0
 

Author Comment

by:annasfe
Comment Utility
It doesnt work :'(

I tried all the changes you have in your files but still the same. First I changed my insert statement to  
String insertCommand = "insert into agenda (name, mobile) values ('" + request.getParameter("name") + "', '" + request.getParameter("mobile") + "')";
....but nothing.
Then I changed my first file that sends the data to make it have two cards, but still no difference.

Finally, I skipped the database thing and made my second file exactly like yours but still nothing.

I dont see any more differences, the only thing is that you are not using database transactions but your log is ok while mine is still $(name) $(mobile) so I guess it doesnt matter, when I can make the log file appear correctly I am sure I can make the database transaction ok too.

Here is my code as it is now:

agenda.wml
-------------------
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//OPENWAVE.COM//DTD WML 1.3//EN"
                "http://www.openwave.com/dtd/wml13.dtd" >
<wml>
<card id="agenda" title="Agenda">
 <p align="left">
      NAME: &nbsp;
      <input name="name" value=""/><br/>
 </p>
 <p align="left">
      MOBILE:
      <input name="mobile" value=""/><br/>
 </p>
 <p><a href="#card2">ParamTest</a></p>
  </card>
  <card id="card2" title="ParamTest">
    <p>About to send:</p>
    <p>name = $(name)</p>
    <p>mobile = $(mobile)</p>
    <do type="accept" label="SUBMIT">
      <go method="post" href="insert_contact.jsp">
            <postfield name="name" value="$(name)"/>
            <postfield name="mobile" value="$(mobile)"/>
     </go>
    </do>
  </card>
</wml>



insert_contact.jsp
---------------------
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//OPENWAVE.COM//DTD WML 1.3//EN"
                "http://www.openwave.com/dtd/wml13.dtd" >
<%response.setContentType("text/vnd.wap.wml");%>
<wml>
  <card id="card1" title="Response">
<%
System.out.println("Received: name = " + request.getParameter("name") + ", mobile = " + request.getParameter("mobile"));
%>
    <p>Received:</p>
    <p>Name: <%= request.getParameter("name") %></p>
    <p>Mobile: <%= request.getParameter("mobile") %></p>
  </card>
</wml>


As I told you, all the times in my WAP browser the data appear correctly while in the catalina log it appears $(name) $(mobile)
I dont see any differences with yours, do you?
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Aww.  I'm sad now :'(   <sniff>

I thought I'd got it.

Never mind.  We'll keep going.

>>  when I can make the log file appear correctly I am sure I can make the database transaction ok too.

Absolutely right :-)

>> I dont see any differences with yours, do you?

No.  The code at both ends looks OK.

...

(There's a lot of frowning and head-scratching going on at the moment here...)

...

OK.  What do we know?

Well... for one thing, your input fields must be working correctly because your WAP browser is correctly displaying the values that you have entered.

The WML must be well formed, otherwise the WAP browser would reject it.


Mmm.  Alright then.  Things to double check (please don't take these questions the wrong way.  I just want to confirm that I haven't missed something):

When you modified the WML file and accessed it on your handset, did you "reload" the page or did you clear the cache?  - Just confirming that your WAP browser is definitely using the latest page.

Unfortunately, there are no timestamps in catalina.out.  Are you sure that the output that appears in it is from the latest JSP (ie. does it contain the whole text "Received: name = $(name), mobile = $(mobile)")?


If you want to try it, you could access my test page with your WAP browser and I could tell you whether I get the expected data.  http://www.ossmobile.co.uk/testsite/test.wml

I know this can work, so like the terminator, "I absolutely will not stop, ever, until we get the data" ;-)
0
 

Author Comment

by:annasfe
Comment Utility
OK GOOD NEWS! (and some not so good but anyway...)

When I tried the same code that I sent you through a WAP mobile it worked!! I cant believe it, if I finally understand that it was all this time Nokia's simulator fault I will through away my nokia phone !! grrrrr!!!

Anyway, when I say worked, I mean that the catalina log was fine! Then I tried to put my old code with the database transaction but I got a server 500 error...well, still working on it but I have much more positive thinking now. :)

Now about your questions. I am pretty sure that in the Nokia simulator the page was reloaded ok cause when I was making small changes I could see them so it was not the cache one.Then in the catalina log I am also sure that it was writing the latest output cause I was not deleting the previous and I could see every time one new line, and at the end for example with the new format "Received :..."

So I will try to make the database work and keep you informed....I still cant believe it was the simulator's fault....
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Never trust a simluator/emulator or any other ..ator ;-)  LOL.

Real devices always give you the actually functionality (and bugs ;-))
0
 

Author Comment

by:annasfe
Comment Utility
You are totally right, I am still very mad with the stupid emulator.....
well, I give you the points although still I dont have it working totally but I am sure I am in the right way now (probably you deserve more for all this trouble you got into ;))

Thanks again and wait for my next question! (I am sure there are a lot to come :D)
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
I would have been happy to carry on here if I could have earned an 'A' ;-)

As I'm sure I've said before.  It's always a pleasure to help someone who is really interested and wants to know "how" and "why" ;-)

I await your questions :-)
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
Just for info:

It makes no difference to you (as a questioner) what grade you give.  In this case, for example, you spent 200 points, that's it and that's all ;-)

The grade does, however, make a difference to the person that answered the question.  A 'C' grade gives 2x points, a 'B' gives 3x points and an 'A' give 4x points.

(That's why I would have liked to have kept going for the 'A' grade ;-))
0
 

Author Comment

by:annasfe
Comment Utility
Sorry, you are right, I also thought about it (but it was too late cause I had submited it and then I saw I cannot change it). And also I didnt know it makes this kind of difference what I put :(  (but I increased the points, I hope this makes it up a bit ;))

I promise next time will be definately an A :)

Thanks again a lot, I will post a final note when I have it working completely, just so that you know it worked out fine (or if not I will post a new question ;)
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
No problem really ;-)

;-)  Good luck.  
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

Need WiFi? Often, there are perfectly good networks that don't have WiFi capability - and there's a need to add it.  - Perhaps you have an Ethernet port into a network but no WiFi nearby. - Perhaps you have a powerline extender and no WiFi at the…
This subject  of securing wireless devices conjures up visions of your PC or mobile phone connecting to the Internet through some hotspot at Starbucks. But it is so much more than that. Let’s look at the facts: devices#sthash.eoFY7dic.
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now