在我的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