Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

jsp hidden field

Posted on 2005-05-02
26
Medium Priority
?
893 Views
Last Modified: 2012-08-13
Hello, experts,

I want to ask a simple question :

in a html page I use a hidden field :

<input type="hidden" name="id" value="abc">

then in a jsp file called : usernames.jsp

<%
 String memberName=request.getParameter("id");
 out.println("memberName=" + memberName);

%>

it should print memberName=abc, right , but i got:  memberName=null

please tell where it's wrong?
0
Comment
Question by:lilyyan
  • 13
  • 11
  • +1
26 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13914735
can u post the entire form
0
 
LVL 33

Expert Comment

by:hongjun
ID: 13914741
check to see if the form is posted to the correct page.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13914847
Hi,

Is <input type="hidden" name="id" value="abc"> within the <form> tag?

Yes, as what object said, you may be interested in posting your codes here.

Regards
Dave
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:lilyyan
ID: 13914903
Thanks for your prompt responses.

I didn't use form. I think I made a mistake here?

do i have to use form. ?

i use a link : http://mysite/usernames.jsp?id=memberName in the browser to retirve the value of id.

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13914914
Hi,

Well, if that is the case, you should be able to get id, don't you?

Normally, in the real application, we normally place all the textboxes within the <form> tag. So when I submit the form, all the information in the textboxes will be sent to Servlet. Then from this Servlet, we can get the information by

String memberName=request.getParameter("id");
 out.println("memberName=" + memberName);

and do the necessary actions such as store or update DB. Afterward, from this Servlet, we will redirect it into another JSP page.

Does that help you?

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13914932
Hi,

So one point....

If you use http://mysite/usernames.jsp?id=memberName then you should be able to get the id by the command above. However, if you want to get the value from one JSP/HTML to another JSP/HTML and do necessary calculations, you need to be in <form> so that your program know where to calculate those values.

Regards
Dave
0
 

Author Comment

by:lilyyan
ID: 13915013
There is a context for my application.

In the first html pageA.html
----------------------------------
<form action="userName.jsp" method=post>

user input, name and password

</form>

In userName.jsp
------------------------------------
<% String memberName=request.getParameter("userName");
%>
<input type="hidden" name="id" value="<%=memberName>">
Then I send a link : http://mysite/active.jsp?id=memberName  to user by email

In active.jsp

I try to retirve above id value, and process something alse.

but when i try to print id in active.jsp , i got null value or id=memberName.

suppose in the link http://mysite/usernames.jsp?id=memberName,   memberName should be a variable name. instead of a real sting ( id=myname) right? This is kind of confused to me.







0
 

Author Comment

by:lilyyan
ID: 13915016
0
 

Author Comment

by:lilyyan
ID: 13915037
and this line :
<input type="hidden" name="id" value="<%=memberName>">
is <input type="hidden" name="id" value="<%=memberName%>">
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915049
Well,

If I understand you correctly, assuming that you are interested in ONLY username

<form action="userName.jsp" method=post>

user input, name and password
<!-- You should have this -->
<input type="text" name="userName"> <!-- Case sensitive -->
<input type="submit" value="submit">
</form>

Then your userName.jsp page,

<%
      String memberName=request.getParameter("userName").toString();
      request.setAttribute("test", memberName);
%>

<input type="hidden" name="id" value="<%= request.getAttribute("test").toString() %>">


I hope that helps you.

Regards
Dave
0
 

Author Comment

by:lilyyan
ID: 13915083
Appreciate your reply.

in userName.jsp

when i use <%=userName%>, i can print out the value of userName.
so i assume <input type="hidden" name="id" value="<%=memberName%>">  is wroking.

do i have to use: request.setAttribute("test", memberName); ?

i'm confused that : in the link :  http://mysite/active.jsp?id=memberName, using memberName is corrent or not.
how to test this ?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915106
Hi,

Okay, <input type="hidden" name="id" value="<%=memberName%> will work only if you specify it in the same scope. You can understand better by sample below.

Sample 1
-----------
<%
       String memberName=request.getParameter("userName").toString();
%>

<input type="hidden" name="id" value="<%=memberName%>

sample 1 won't work because memberName is out of the scope of the HTML static. It is in the scriplet.
You need to use request to basically and temporarily store the memberName in the request to be accessed by input type.

Well, http://mysite/active.jsp?id=memberName, this should work if you specify the name. For example, http://mysite/active.jsp?id=dave. Then in your active.jsp, you will need to have a thing called request.getParameter("id").toString();

Does that help you?

Regards
Dave
0
 

Author Comment

by:lilyyan
ID: 13915114
in userName.jsp
i already set the valuse of id,

in http://mysite/active.jsp?id=memberName , so i need to set it again?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915126
Okay in your userName.jsp, when you send the link to http://mysite/active.jsp?id=memberName, you need to set the parameter as

http://mysite/active.jsp?id=<%= memberName %>

or

http://mysite/active.jsp?id=<%= request.getAttribute("test").toString() %>

Regards
Dave
0
 

Author Comment

by:lilyyan
ID: 13915144
i will try your suggestion.

well, when i use
<%
       String memberName=request.getParameter("userName");
%>

<%=memberName%>

it will print the value of userName.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915146
Sorry but am I explaining clearly? I am sorry lilyyan if I am not clear in explaining. I am dong my project too :)

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915151
Hi,

>>"it will print the value of userName."
Oopppsss...please ignore my explanation again. I was wrongly telling you.

Just try my suggestion and let me know.

Regards
Dave
0
 

Author Comment

by:lilyyan
ID: 13915176
Really apprciate your attention

cause i need send the link by email, so in the userName.jsp, i need send it as a mail message

http://mysite/active.jsp?id=<%= memberName %>, <% is special character, how could i escape this in the <%  %> block
0
 

Author Comment

by:lilyyan
ID: 13915179
this will be my last question, i promise : )
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915185
Hi,

How about to store it in a String then you send it to your client.

<%
String link = "";
link = "http://mysite/active.jsp?id=" + memberName;
%>

How about that?

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915187
Hi,

I am going out for lunch now. Will get back to u soon.

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915199
>> "this will be my last question, i promise : )"

No worries :)

regards
Dave
0
 

Author Comment

by:lilyyan
ID: 13915204
Hi, really appreciate your help.

I guess I also need to take a break, and will be back tomorrow.
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 320 total points
ID: 13915916
Hi lilyyan,

I read back our posting again and I think that I was partly misunderstand your question. Just as a summary.

In your username.jsp, you want to send the link to user via email such as
http://mysite/active.jsp?id=david // for me
http://mysite/active.jsp?id=lilyyan // for you

Correct?

Now, after you are able to get the info from HTML page as
<%
       String memberName=request.getParameter("userName");
%>

<%=memberName%>

in your username.jsp, now you need to create one String to store the link data. Modify the above codes into below.

<%
       String memberName=request.getParameter("userName").toString(); // Assuming that memberName won;t be null
       String link = "";
       link = "http://mysite/active.jsp?id=" + memberName;

       /* Do whatever you want to send the link to your client's email */
      //email.send(link); // sample :)
%>

Then next time your user click on that link, it would be a kind like http://mysite/active.jsp?id=his-or-her-ID

I hope that helps.

Regards
Dave


0
 

Author Comment

by:lilyyan
ID: 13918502
Hi Dave,

Really apprecaite. Your suggestion is the key point. link = "http://mysite/active.jsp?id=" + memberName

The link I used is: link= "http://mysite/active.jsp?id=memberName", here memberName was treated as a real string, instead of a variable. Can't believe I made this mistake.: (

Once again, thanks for your help,

lilyyan

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13918614
Hi lilyyan,

Glad I could help :).

Regards
Dave
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

572 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