Sending Data From Jsp Page To Javascript In Another Page
I'm trying to send the data got from network in JSP page to html file in order to display it, Here I wrote an ajax function but no data coming to JS in html. How to resolve this is
Solution 1:
Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. See How to avoid Java Code in JSP-Files?
Soltion
In Ajax function call servlet instead of JSP
Ajax
$.ajax({
url:'NetDataServlet', // call servlet NetDataServletcache:false,
success:function(data){
alert(data);
},
error:function(){
alert("failed to fecth data");
}
});
Servlet
Create servlet with name NetDataServlet
and inside Get
method add following code
@OverrideprotectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException {
//all your scriptlet code from JSP here
......
.......
........
Stringmesg="success"
response.setContentType("text/plain"); // Set content type of the response.
response.setCharacterEncoding("UTF-8");
response.getWriter().write(mesg); // Write response body.
}
Deployment descriptor (web.xml) for the Servlet
<servlet><servlet-name>NetDataServlet</servlet-name><servlet-class>com.stackoverflow.NetDataServlet</servlet-class></servlet><servlet-mapping><servlet-name>NetDataServlet</servlet-name><url-pattern>/NetDataServlet/*</url-pattern></servlet-mapping>
Post a Comment for "Sending Data From Jsp Page To Javascript In Another Page"