如何将数据从包含arraylist的列表框复制到jsp页面中的文本框?

时间:2014-04-04 07:22:06

标签: jquery jsp arraylist

我有一个包含列表框和文本框的jsp页面。我正在从一个servlet向这个jsp页面发送一个Array List对象。列表框由arraylist中包含的数据填充。现在,我想将所选项目(一次一个)从列表框复制到文本框。我无法做到。有人可以帮忙吗?

servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException {
String db_url = "jdbc:mysql://localhost/s_data";
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("search_radio_box");

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("expage_edit_beta.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:
<html>    
<head>
<%!
int num=0;
String uname;
User obj;
User objtemp;
int limit=0;
int index;
String nametemp;
ArrayList ulisttemp;
ArrayList ulist;
%>
</head>
<body>
<form name="edit_srchform"  method="get" action="EditUserBetaServlet">
<div id="search_radio_div">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="search_radio_box"   value="username">User Name
<br>&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="search_radio_box" value="teamname">Team Name</div>
<div>
<spanstyle="width: 90px; background-color: #c0c0c0; top: 46px; left: 76px;   position: absolute">&nbsp;Search By
</span>
</div>
<div id="srchtext_div">
<input type="text" name="srchtext1" id="srchtext1"style="width: 250px" class="text"> 
<input type="submit" name="srch" value="Search"></div>
</form>
<select id="edit_listbox" name="edit_listbox" size="14">
<%
if(request.getAttribute("status") != null){
ulist=(ArrayList)request.getAttribute("datalist");
limit=ulist.size();
num=0;
while(num<limit){
obj=(User)ulist.get(num);
uname=obj.getfname()+" "+obj.getlname();
%>
<option><%=uname%></option>
<%
num=num+1;
}  
}
%>  
</select>
</body>

</html>

渲染的html是:

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" rel="stylesheet" href="one.css">
<title>Edit User</title>
<script src="jquery-latest.js"> 
</script>

</head>
<body >

<div id="arlogo"><img src="arlogo.jpg"/></div>
<div id="applogo"><img src="logo.gif"/></div>
<img id="welcomelogo" src="userlogo.jpg" style="width=30px;height:30px;left:810px;top:91px;position:absolute"/>
<div id="welcome">
Welcome
</div>
<div id="userinfo">
null

</div>
<div id="changepassword" >
&nbsp; Change Password &nbsp;
</div>
<form name="logout" action="Logout_adminServlet">
<div id="logout" >
&nbsp; <button type="submit">Log out</button> &nbsp;
</div>
</form>

<div id="menubar">
<ul>
<li><a href="adminpage_home.jsp">Home</a></li>
<li><a href="adminpage_newuser.jsp">New User</a></li>
<li><a href="adminpage_edituser_beta.jsp">Edit User</a></li>
<li><a href="adminpage_deleteuser.jsp">Delete User</a></li>
<li><a href="adminpage_stat.jsp">Statistics</a></li>
<li><a href="adminpage_help.jsp">Help</a></li>
</ul>
</div>


    <div id="mainheader"></div>
    <div id="commondiv">
        <div id="admin_usereditbox">
            <div id="heading"
                style="position: absolute; left: 380px; top: 10px; text-align: center; font-family: Ubuntu; font-size: 20px">Edit
                User Data</div>

            <form name="admin_edit_srchform"  method="get" action="EditUserBetaServlet">
                <div id="admin_search_radio_div">
                    <br>&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio"
                        name="search_radio_box" value="username">User Name<br>
                    &nbsp;&nbsp;&nbsp;&nbsp; <input type="radio"
                        name="search_radio_box" value="teamname">Team Name
                </div>
                <div>
                    <span
                        style="width: 90px; background-color: #c0c0c0; top: 46px; left: 76px; position: absolute">&nbsp;Search
                        By</span>
                </div>
                <div id="admin_srchtext_div">
                    <input type="text" name="srchtext1" id="srchtext1"
                        style="width: 250px" class="text"> <input type="submit"
                        name="srch" value="Search">
                </div>
            </form>

            <div id="admin_edit_textbox">
                <form name="admin_edit_textform" method="post" action="EditUserBetaServlet">
                    <!--  sending data to itself-->
                    <br>
                    <table id="admin_edit_table">
                        <tr>
                            <td>First Name</td>
                            <td><input type="text" size="30" name="f_name" id="f_name" class="text"><br></td>
                        </tr>
                        <tr>
                            <td>Last Name</td>
                            <td><input type="text" size="30" name="l_name" id="l_name" class="text"><br></td>
                        </tr>
                        <tr>
                            <td>Team Name</td>
                            <td><input type="text" size="30" name="t_name" id="t_name" class="text"><br></td>
                        </tr>
                        <tr>
                            <td>Role</td>
                            <td><input type="text" size="30" name="role" id="role" class="text"><br></td>
                        </tr>
                        <tr>
                            <td>Phone</td>
                            <td><input type="text" size="30" name="phone" id="phone" class="text"><br></td>
                        </tr>
                        <tr>
                            <td>Email</td>
                            <td><input type="text" size="30" name="email" id="email" class="text"><br></td>
                        </tr>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                        <tr>
                            <td></td>
                            <td><button type="submit" name="add"
                                    style="left: 100px; position: absolute; width: 70px">Add</button>
                                <input type="reset" name="reset" value="Reset"
                                style="left: 180px; position: absolute; width: 70px"></td>
                        </tr>
                    </table>
                    <br>
                </form>
            </div>

            <div id="admin_edit_listbox_div">
                <select id="admin_edit_listbox" name="admin_edit_listbox" size="14">

                    <option>Amitabh Roy</option>

                    <option>Titan Bran</option>

                    <option>Manoj Roy</option>

                    <option>Lalit jain</option>

                    <option>kumar jain</option>

                    <option>Amitabh kumar</option>

                </select>
            </div>
        </div>
    </div>
    <div id="mainfooter"></div>
    <div id="footer">
<center>
<span style="top:10px;position:relative">
Copyright &copy; Reserved
</span>
</center>
</div>
    <script> 
$(function(){





});


</script>
</body>
</html>

0 个答案:

没有答案