0
我是使用json和ajax的新手。請有人幫助我如何將值附加到選項標籤。值被打印在頁面上而不是在選項標籤內。我見過這麼多例子,但不明白。如何使用json檢索從servlet到jsp的值
Retrieveservlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("action is called inside dropdown");
ViewValuesInDB daoobj = new ViewValuesInDB();
ArrayList<String> list = new ArrayList<String>();
String json = null;
try {
list = daoobj.makeConnection();
System.out.println(list);
if(null == list){
System.out.println("list is null");
}
else{
System.out.println("list has values");
}
System.out.println("size of ids list :"+list.size());
/*using json*/
json = new Gson().toJson(list);
System.out.println(json);
response.setContentType("application/json");
response.getWriter().write(json);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我的JSP代碼:
<script>
$(document).ready(function() {
$.get("Retrieveservlet", function(responseJson) {
('#dropDownId').append($('<option>').text(value)('value'));
});
});
</script>
<body>
<label for="dropDownId">Country*:</label>
<select id="dropDownId">
</select>
</body>
我建議你閱讀了這個問題 - https://stackoverflow.com/questions/3608891/pass-variables-from-servlet-to-jsp – fabfas
嗨fabfas.Thanks的鏈接。我紅色的鏈接。我已經實現了這個使用jstl和在servlet中使用setattribute。我試圖現在使用json實現。任何方式在json中實現這個? – Rebecca
@Rebecca Servlet - JSP連接不能像那樣工作。 JSP預編譯成Java並可以訪問傳遞給它的Java對象。這一切都發生在服務器端。 – Jaydee