0

I have a .jsp page from a third party i need to get information from.My application is being developed in MVC4. how will i get information from this .jsp file in my application.

I tried using webrequest but the content is not there.

Regards Fatema

Fatema
  • 135
  • 1
  • 10
  • Not possible. JSP must be compiled first. Once it's compiled, you can try requesting it and then scrapping data from it. –  Nov 07 '13 at 12:06
  • It looks like you already have a correct answer, but what do you mean by "get information from"? Do you mean call the jsp from the server and parse the response? Maybe an example of what information you want and how it is currently presented to you... – Jeff Nov 07 '13 at 16:11
  • @Jeff Yes i want to parse the response this JSP page is a link provided to me from a third party. but unfortunalty i cant find a way to do it in .net. i tried javascript as well but its seems there is some "same origin policy" issue. confused as how to parse a jsp page from the application. – Fatema Nov 08 '13 at 04:47
  • 1- What do you expect from the jsp? (Hmlt, xml, json?) 2- Are you trying this on the client side via JS or on the server in a controller? – Jeff Nov 08 '13 at 16:18
  • @Jeff json or xml both will do. i am trying this on client side. – Fatema Nov 15 '13 at 10:37

2 Answers2

1

The JSP-page has to be actually rendered by a Servlet-Container like tomcat because the containing data are dynamical. Done so you can parse the HTML-output with your .net-application.

This might be the only way. To read the data directly from the jsp.

Anyway I suggest you find another way to retrieve the data like adding an API to you Java EE-application that the jsp is part of. Or access an existing one.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Randy
  • 1,299
  • 2
  • 10
  • 23
1

I think you have two options:

The first option is to do this on the client side using Ajax: (http://api.jquery.com/jQuery.ajax/

Code could look something like:

function CallOtherSite(otherSiteUrl) {  
$.ajax({
    url: otherSiteUrl,
    cache: false,
    success: function (html) {
     //This will be the html from the other site
    //parse the html/xml and do what you need with it.
    }
});

Because this is done with JavaScript on the client side you will most likely run into a problem with CORS. (http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx)

The other option and better option in my opinion is to do this on the server side. (Either in a controller or view with Razor) (It will be much easier in the contoller...)

try
{
    var request = (HttpWebRequest)WebRequest.Create(urlToOtherSite);
    request.Accept = "application/xml";
    request.Method = "GET";
    webResponse = (HttpWebResponse)request.GetResponse();
    sr = new StreamReader(webResponse.GetResponseStream());
    string responseText = sr.ReadToEnd();
}
catch(Exception ex)
{
}
finally
{
    if (sr != null) { sr.Close(); }
    if (webResponse != null) { webResponse.Close(); }
}

Then you can use the StreamReader to get the html/xml and do what you will with it.

Hope this helps...

Jeff
  • 2,728
  • 3
  • 24
  • 41