0

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 issue??? Ajax:

$.ajax({
  url:'anr_data.jsp',
  cache:false,
   success:function(data,status){   
    alert(data);    
  },
  error:function(){
     alert("failed to fecth data");
 }
});

Here is my JSP page JSP:

<%@page import="java.net.URI"%>

<%@page import="java.io.BufferedReader"%>

<%@page import="org.apache.http.client.methods.HttpGet,java.io.InputStreamReader"%>

<%@page import="java.io.BufferedReader,java.io.IOException"%>

<%@page import="org.apache.http.client.methods.HttpGet,org.apache.http.HttpResponse" %>

<%@page import="org.apache.http.impl.client.DefaultHttpClient" %>

<%@page import="org.apache.http.client.HttpClient" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%

// get the input values
double val1 = 10;
double val2 = 12;
HttpResponse response1 ;
int responseCode = 0; 
String errorMsg = "";
try { 
String url = "http://10.138.89.70:9090/anrs";

HttpGet request1 = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
// add request header
//request.addHeader("User-Agent", USER_AGENT);
response1 =  client.execute(request1);

System.out.println("response is "+response1.toString());
responseCode = response1.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(new     InputStreamReader(response1.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
    result.append(line);
}
out.println(result); 
System.out.println("result is "+result);
    } catch (Exception e) {
System.out.print(e.toString());
    errorMsg = "Non-numeric input";
  }
 %>
5
  • how are you sending response back to the Ajax function? Commented Dec 24, 2013 at 9:30
  • I'm calling ajax function in a java script @Aniket Commented Dec 24, 2013 at 9:34
  • Why dont you simply use printwriter in your jsp code. Then your response will be captured in data variable in your javascript function Commented Dec 24, 2013 at 9:36
  • @Harikrishna : yes, but you are not sending any response back to the called function. Commented Dec 24, 2013 at 9:37
  • I'm calling jsp file in a ajax function using url Commented Dec 24, 2013 at 10:04

1 Answer 1

2

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 NetDataServlet
   cache: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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
                                                         ServletException, IOException {   

 //all your scriptlet code from JSP here
 ......
 .......
 ........
 String mesg = "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>  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.