nubee
asked on
simple question: get,set methods using arrays.
I am using Beans and JSP to build a webpage.
The user selects more than one item from the listbox and clicks submit. For eg: If he selects vendor,Model,ID three different listboxes with Vendor, Model and ID should appear in the next page.
What I was trying to do is pass the selected values in the listbox and collect them in the bean and then query the database:
String Multicol_lst[]; ---- this is the name of the listbox from where the user makes multi search.
public void setMulticol_lst[](String Value) {
for(int i=0;i<Multicol_lst.length; i++) {
int j=1;
String str[j] = request.getParameter(Multi col_lst(i) );
System.out.println("in array" +str[j]);
j++;
}
this.Multicol_lst[i] = Value;
}
public String getMulticol_lst[](String Value) {
return this.Multicol_lst[];
}
When I compile this I am getting an error : '( ' expected
and pointing to the line---------- public void setMulticol_lst[](String Value) {
can you please tell me how I should get this to working and return the result?
Thanks,
Nubee
The user selects more than one item from the listbox and clicks submit. For eg: If he selects vendor,Model,ID three different listboxes with Vendor, Model and ID should appear in the next page.
What I was trying to do is pass the selected values in the listbox and collect them in the bean and then query the database:
String Multicol_lst[]; ---- this is the name of the listbox from where the user makes multi search.
public void setMulticol_lst[](String Value) {
for(int i=0;i<Multicol_lst.length;
int j=1;
String str[j] = request.getParameter(Multi
System.out.println("in array" +str[j]);
j++;
}
this.Multicol_lst[i] = Value;
}
public String getMulticol_lst[](String Value) {
return this.Multicol_lst[];
}
When I compile this I am getting an error : '( ' expected
and pointing to the line---------- public void setMulticol_lst[](String Value) {
can you please tell me how I should get this to working and return the result?
Thanks,
Nubee
String str[j] = request.getParameter(Multi col_lst(i) );
public void setMulticol_lst(String[] Value) {
for(int i=0;i<value.length;i++)
{
System.out.println("in array" +str[i]);
this.Multicol_lst[i] = Value[i];
}
public String[] getMulticol_lst()
{
return this.Multicol_lst;
}
public void setMulticol_lst(String[] Value) {
for(int i=0;i<value.length;i++)
{
System.out.println("in array" +str[i]);
this.Multicol_lst[i] = Value[i];
}
public String[] getMulticol_lst()
{
return this.Multicol_lst;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String str[]=request.getparameter Values("Mu lticol_lst ");
String str[]=request.getParameter Values("Mu lticol_lst ");
ASKER
Thanks for your response,
String[] Multicol_lst;
public void setMulticol_lst(String[] Value) {
String str[] = request.getParameterValues (Multicol_ lst);
for(int i=0;i<Value.length;i++) {
System.out.println("in array" +str[i]);
}
}
public String[] getMulticol_lst() {
return this.Mutlicol_lst;
}
And in MsearchResults.JSP:
<%
String str[] = request.getParameterValues ("Multicol _lst");
Simple_SearchBean.processR equest(req uest,sessi on);
Simple_SearchBean.setMulti col_lst(st r);
I am able to compile the bean with no errors, but If I reach the page ResultPage.jsp, I am receiving nullpointerException.
java.lang.NullPointerExcep tion
at PlanningBeans.Simple_Searc hBean.setM ulticol_ls t(Simple_S earchBean. jav
a:51)
at examples.jsp.Planning_0005 fdatabaseS earch.Msea rchResults _jsp_1._js pSe
rvice(MsearchResults_jsp_1 .java:103)
I am wondering if I have to use Indexed property in the Bean to access the array? Am I using Indexed property or somethng else? PLease help
Thanks,
aruna
String[] Multicol_lst;
public void setMulticol_lst(String[] Value) {
String str[] = request.getParameterValues
for(int i=0;i<Value.length;i++) {
System.out.println("in array" +str[i]);
}
}
public String[] getMulticol_lst() {
return this.Mutlicol_lst;
}
And in MsearchResults.JSP:
<%
String str[] = request.getParameterValues
Simple_SearchBean.processR
Simple_SearchBean.setMulti
I am able to compile the bean with no errors, but If I reach the page ResultPage.jsp, I am receiving nullpointerException.
java.lang.NullPointerExcep
at PlanningBeans.Simple_Searc
a:51)
at examples.jsp.Planning_0005
rvice(MsearchResults_jsp_1
I am wondering if I have to use Indexed property in the Bean to access the array? Am I using Indexed property or somethng else? PLease help
Thanks,
aruna
can you post your jsp also....
i think there is problem with your jsp
i think there is problem with your jsp
ASKER
*******There is nothing fancy in my jsp. There are hardly more than 2 lines of code in it.
<HTML>
<HEAD>
<TITLE>
MsearchRESULTS
</TITLE>
</HEAD>
<BODY>
<form method=GET action="MainPage.jsp">
<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>
<jsp:useBean id="Simple_SearchBean" scope="session" class="PlanningBeans.Simpl e_SearchBe an" />
<jsp:setProperty name="Simple_SearchBean" property="*" />
<%
String str[] = request.getParameterValues ("Multicol _lst");
Simple_SearchBean.setMulti col_lst(st r);
%>
</BODY>
</HTML>
************BEANS***Before i posted wrong code in the beans. These are the ones for which I am getting the above posted error.*************
public void setMulticol_lst(String[] Value) {
for(int i=0;i<Value.length;i++) {
this.Multicol_lst[i] = Value[i];
}
}
public String[] getMulticol_lst() {
return this.Multicol_lst;
}
<HTML>
<HEAD>
<TITLE>
MsearchRESULTS
</TITLE>
</HEAD>
<BODY>
<form method=GET action="MainPage.jsp">
<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>
<jsp:useBean id="Simple_SearchBean" scope="session" class="PlanningBeans.Simpl
<jsp:setProperty name="Simple_SearchBean" property="*" />
<%
String str[] = request.getParameterValues
Simple_SearchBean.setMulti
%>
</BODY>
</HTML>
************BEANS***Before
public void setMulticol_lst(String[] Value) {
for(int i=0;i<Value.length;i++) {
this.Multicol_lst[i] = Value[i];
}
}
public String[] getMulticol_lst() {
return this.Multicol_lst;
}
this code seems ok....
what is the jsp which is forwarding to this Jsp..i mean the jsp where u have defined your list box.....
what is the jsp which is forwarding to this Jsp..i mean the jsp where u have defined your list box.....
ASKER
HTML>
<HEAD>
<TITLE>
Search PAGE
</TITLE>
</HEAD>
<BODY>
<form method=GET action="MainPage.jsp">
<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>
<jsp:useBean id="Simple_SearchBean" scope="session" class="PlanningBeans.Simpl e_SearchBe an" />
<jsp:setProperty name="Simple_SearchBean" property="*" />
<%
Simple_SearchBean.processR equest(req uest,sessi on);
Vector Logicalnm_vec = new Vector ((Vector) Simple_SearchBean.getLogic alnm_vec() );
Vector Col_vec = new Vector ((Vector) Simple_SearchBean.getCol_v ec());
ListIterator iter_ln = Logicalnm_vec.listIterator ();
ListIterator iter_col = Col_vec.listIterator();
%>
<b>Simple Search</b> <br/>
Search here by logical name and all information related to that logical name will be displayed.
<br />
<br />
Select the Logical Name from the list provided.
</br>
Logical name or server name:
<select name="Logicalnm_lst" >
<%
while (iter_ln.hasNext()) {
String value = (String)iter_ln.next();
%>
<option value = "<%=value%>"><%=value%></o ption>
<%
}
%>
</select>
</br>
</br>
<INPUT TYPE="submit" name="simplesearch" value="Submit">
<input type=reset value="Reset" >
</br>
<Pre>
<b>Multiple Search</b>
<SELECT NAME="Multicol_lst" SIZE=3 MULTIPLE>
<%
while (iter_col.hasNext()) {
String value = (String)iter_col.next();
%>
<option value = "<%=value%>"><%=value%></o ption>
<%
}
%>
</select>
<INPUT TYPE="submit" name="multiplesearch" VALUE="Submit">
</pre>
</BODY>
</html>
<HEAD>
<TITLE>
Search PAGE
</TITLE>
</HEAD>
<BODY>
<form method=GET action="MainPage.jsp">
<%@ page language="java" session="true"%>
<%@ page import = "java.util.*" %>
<jsp:useBean id="Simple_SearchBean" scope="session" class="PlanningBeans.Simpl
<jsp:setProperty name="Simple_SearchBean" property="*" />
<%
Simple_SearchBean.processR
Vector Logicalnm_vec = new Vector ((Vector) Simple_SearchBean.getLogic
Vector Col_vec = new Vector ((Vector) Simple_SearchBean.getCol_v
ListIterator iter_ln = Logicalnm_vec.listIterator
ListIterator iter_col = Col_vec.listIterator();
%>
<b>Simple Search</b> <br/>
Search here by logical name and all information related to that logical name will be displayed.
<br />
<br />
Select the Logical Name from the list provided.
</br>
Logical name or server name:
<select name="Logicalnm_lst" >
<%
while (iter_ln.hasNext()) {
String value = (String)iter_ln.next();
%>
<option value = "<%=value%>"><%=value%></o
<%
}
%>
</select>
</br>
</br>
<INPUT TYPE="submit" name="simplesearch" value="Submit">
<input type=reset value="Reset" >
</br>
<Pre>
<b>Multiple Search</b>
<SELECT NAME="Multicol_lst" SIZE=3 MULTIPLE>
<%
while (iter_col.hasNext()) {
String value = (String)iter_col.next();
%>
<option value = "<%=value%>"><%=value%></o
<%
}
%>
</select>
<INPUT TYPE="submit" name="multiplesearch" VALUE="Submit">
</pre>
</BODY>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello kupra1,
I tried to check the null before attempting to loop, but I am still getting the same error.
So, I changed the bean to see if the value is returning at all .
String Multicol_lst;
public void setMulticol_lst(String Multicol_lst) { this.Multicol_lst=Multicol _lst; }
public String getMulticol_lst() { return this.Multicol_lst; }
public void processRequest(HttpServlet Request request,HttpSession session)throws ServletException,IOExcepti on {
System.out.println("listbo x results"+request.getParame ter("Multi col_lst")) ;
}
***********In the JSP************
I jsut used this statement:
Simple_SearchBean.processR equest(req uest,sessi on);
************************** ********
In the list box there will ID, Vendor, Model etc and I selected the first three and hit the submit button. It took me to the nextpage and in the comand prompt it printed just the first item "ID". So this confirms that I am using exact variables in both Bean and JSP.
Can you tell how can I modify the get and set methods to read all the items selected in the listbox and return them .
Thanks,
Nubee
I tried to check the null before attempting to loop, but I am still getting the same error.
So, I changed the bean to see if the value is returning at all .
String Multicol_lst;
public void setMulticol_lst(String Multicol_lst) { this.Multicol_lst=Multicol
public String getMulticol_lst() { return this.Multicol_lst; }
public void processRequest(HttpServlet
System.out.println("listbo
}
***********In the JSP************
I jsut used this statement:
Simple_SearchBean.processR
**************************
In the list box there will ID, Vendor, Model etc and I selected the first three and hit the submit button. It took me to the nextpage and in the comand prompt it printed just the first item "ID". So this confirms that I am using exact variables in both Bean and JSP.
Can you tell how can I modify the get and set methods to read all the items selected in the listbox and return them .
Thanks,
Nubee
first a little clarification: why r u doing this?
<jsp:setProperty name="Simple_SearchBean" property="*" />
when you are explicitly setting like this.
String str[] = request.getParameterValues ("Multicol _lst");
Simple_SearchBean.setMulti col_lst(st r);
Just to check, print out the length of String str[]. Is it 3 or 1? If it's 1, then there is something wrong with the way values are getting passed on clicking the submit button.
Waiting for your reply.
<jsp:setProperty name="Simple_SearchBean" property="*" />
when you are explicitly setting like this.
String str[] = request.getParameterValues
Simple_SearchBean.setMulti
Just to check, print out the length of String str[]. Is it 3 or 1? If it's 1, then there is something wrong with the way values are getting passed on clicking the submit button.
Waiting for your reply.
should be
public void setMulticol_lst(String[] Value)