Wednesday, 8 October 2014

Directing to some other website from your Servlet - Designing a simple search functionality



Search.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
      <body>
            <form action="SearchCls">
                  <table>
                        <tr>
                              <td>Search What ? </td>
                              <td><input type="text" name="searchfield"></td>
                        </tr>
                        <tr>
                              <td>Using </td>
                              <td>
                                    <table>
                                          <tr><td><input type="radio" name="searchengine" value="Google">Google</td></tr>
                                          <tr><td><input type="radio" name="searchengine" value="MSN">MSN</td></tr>
                                          <tr><td><input type="radio" name="searchengine" value="Yahoo">Yahoo</td></tr>
                                    </table>
                              </td>
                        </tr>
                        <tr><td><input type="submit" value="Search"></td></tr>
                  </table>
            </form>
      </body>
</html>
/************************************************************/
SearchCls.java
package SearchPkg;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SearchCls
 */
@WebServlet("/SearchCls")
public class SearchCls extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchCls() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            PrintWriter pw=response.getWriter();
            String searchString=request.getParameter("searchfield");
            String searchEngine=request.getParameter("searchengine");
            if(searchEngine.equals("Google"))
                  response.sendRedirect("http://www.google.com/search?q="+searchString);
            else if(searchEngine.equals("Yahoo"))
                  response.sendRedirect("https://in.search.yahoo.com/search;_ylt=AlCaEqD7H2T6gVNXR8p89MCuitIF?p="+searchString+"&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-704&fp=1");
            else if(searchEngine.equals("MSN"))
                  response.sendRedirect("http://www.bing.com/search?q="+searchString);
           
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}

No comments:

Post a Comment