Share on Google+Share on Google+

Amitabh Roy
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?
0 Answer(s)      3 years ago
Posted in : JSP-Servlet

I have a jsp page containing a list box and a text box. I am sending an Array List object from a servlet to this jsp page. The listbox gets populated by the data contained in the arraylist. Now, I want to copy the selected item(one at a time) from the list box to the text box. I am unable to do it. Can someone help?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String dburl = "jdbc:mysql://localhost/sdata"; String user = "dbuser"; String password = "abcd"; ResultSet rs = null; Connection conn=null; PreparedStatement pst = null; ArrayList<User> userlist=new ArrayList<User>();

int x=0; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, user, password);

String srchtext = request.getParameter("srchtext1"); String radio = request.getParameter("searchradiobox");

if ("teamname".equals(radio)) { pst = conn.prepareStatement("select * from User where team like ?"); pst.setString(1, "%"+srchtext+"%"); } else{ pst = conn.prepareStatement("(select * from User where fname like ?)union (select * from User where lname like ?) "); pst.setString(1, "%"+srchtext+"%"); pst.setString(2, "%"+srchtext+"%"); } rs = pst.executeQuery(); while(rs.next()){ User soluser=new User(); User.setdata(rs.getString("fname"),rs.getString("lname")); userlist.add(x,soluser); x=x+1;
} } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally{ try{ if(rs!=null) { rs.close(); } if(pst!=null) { pst.close(); } if(conn!=null) { conn.close(); } }catch(Exception e){ e.printStackTrace(); } } request.setAttribute("datalist",userlist ); RequestDispatcher rd=request.getRequestDispatcher("expageeditbeta.jsp"); rd.include(request,response); }

The User class is a POJO class. public class User{ String fname; String lname; public void setdata(String a,String b){ fname=a; lname=b; } public String getfname(){ return fname; } public String getlname(){ return lname; } }

The jsp page:
<%! int num=0; String uname; User obj; User objtemp; int limit=0; int index; String nametemp; ArrayList ulisttemp; ArrayList ulist; %>


     User Name
     Team Name
 Search By

Ads

View Answers









Related Tutorials/Questions & Answers:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training fat image from db fat image from db fat image from db fat image from db unable to get value returned from javascript variable in page How to change image size before adding to mysql using jsp Design a registration.jsp page with input for user registration details Cart Quantity jsp servlet coding issue servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? servlet,jsp login registration forms without database in ecalipse ide using hashmap to store the registraion details ? image pdf marking pdf marking Multiple image upload using JSP Read code from excel sheet and upload into database using JSP Student java about search engine Convert BLOB to PDF and save it to a location how to copy files from remote to local machine using SFTP SSL in java? Like Team viewer Socket and Servert -Jsp how to copy files from remote location to local using java? How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java how to create SOAP based web service in java? how to fetch data from mysql database table and draw a bar chart on that data using in jsp how to create users in LDAP using java? how to authenticate user using TIVOLI api in java? how to create users using IBM Tivoi API in jsp/servelet? Java/J2ee Please explain this code as many packages and classes are unknown to me .! how to download the uploaded folder files using jsp Login Query image is display from path of mysql database please reply the code to store dynamic fields in database? Reloading issue in jsp How to get Button Id ? calling one jap page from another jsp page login login Problem to get connection from DAO class to JDBC Page Loading Time Fix table's column's name row(1st row of the table) so that it does not move up when the table is scrolled up to view more rows below Sending hidden values

Ads

 
Advertisement null

Ads