index.html
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="google" method="get">
Google Search:<input type="text" name="search"></br>
<input type="submit" value="Search">
</form>
</body>
</html>
GoogleSearch Servlet File:
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;
public class GoogleSearch extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String name=req.getParameter("search");
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
res.sendRedirect("https://www.google.com/#q="+name);
}
}
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" 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>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ss</servlet-name>
<servlet-class>GoogleSearch</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ss</servlet-name>
<url-pattern>/google</url-pattern>
</servlet-mapping>
</web-app>
No comments:
Post a Comment