2015-04-01 66 views
1

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


     } 
     } 
    } 
    } 
+1

所以你卡在什麼部分? – silentprogrammer 2015-04-01 10:30:02

+0

你的具體問題是什麼?你想讓我們爲你寫一切嗎?你是否希望我們在你發佈的代碼牆上猜測可能是什麼問題?請仔細告訴我們您希望此代碼執行的操作以及代碼執行的操作。發佈任何相關的錯誤消息。 – 2015-04-01 10:32:31

+0

因此,當您從選擇選項中選擇選項時,您需要執行servlet。我是正確的? – Pratik 2015-04-01 10:33:44

回答

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); 
       } 
     });  
} 
+0

評論不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/74576/discussion-on-answer-by-pratik-communication-error-between-jsp-and-servlet)。 – Taryn 2015-04-06 15:03:08

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); 


     } 
     } 
    } 
}