I'm sending a POST form using AJAX, and i'm trying to send back this value to my JSP, but I do not know exactly how to do it .
This is my AJAX.
function ajax() {
var e = document.getElementById("combo2");
var e2 = e.options[e.selectedIndex].value;
$.post( "MiddleContentApi", { cidades: e2})
.done(function( data ) {
alert( "Data Loaded: " + data );
});
}
document.getElementById("okCity").addEventListener("click",ajax);
My Servlet :
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Integer cidadeId = Integer.parseInt(request.getParameter("cidades"));
System.out.print(cidadeId);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(String.valueOf(cidadeId));
}
Ajax is working totally ok, the value is returning to my "data" and the alert(data) is working either, but what I really need is to access this value returned using scriptles <% %> in jsp, but if I try to do something like :
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Integer cidadeId = Integer.parseInt(request.getParameter("cidades"));
request.setAttribute("numero",cidadeId);
RequestDispatcher rd = request.getRequestDispatcher("estados.jsp");
rd.forward(request,response);
}
This's just not working, and I can't acess the data in jsp by using :
Object jbo = request.getAttribute("numero");
Does someone now how can I do this?