Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting the select value using myrequest.getParameter(..)  RSS feed

 
herbey zepeda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Im trying to display the value selected from a select combobox using a servlet using req.getParameter(myComboName)
but it displays null, I tried using a text box and it worked fine but I cant manage to get the select value, does anybody have a suggestion?

Thank you
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the correct way to do it. Please show us the HTML code for your <select> element (which is not a combobox, by the way) and the exact code from your servlet that is trying to retrieve its value.
 
herbey zepeda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Bear here it is:

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import java.sql.*;

// MyServlet.java



public class hzUser extends HttpServlet
{

public void doGet ( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {

HttpSession session=req.getSession();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
res.setContentType( "text/html");
PrintWriter out = res.getWriter();
String individualCharsCboName="individualCharacteristicsFilters";

this.openForm(out);


String currentUser= (String)session.getAttribute("myCurrentUser");
String currentCaller;


out.println( "hello "+currentUser +"<br>");
out.println(" myIsPostback= "+(Boolean)session.getAttribute("myUserIsPostBack"));






try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hzjoyfinalproject","lorito","rayita");
stmt = con.createStatement();
//Catalog filling
String queryIndividualCharacteristicsCatalog=
"select name from individualCharacteristics order by name";
rs = stmt.executeQuery(queryIndividualCharacteristicsCatalog);




this.openTable(out);
createCombo(out,rs,"Search Matches by Individual Characteristics",individualCharsCboName);
this.closeTable(out);



if((Boolean)session.getAttribute("myUserIsPostBack")==false)
{
String query1=
"select searchingUserId,searchedUserId,count(*) as NumberOfVariablesMatched from("+
"select w.userId as searchingUserId,w.characteristicname as wants,w.value as valueWanted,h.userId as searchedUserId, "+
" h.characteristicname as has,h.value as valueHad"+
" from has as h"+
" inner join wants as w on (w.userId!=h.userId and w.characteristicname=h.characteristicname and w.value=h.value)"+
" where w.userId='"+(String)session.getAttribute("myCurrentUser")+"'"+
" order by w.userId,w.characteristicname ,h.userId,h.characteristicname"+
")a"+
" group by a.searchingUserId,a.searchedUserId"+
" order by count(*) desc,a.searchedUserId"
;
String query2=
"select w.userId as searchingUserId,w.characteristicname as wants,w.value as valueWanted,h.userId as searchedUserId, "+
" h.characteristicname as has,h.value as valueHad"+
" from has as h"+
" inner join wants as w on (w.userId!=h.userId and w.characteristicname=h.characteristicname and w.value=h.value)"+
" where w.userId='"+(String)session.getAttribute("myCurrentUser")+"'"+
" order by w.userId,w.characteristicname ,h.userId,h.characteristicname";

rs = stmt.executeQuery(query1); this.printTableWithHeaders(out,rs,"Matching users by rank");
rs = stmt.executeQuery(query2); this.printTableWithHeaders(out,rs,"All matches in the system");

session.setAttribute("myUserIsPostBack",true);
}else
if((Boolean)session.getAttribute("myUserIsPostBack")==true)
{
out.println("value of my userCombo= "+ req.getParameter("myCbo"));

}

rs.close();
con.close();
this.closeForm(out);




} catch (Exception e) { out.println("<LI> there was a problem"); }
out.close();
}////////end of do function


public void createCombo(PrintWriter out,ResultSet rs,String title,String controlName) throws Exception
{
out.println(title);
out.println(" <SELECT name=\"myCbo\" onChange=filter() id=\"cboTypeOfUser\" >");
while(rs.next()){
out.println("<OPTION value=\"\">"+rs.getString("name")+" </OPTION>");
}
out.println("</SELECT>");
//out.println(" <input id=\"pass\" type=\"text\" name=\"pass\"/>");
}





public void openForm(PrintWriter out)
{
String headBuilder=
" <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" ><HTML>"+
" <HEAD>"+
" <title>defsd</title>"+
" <meta content=\"Microsoft Visual Studio .NET 7.1\" name=\"GENERATOR\">"+
" <meta content=\"C#\" name=\"CODE_LANGUAGE\">"+
" <meta content=\"JavaScript\" name=\"vs_defaultClientScript\">"+
" <meta content=\"http://schemas.microsoft.com/intellisense/ie5\" name=\"vs_targetSchema\">"+

" <script type=\"text/javascript\">"+
" function filter(){ document.myUserForm.submit(); }</script>"+
" </HEAD>"+
" <body text=\"#d0f\" MS_POSITIONING=\"GridLayout\">"+
" <form name=\"myUserForm\" action=\"http://localhost:8081/pauloProject/servlets/servlet/hzUser\" id=\"myUserForm\">";

out.println(headBuilder);
}

public void closeForm(PrintWriter out)
{
out.println("</form></body></HTML>");
}


public void openTable(PrintWriter out) throws Exception
{
out.println("<TABLE id=\"Table1\" border=\"1\">");
}

public void closeTable(PrintWriter out) throws Exception
{
out.println("</TABLE>");
}

public void printTableWithHeaders(PrintWriter out,ResultSet rs,String tableTitle) throws Exception
{
out.println(tableTitle);
out.println("<TABLE id=\"Table1\" border=\"1\">");
out.println("<TR>");
for(int i=1;i<=rs.getMetaData().getColumnCount();i++ )
{
out.println("<TD>"+rs.getMetaData().getColumnName(i)+"</TD>");
}
out.println("</TR>");

while (rs.next()) {
out.println("<TR>");
for(int i=1;i<=rs.getMetaData().getColumnCount();i++ )
{
out.println("<TD>"+rs.getString(i)+"</TD>");
}
out.println("</TR>");
}
out.println("</TABLE>");
}

}//total end bracket
 
herbey zepeda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the servlet I copied is first called from another servlet, then it is called by the same html form it produced itself, I cant manage to get the selected values from the "SELECT" element, again if I use a text box input element instead of the SELECT and then submit the form, I can get the value of the text input with no problems, it is the SELECT element that is causing the error somewhere, or most probably I am forgetting something,

thank you
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OPTION's value is not set.
 
herbey zepeda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much Satou!

Everything works now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!