0
我想填充在頁面加載,但它不工作如何填充在頁面加載事件選擇框,使用JSP和Ajax
這裏選擇框是我的html代碼文件1
<button id="edit-btn" onclick="popupwindow('edituser.jsp','Add new user',500,300)">Edit user</button>
文件2
<script>
$(document).ready(function(){
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/MyServlet",
data: "action=list",
success: function(data){
alert("success");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown)
}
});
});
</script>
<select name="currentusername" id="current-username" style="width:157px;">
</select>
MYDAO
public List getAllUserNames(){
List<UserBean> users = new ArrayList<UserBean>();
try {
String sql = "SELECT username FROM users ";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs != null) {
while (rs.next()) {
UserBean userBean = new UserBean();
System.out.println(rs.getString("username"));
userBean.setUsername(rs.getString("username"));
users.add(userBean);
}
}
else{
System.out.println("No users");
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
Servlet的
if(action.equalsIgnoreCase("list")){
System.out.println("list");
request.setAttribute("users", dao.getAllUserNames());
}