Hello friends today I tell you how to extracting URL data using Jquery and Ajax in jsp.
Step:1- index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Facebook Extract Link Data</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#contentbox").keyup(function()
{
var content=$(this).val();
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var url= content.match(urlRegex);
if(url.length>0)
{
$("#linkbox").slideDown('show');
$("#linkbox").html("<img src='link_loader.gif'>");
$.get("getData.jsp?url="+url,function(response)
{
var title=(/<title>(.*?)<\/title>/m).exec(response)[1];
var logo=(/src='(.*?)'/m).exec(response)[1];
$("#linkbox").html("<img src='"+logo+"' class='img'/><div><b>"+title+"</b><br/>"+url)
});
}
return false();
});
});
</script>
<style>
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
#contentbox
{
width:458px; height:50px;
border:solid 2px #dedede;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
margin-bottom:6px;
}
.img
{
float:left; width:150px; margin-right:10px;
text-align:center;
}
#linkbox
{
border:solid 2px #dedede; min-height:50px; padding:15px;
display:none;
}
</style>
</head>
<body>
<div id="xxx"></div>
<div style="margin:50px; padding:10px; width:460px">
<div style="height:25px">
<div style="float:left"><b>Facebook Box</b><br />Eg: 9lessons programmin blog http://www.9lessons.info</div>
</div>
<textarea id="contentbox"></textarea>
<div id="linkbox">
</div>
</div>
</div>
</body>
</html>
Step:2-getData.jsp
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.BufferedInputStream"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String url=request.getParameter("url");
//System.out.println(url);
URL url1 = new URL(url);
System.out.println("111111111111 :"+url);
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
out.println(html);
System.out.println(html);
%>
</body>
</html>
Step:2-getData.jsp (Alternate )
In this case you need 3 jars
1.commons-httpclient.jar
2.commons-logging.jar
3.httpcore-4.0.1.jar
<%@page import="org.apache.commons.httpclient.*,org.apache.http.HttpStatus,
org.apache.commons.httpclient.methods.*"%>
<%
String url=request.getParameter("url");
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod( url );
try{
client.executeMethod( method );
if (method.getStatusCode() == HttpStatus.SC_OK)
{
String res= method.getResponseBodyAsString();
out.print(res);
}
else
{
out.println("Status Code of hitting URL: "+method.getStatusCode());
}
}catch(Exception e){ e.printStackTrace();}
%>
Out Put:
Step:1- index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Facebook Extract Link Data</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#contentbox").keyup(function()
{
var content=$(this).val();
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var url= content.match(urlRegex);
if(url.length>0)
{
$("#linkbox").slideDown('show');
$("#linkbox").html("<img src='link_loader.gif'>");
$.get("getData.jsp?url="+url,function(response)
{
var title=(/<title>(.*?)<\/title>/m).exec(response)[1];
var logo=(/src='(.*?)'/m).exec(response)[1];
$("#linkbox").html("<img src='"+logo+"' class='img'/><div><b>"+title+"</b><br/>"+url)
});
}
return false();
});
});
</script>
<style>
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
#contentbox
{
width:458px; height:50px;
border:solid 2px #dedede;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
margin-bottom:6px;
}
.img
{
float:left; width:150px; margin-right:10px;
text-align:center;
}
#linkbox
{
border:solid 2px #dedede; min-height:50px; padding:15px;
display:none;
}
</style>
</head>
<body>
<div id="xxx"></div>
<div style="margin:50px; padding:10px; width:460px">
<div style="height:25px">
<div style="float:left"><b>Facebook Box</b><br />Eg: 9lessons programmin blog http://www.9lessons.info</div>
</div>
<textarea id="contentbox"></textarea>
<div id="linkbox">
</div>
</div>
</div>
</body>
</html>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.BufferedInputStream"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String url=request.getParameter("url");
//System.out.println(url);
URL url1 = new URL(url);
System.out.println("111111111111 :"+url);
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
out.println(html);
System.out.println(html);
%>
</body>
</html>
Step:2-getData.jsp (Alternate )
In this case you need 3 jars
1.commons-httpclient.jar
2.commons-logging.jar
3.httpcore-4.0.1.jar
<%@page import="org.apache.commons.httpclient.*,org.apache.http.HttpStatus,
org.apache.commons.httpclient.methods.*"%>
<%
String url=request.getParameter("url");
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod( url );
try{
client.executeMethod( method );
if (method.getStatusCode() == HttpStatus.SC_OK)
{
String res= method.getResponseBodyAsString();
out.print(res);
}
else
{
out.println("Status Code of hitting URL: "+method.getStatusCode());
}
}catch(Exception e){ e.printStackTrace();}
%>
Out Put:
0 comments:
Post a Comment