如何在jsp中实现关键字搜索

时间:2015-03-25 14:12:35

标签: java jsp

我是计算机科学的bca学生。    我在java服务器页面中有一个项目,我必须在其中进行优化搜索。

我设计了一个jsp页面,其中有一个包含多个url的下拉菜单。 还有一个关键字文本框。 当我点击一个按钮时,它会从下拉菜单重定向到选定的网址。 但我不知道如何检索jsp页面中与关键字相关的链接。

以下是我的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 ="./Main.java"   method="post">
   <center>
    <h1>  Optimzed Search</h1>
     Choose Website:    

       <select  name ="URL" >
      <%
          while(rs.next())
         {
       %>
    <option   value="<%=rs.getString(1) %>">
    <% out.println(rs.getString(1)); %>

    </option>

   <% } %>

    </select>
 <% }
   catch(Exception e)
   {
    out.println("Wrong Input" +e);
   }    
%>
 <br>
  Enter Keyword:
    <input Type="text" name="name" />
    <input type="button" value="submit"
   onclick = "window.location.href=this.form.URL.
     options[this.form.URL.selectedIndex].value"/>
                  </center>
  </form>
   </body>
   </html>

..........................

以下是我的Main.java文件,其中链接正在重新启动但在控制台上。我想在浏览器中显示..

请帮帮我..我很郁闷..


  import java.io.IOException;
  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;


  public class Main {
  public static DB db = new DB();

 public static void main(String[] args) throws SQLException, 
IOException 
 {
db.runSql2("TRUNCATE Record;");
processPage("http://www.wikipedia.com");
}

  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.wikipedia.com").get();

  if(doc.text().contains("plain 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("wikipedia"))
 processPage(link.attr("abs:href"));
}
}
}
}

0 个答案:

没有答案