aspose file tools*
The moose likes JSP and the fly likes Getting Parameters from form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting Parameters from form" Watch "Getting Parameters from form" New topic
Author

Getting Parameters from form

Kamal Patel
Greenhorn

Joined: Nov 05, 2003
Posts: 18
Hi,
I am trying to use a HTML form which has a drop down list, where a user selects an entry and the entry gets displayed on the same page when the user presses a 'submit' button. I have managed to do this, but I keep getting a value displayed on the screen before the submit button gets pressed, i.e. a entry appears on the screen when the page first loads! I am not sure why this is - I may need to initialise some variable as blank, but I am not sure which one!
A snippet of my code is given below where a user is asked to select an entry froma drop down list (an application name), then the 'request' object gets the parameters, where I display the 'parameterValue' variable
on the screen. This is the variable that I am trying to get blank before the user submits.
<TD> Please Choose An Application from the Drop down menu</TD>
<TD> <SELECT NAME=App>
<%int k=0;
while(appnames[k]!=null){%>
<OPTION><%=appnames[k]%></OPTION>
<%k++;}%>
</SELECT></TD>
<INPUT TYPE=SUBMIT VALUE=Submit>
<%
Enumeration parameters = request.getParameterNames();
while(parameters.hasMoreElements()){
parameterName = (String)parameters.nextElement();
parameterValue = request.getParameter(parameterName); %>
<TD><%=parameterValue%></TD>

<% } %>
Any ideas anyone??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

If no one option is marked selected, the select element will display the value of the first option. That's standard HTML behavior.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
I don't understand it all but I can say one thing. it is no use to
have a Selectbox without any value assigned to the option. How would you know which app the user selected?


SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Oppps! I've posted without knowing about the above post.
Kamal Patel
Greenhorn

Joined: Nov 05, 2003
Posts: 18
How do I get it not to display anything the first time the page gets loaded?
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 639
Kamal,
Try This

Cheers
Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Kamal Patel
Greenhorn

Joined: Nov 05, 2003
Posts: 18
Just tried putting the ------select------- entry in, but has not made a difference - still picking up a value. It is like it is caching a value that I selected before I refresh the page!
I think I need some way of saying, don't call the:
Enumeration parameters = request.getParameterNames();
code when the page first loads??
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
Basically you did not make all your code available and people are responding based on partial information. I think you are submitting to the same form which makes the parameter value always available to the request. Please paste the whole code including the form tag so people would have enough information to help you out. We might even run the application to know how to tackle the problem.
[ November 10, 2003: Message edited by: Anselm Paulinus ]
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 639
try clearing your cache by clicking on
Tool->internetoptions->delete files (In case u ar not aware of it)
Kamal Patel
Greenhorn

Joined: Nov 05, 2003
Posts: 18
Ok, here is all my code:
<%@ page contentType="text/html" %>

<%--//Page Directives --%>
<%@ page language="java" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.Document" %>
<%@ page import="org.w3c.dom.Element" %>
<%@ page import="org.w3c.dom.DOMException" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="logonBean.*" %>
<%@ page import="java.security.*" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.StringTokenizer" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.naming.directory.*" %>
<%@ page import="javax.servlet.*" %>
<%! public String routeURL="logon.jsp";%>
<%! static String[] appnames = new String[40];%>
<%! int n=0; int flag=0; String name1 = ""; String name2 = ""; String parameterValue = ""; String parameterName="";%>
<%-- //HTML Header --%>

<html>
<head>
<title>Main Page</title>
</head>
<h1>LDAP Server Connection</h1>
<body bgcolor="white">
<FORM ACTION=main.jsp METHOD=POST>
<%-- //Init Page --%>
<%
//#E2EFED
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

/*
env.put(Context.PROVIDER_URL, "ldap://another address/ou=db.com,o=NetscapeRoot");
*/
env.put(Context.PROVIDER_URL, "ldap://address to server/dc=db,dc=com");
try {

DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//insteadof ou=WebSSO, take user input to determine value into variable
NamingEnumeration results = ctx.search("ou=Application", "(ou=*)&& !(ou=App)", constraints);
//boxname=egcilab*

while (results != null && results.hasMore() && flag==0) {
SearchResult si = (SearchResult)results.next();
String name = si.getName();
StringTokenizer st = new StringTokenizer(name, "=");
while(st.hasMoreTokens()){
name1 = st.nextToken();
name2 = st.nextToken();
appnames[n] = name2;
n++;}

/*Attributes attrs = si.getAttributes();
if (attrs == null) {
//out.println("No attributes <br>");
}else{
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements() {
Attribute attr = (Attribute)ae.next();
String attrId = attr.getID();
for (Enumeration vals = attr.getAll();
vals.hasMoreElements();
//out.println(attrId + ": " + vals.nextElement() + "<br>"));
}
}*/

}
flag=1;
//Exception Handling
}//end of try
catch(NamingException e)
{
out.println("Search failed. <br>");
e.printStackTrace();
}
%>
<TR> <TD> Please Choose An Application from the Drop down menu</TD>
<TD> <SELECT NAME=App>
<OPTION>-------Select Value-------</OPTION>
<%
int k=0;
while(appnames[k]!=null){
%>
<OPTION><%=appnames[k]%></OPTION>
<%
k++;
}
%>
</SELECT></TD>
</TR>
<%-- //Page Routing --%>
<INPUT TYPE=SUBMIT VALUE=Submit>
<br><br><br><br>
</FORM>
<HR>
<B>Application selected</B><BR>
<TABLE>
<%
Enumeration parameters = request.getParameterNames();
while(parameters.hasMoreElements()){
parameterName = (String)parameters.nextElement();
parameterValue = request.getParameter(parameterName); %>
<TR>
<%--<TD><%=parameterName%></TD>--%>
<TD><%=parameterValue%></TD>
</TR>
<% }
%>
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 639
Hey Kamal,
It is working fine for me!!!
I have pasted your code in my file and tried with few changes
Change 1 -
As I the combo box shows me only ---select value-----
I have added <option>--slect valeu1</option>
<option>--slect valeu2</option>
<option>--slect valeu3</option>
then i open new browser and executed this file
the list line of o/p is
Application Selected
Then I select first option and hit submit
last two lines of o/p
Application selected
-------Select Value1-------

then i reopne the browser and type same url with gives me nothing after
Application Selectd
so i Think there is problem with your browser,try with deleting all offline contents.
Cheers
Praful
SORRY FOR LOT OF MISS-SPELLS was in hurry to go home
[ November 10, 2003: Message edited by: Praful Thakare ]
 
jQuery in Action, 2nd edition
 
subject: Getting Parameters from form