我的jsp页面中有一个下拉列表,其中有几个网站名称,例如google.com等。 并且有一个文本框用作关键字搜索。
还有一个servlet文件,用于webcrwaling。 现在,当我从下拉列表中选择任何url时,它会连接到servlet,然后检索该特定关键字的链接。 如何实现这一点请帮助..
.jsp文件
<%@ page
import="java.sql.*"
%>
<%ResultSet rs=null; %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
Select website name from DropdownList
</title>
<link href="Desktop/style.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="8B4513">
<%
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection
("jdbc:mysql://localhost:3306/tendermysql","root","root");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery("select * from Record");
%>
<form action ="Search.java" method="post">
<center>
<h1> Welcome to Ezest Tender Optimzed Search</h1>
Choose Website:
<select name ="URL" >
<%
while(rs.next())
{
%>
<option value="<%=rs.getString(3) %>">
<% out.println(rs.getString(3)); %>
</option>
<% } %>
</select>
<% }
catch(Exception e)
{
out.println("Wrong Input" +e);
}
%>
<br>
Enter Keyword:
<input Type="text" name="name" />
<input type="submit" value="submit" />
</center>
</form>
</body>
</html>
.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 java.io.PrintWriter;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
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.mit.edu", out);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void processPage(String URL, PrintWriter out)
throws SQLException, IOException
{
//check if the given URL is already in database
String sql = "select * from Record where URL_Link = '"+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_Link`)
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.mit.edu").get();
if(doc.text().contains("education"))
{
out.println ("<a href='" +URL+ "'>"+URL+"</a>" );
}
else
{
out.println("There are no content");
}
out.println("<br/>");
out.println("<br/>");
//get all links and recursively call the processPage method
Elements questions = doc.select("a[href]");
for(Element link: questions){
if(link.attr("href").contains("mit.edu"))
processPage(link.attr("abs:href"),out);
}
}
}
}
答案 0 :(得分:0)
在这里你可以采用虚拟形式。 然后在更改选择时,您可以调用函数并使用javascript提交表单。
HTML
<select name ="URL" onchange="SubmitCall('<%=rs.getString(3) %>')">
<%
while(rs.next())
{
%>
<option value="<%=rs.getString(3) %>">
<% out.println(rs.getString(3)); %>
</option>
<% } %>
</select>
<% }
catch(Exception e)
{
out.println("Wrong Input" +e);
}
%>
<form action="Search" id="searchForm">
<input type="hidden" name="url" id="url">
</from>
的javaScript
function SubmitCall(valueObj){
//Set in hidden field
$("#url").val(valueObj);
$("#Search").submit();
//Using ajax
$.ajax({url: "Search",
data: $("#url").val(valueObj),
success: function(result){
alert(data);
}
});
}
答案 1 :(得分:0)
据你说这是我的jsp文件...
<form action="Search" id="searchForm">
<input type="hidden" name="url" id="url">
<center>
<h1> Welcome to Ezest Tender Optimzed Search</h1>
Choose Website:
<select name ="URL" onchange="SubmitCall('<%=rs.getString(2) %>')">
<%
while(rs.next())
{
%>
<option value="<%=rs.getString(2) %>">
<% out.println(rs.getString(2)); %>
</option>
<% } %>
</select>
<% }
catch(Exception e)
{
out.println("Wrong Input" +e);
}
%>
<script>
function SubmitCall(valueObj)
{
//Set in hidden field
$("#url").val(valueObj);
$("#Search").submit();
}
</script>
</center>
</form>
</body>
</html>
以下是我的java文件......
public static void processPage(String URL, PrintWriter out)
throws SQLException, IOException
{
//check if the given URL is already in database
String sql = "select * from Record where URL_Link = '"+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_Link`) VALUES " + "(?);";
PreparedStatement stmt = db.conn.prepareStatement
(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1,URL);
stmt.execute();
//get useful information
Document doc = Jsoup.connect("Link").get();
if(doc.text().contains("education"))
{
out.println ("<p>'" +URL+ "'>"+URL+"</p>" );
}
else
{
out.println("There are no content");
}
out.println("<br/>");
out.println("<br/>");
//get all links and recursively call the processPage method
Elements questions = doc.select("a[href]");
for(Element link: questions){
if(link.attr("href").contains("mit.edu"))
processPage(link.attr("abs:href"),out);
}
}
}
}