我遇到了如何在浏览器上显示网址链接的问题。
下面是我的servlet文件和jsp文件。
Search.java
:
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.PrintWriter;
public class Search extends HttpServlet
{
private static final long serialVersionUID = 1L;
public static DB db = new DB();
protected void doGet(HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException
{
PrintWriter out=response.getWriter();
try {
db.runSql2("TRUNCATE Record;");
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
processPage("http://www.yahoo.com");
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void processPage(String URL) throws SQLException,
IOException
{
//check if the given URL is already in database
String sql = "select * from Record where URL = '"+URL+"'";
ResultSet rs = db.runSql(sql);
if(rs.next())
{
}
else
{
//store the URL to database to avoid parsing again
sql = "INSERT INTO `tenderMysql`.`Record` " + "(`URL`)
VALUES " + "(?);";
PreparedStatement stmt = db.conn.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, URL);
stmt.execute();
//get useful information
Document doc = Jsoup.connect("http://www.yahoo.com").get();
if(doc.text().contains("crash")){
System.out.println(URL);
}
//get all links and recursively call the processPage method
Elements questions = doc.select("a[href]");
for(Element link: questions)
{
if(link.attr("href").contains("www"))
processPage(link.attr("abs:href"));
}
}
}
}
下面给出的是我的jsp文件,我必须在浏览器上显示链接。
Search.jsp
:
<%@ 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=UTF-8">
<title>
Select website name from DropdownList
</title>
</head>
<body bgcolor="8B4513">
<form action="HelloServlet" >
<center>
<h1> Optimzed Search</h1>
<br>
</center>
</form>
</body>
</html>
答案 0 :(得分:0)
如果你想在普通的html页面上打印出来,你可以写出来。
protected void doGet(...)
{
PrintWriter out=response.getWriter();
processPage("http://www.yahoo.com", out);
}
public static void processPage(String URL, PrintWriter out)
{
....
if(doc.text().contains("crash")){
out.println("<p>" + URL+ "</p>");
}
}
将依赖jar添加到eclipse中的部署资源列表:
project -- > properties --> Deployment Assembly -- > Add -
-> Archive from File system ---> Add --> Select your jar (in this case soup)