Sample Code Snippets

From DBSight Full-Text Search Engine/Platform Wiki

Table of contents


HTML Search Form

    <center>
        <form name=zs method=GET action=jsp1.jsp>
        <input type=hidden name=indexName value=BugDB>
        <input type=hidden name=length value=30>
        <input type=text name=q size=41 maxlength=2048 value="">
        <input type=submit name="btnG" value="dbSearch">
        </form>
    </center>

Java JSP

Direct Fetch And Display Search Result

This JSP file is named jsp1.jsp

    <%@ page language="java" contentType="text/html" %>
    <%if(request.getParameter("q")!=null){
      String indexServerURL = "http://localhost:8080/dbsight/search.do";
      %><%@ include file="indexResultInclude.jsp" %><%
    }%>

Reusable Included JSP code, indexResultInclude.jsp

This JSP file is named indexResultInclude.jsp

    <%@ page import="java.io.BufferedReader" %>
    <%@ page import="java.io.IOException" %>
    <%@ page import="java.io.InputStreamReader" %>
    <%@ page import="java.io.OutputStreamWriter" %>
    <%@ page import="java.net.URL" %>
    <%@ page import="java.net.URLConnection" %>
    <%@ page import="java.net.URLEncoder" %>
    <%@ page import="java.util.ArrayList" %>
    <%
    try{
        //1. Construct data
    
        String data = URLEncoder.encode("indexName", "UTF-8")
            +"="+URLEncoder.encode(((request.getParameter("indexName")!=null)?request.getParameter("indexName"):""), "UTF-8");
    
        data += ("&" + URLEncoder.encode("length", "UTF-8")
                  +"="+URLEncoder.encode(((request.getParameter("length")!=null)?request.getParameter("length"):""),"UTF-8"));
    
        data += ("&" + URLEncoder.encode("start", "UTF-8")
                  +"="+URLEncoder.encode(((request.getParameter("start")!=null)?request.getParameter("start"):""),"UTF-8"));
    
        data += ("&" + URLEncoder.encode("q", "UTF-8")
                  +"="+URLEncoder.encode(((request.getParameter("q")!=null)?request.getParameter("q"):""),"UTF-8"));
    
        //2. Send data
        URL tburl = new URL(indexServerURL);
        URLConnection conn = tburl.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
    
        //3. Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    
        //print out the response
        String line;
        while ((line = rd.readLine()) != null){
            out.println(line);
        }
    
        //4. close resources
        wr.close();
        rd.close();
    }catch (IOException e){
        out.println(e);
    }
    %>

Standard Tag Library Based Fetch And Display Search Result

    <%@ page language="java" contentType="text/html" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    
    <%if(request.getParameter("q")!=null){%>
      <c:import url="http://localhost:8080/dbsight/search.do" >
        <c:param name="indexName" value="BugDB" />
        <c:param name="length" value="30" />
        <c:param name="start" value='${param["start"]}' />
        <c:param name="q" value='${param["q"]}' />
      </c:import>
    <%}%>

Perl

Search Form

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use CGI;
    
    my $cgi = new CGI;
    
    print $cgi->header;
    print $cgi->start_html("Example CGI Search");
    print "<h1>Example CGI Search</h1>";
    print $cgi->start_form(-name=>'zs', -method=>'GET', -action=>'results.cgi');
    print $cgi->hidden('indexName', 'BugDB');
    print $cgi->hidden('length', '30');
    print $cgi->textfield(-name=>'q', -size=>'41',-maxlength=>'2048');
    print $cgi->submit('Action', 'dbSearch');
    print $cgi->end_form;
    print $cgi->end_html;

Fetch and display Search Result

    #!/usr/bin/perl
    # Example CGI script that talks to the index server
    
    use strict;
    use warnings;
    
    use CGI;
    use LWP::UserAgent;
    
    my $cgi = new CGI;
    
    my $browser = LWP::UserAgent->new;
    my $url = 'http://localhost:8080/dbsight/search.do';
    
    my $response = $browser->post(
        $url,
        Content_Type  => 'application/x-www-form-urlencoded',
        Content => [ $cgi->Vars ]
    );
    
    print "Content-type: text/html\n\n";
    
    if($response->is_success) {
        print $response->content;
    } else {
        print $response->status_line;
    }