welcome.html
[html]
<html>
<form action="./welcome">
<h2>Enter your tutorial name<input type="text"name="name/">
<input type="submit"value="search">
</form>
</html>
[/html]
Here created a text box to search the required page and created
submit button to give the input to the server.
web.xml
[xml]
<servlet>
<servlet-name>DemoSendRedirect</servlet-name>
<servlet-class>sendredirect.SimpleSendRedirect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoSendRedirect</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome.html</welcome-file>
</welcome-file-list>
</web-app>
[/xml]
As discussed
servlet name should be same and
URL should be match with HTML form.
DemoSendRedirect.java
[java]
package sendredirect;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleSendRedirect extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String tutorialName = request.getParameter("name");
response.sendRedirect("http://www.splessons.com/lesson/"+tutorialName+"+tutorial/");
out.flush();
out.close();
}
}
[/java]
The
doGet() method is utilized to send parameters to an URL along with the header information. The
sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file.
Output:
When compiled the program following output will be displayed.
When clicked on search button required page will be displayed.
Difference between doGet() and doPost()
- In doGet() parameters are sent along with header information where as in doPost() parameters are sent in the body.
- The doGet() will have limitation to send the data where as no limitation for doPost() .
- In doGet() parameters are not encrypted where as in doPost() parameters are encrypted.
- The doGet() method is utilized to get some information from the server and the doPost() is utilized to post some information to the server.