簡體   English   中英

jsp和servlet之間的通信錯誤?

[英]Communication error between jsp and servlet?

我的jsp頁面中有一個下拉列表,其中有多個網站名稱,例如google.com等。還有一個用作關鍵字搜索的文本框。

還有一個用於webcrwaling的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);


        }
        }
    }
  }

在這里,您可以采用虛擬表格。 然后在選擇更改后,您可以調用函數並使用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);
                }
        });    
}

根據你的,這是我的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);


        }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM