Redirect to Google Search JSP

This tutorial explains you how can you crate a google search in your page. Creating a google search means the text what you have entered into the textbox in your page, the google search should fetch the result related to your text.

Redirect to Google Search JSP

Redirect to Google Search JSP

This tutorial explains you how can you crate a google search in your page. Redircting to google search means the text what you have entered into the textbox in your page, the google search should fetch the result related to your text.

In this section we will discuss how to create a google search using a JSP page. We will use Eclipse to compile and deploy the application on Tomcat 7 server.

You can provide the Google search on your web page. This may help for searching the contents on web through your web page. To create a Google search on the web page we will use JSP.

Example

Here we will give an example of how to provide a google search on your web page. In this example we will create a JSP page where we will put a textbox into which a user can enter their text for search on web through Google search. As well as we will put a submit button after submitting which the page will be redirected to the Google search page with the result related to your query/text. To accomplish this task we will use the sendRedirect() method of HttpServletResponse interface which temporary redirects the resource control to another resources deployed on another server or contained in other domains.

Directory Structure

Source Code

search.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Google Search</title>
</head>
<body>
<form method="get" action="search.jsp">
<table>
<tr>
<td>Google Search </td>
<td><input type="text" name="search"/></td>
<td><input type="submit" value="search"/></td>
</tr>
</table>
<%
String text = request.getParameter("search");
if(!((text) == null))
{
String url = "http://www.google.com/#hl=en&tbo=d&site=&source=hp&q="+text;
response.sendRedirect(url);
}
%>
</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
<display-name>googleSearch</display-name>
<welcome-file-list>
<welcome-file>search.jsp</welcome-file> 
</welcome-file-list>
</web-app>

Output

When you will execute the above example i.e. googleSerach project then the output will be as follows :

1. At first the search.jsp page will be opened as follows :

2. When you will enter the text into the provided textbox in the above image for example I have given "roseindia" then the Google search result will be displayed as follows :

Download Source Code (WAR file)