rd.forward (-,-) | res.sendRedirect (-,-) |
• Performs forward rnode of servlet chaining. | • Performs sendRedirection mode of communication. |
• The source servlet program communicates with destination web resource program directly. | • The source servlet program communicates with destination servlet program by having network round trip with browser window. |
• The source servlet program and destination web resource program use the same request and response objects so the request data coming to the source servlet program is visible and accessible in destination web resource program. | • The source servlet program and destination web resource program will not use the same request and response object so the request data coming to the source servlet program is not visible and accessible in destination program. |
• The source servlet program can use the request attribute to send additional data to destination program.d | • The source servlet program and destination program can be there in the same web application (or) in two different application of the same server (or) different server. |
• The destination program can be a servlet (or) JSP (or) HTML program. | • The destination sendRedirect operation the URL in the browser address bar will be changed. |
• Suitable when the source servlet program and destination program resides in the two different web application. | • Suitable when the source servlet program and destination servlet program resides in the same web application. |
Example on sendRedirect ()
Servlet program redirects the request to non-java web resource programs of websites hosted on the Internet.
The applications in the next page acts as a hub for working with multiple search engines.
The above application the servlet searchSrv redirects the request to search engine based on the search engine that is chosen in the form page.
To work with above application gather the web resource program names and
request parameter names of different search engines as shown below:
For google
http://www.google.co.in/search?q=new+seven+wonders
For Yahoo
http://search.yahoo.com/search?q=new+seven+wonders
For Bing
http://search.bing.com/search?q=new+seven+wonders
Develop the above web application
Step 1: Prepare the deployment directory structure of web application.Step 2: Develop the source code of above servlet program or web application.
Search.html
<form action= “surl” method= “get”>
<center>
<font color= ‘red’ size= ‘6’>
<marquee behavior= ‘alternate’> SERCH ENGINE </marquee> <br> <br>
SEARCH :<INPUT TYPE=“TEXT” height=‘45’SIZE=“50” NAME=“turl”> <br>
<input type= “submit” name= “tsubmit” value= “search”> <br> <br>
GOOGLE: <input type= “radio” name= “tradio” value= “google” CHECKED />
YAHOO: <input type= “radio” name= “tradio” value= “yahoo/>
BING: <input type= “radio” name= “tradio” value= “bing”/>
</font>
</center>
</form>
SearchApp.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SearchApp extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throwsServletException,IOException {
//general Setting
res.setContentType(“text/html);
PrintWriter pw=res.getWriter();
//read form data
String s=req.getParameter(“turl”);
String radio=req.getParameter(“tradio”);
if(radio.equals(“google”)) {
res.sendRedirect(“https://www.google.co.in/search?q=”+s);
}
elseif {
res.sendRedirect(“https://www.yahoo.com/search?q=”+s);
}
else
res.sendRedirect(“https://www.bing.com/search?q=”+s);
}//end of doGet ()
}
web.xml
<web-app>
<servlet>
<servlet-name> abc </servlet-name>
<servlet-class>SearchApp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> abc </servlet-name>
<url-pattern> /surl </url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file> home.html </welcome-file>
</welcome-file-list>
</web-app>
Step 3: Compile the source files of all servlet programs.
Step 4: Configure all four servlet programs in web.xml file having four different URL patterns.
Step 5: Start the server (Tomcat).
Step 6: Deploy the web application and Copy SearchApp folder to Tomcat_home\webapps folder.
Step 7: Test the web application.Open browser window type this url: https://ecomputernotes.com:2020/SearchApp/Search.html.
After clicking search button b, open web site in Google Search engine.