在我的jsp頁面中有一個下拉列表,其中有幾個網站名稱,例如google.com等。 還有一個文本框用作關鍵字搜索。jsp和servlet之間的通信錯誤?
此外還有一個用於webcwwaling的servlet文件。 現在,當我從下拉列表中選擇任何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);
}
}
}
}
所以你卡在什麼部分? – silentprogrammer 2015-04-01 10:30:02
你的具體問題是什麼?你想讓我們爲你寫一切嗎?你是否希望我們在你發佈的代碼牆上猜測可能是什麼問題?請仔細告訴我們您希望此代碼執行的操作以及代碼執行的操作。發佈任何相關的錯誤消息。 – 2015-04-01 10:32:31
因此,當您從選擇選項中選擇選項時,您需要執行servlet。我是正確的? – Pratik 2015-04-01 10:33:44