我需要通过对我之前触发的请求的响应来将控件检索回 Jsp。但我面临的问题是我无法在 jsp 中获得响应。
我浏览了几篇文章并尝试了一些建议,但似乎没有一个对我有用。请指出我的错误在哪里。
下面是我的尝试:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
log.info("IN form doGet");
request.setCharacterEncoding("utf-8");
String selectedClass = request.getParameter("key");
log.info(selectedClass);
String[] section = null;
DBHandler dbhandler = new DBHandler();
dbhandler.makeConnection();
if (true == dbhandler.verifyConnection()) {
section = dbhandler.getSection(selectedClass);
}
for (String str : section) {
log.info(str);
}
Logger log;
if (null != section) {
request.setAttribute(Appconstants.Section, section);
}
response.setHeader("Access-Control-Allow-Origin", "*");
response.sendRedirect("/ReportFetcher/WebContent/FirstJSP.jsp");
// RequestDispatcher view = request.getRequestDispatcher("/First.jsp");
// view.forward(request, response);
}
以下是jsp部分:
function funcOnChange() {
var index = document.detail.Class.selectedIndex;
var valueSelected = document.detail.Class.options[index].value;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
// Check to see if this state change was "request complete", and
// there was no server error (404 Not Found, 500 Server Error, etc)
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var substring=xmlHttp.responseText;
alert("Alert Dialog! Gaurav");
}
if (xmlhttp.readyState == 404){
alert("404 error");
}
if (xmlhttp.readyState == 500){
alert("500 error");
}
}
xhr.open('GET', 'http://localhost:8080/ReportFetcher/FormHandler?key='+encodeURIComponent(valueSelected), true);
xhr.send(null);
return false;
}