welcome.html
[html]
<html>
<body>
<form action="./Splessons">
Username:<input type="text" name="username"/>
Email id:<input type="text" name="email"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
[/html]
Where just created two text boxes, one is for
Username and another is for
Email id and also created
submit button.
web.xml
[xml]
<web-app>
<servlet>
<servlet-name>ForwardServlet</servlet-name>
<servlet-class>ForwardServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DemoRequestDispatcher</servlet-name>
<servlet-class>DemoRequestDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForwardServlet</servlet-name>
<url-pattern>/goForwardServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DemoRequestDispatcher</servlet-name>
<url-pattern>/Splessons</url-pattern>
</servlet-mapping>
</web-app>
[/xml]
Make sure that
URL pattern should be match with
HTML form action URL and should be also same.
DemoRequestDispatcher.java
[java]
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoRequestDispatcher extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
RequestDispatcher requestDispatcher=request.getRequestDispatcher("goForwardServlet");
requestDispatcher.forward(request, response);
}
}
[/java]
Where
DemoRequestDispatcher is the class and that is extending the HtpServlet, The doGet() Method. A GET solicitation results from an ordinary solicitation for a URL or from a HTML web form that has no METHOD determined and it ought to be taken care of by doGet() technique.
setContentType("text/html") mean that response comes in text format or html format.
ForwardServlet.java
[java]
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 ForwardServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String email = request.getParameter("email");
out.println("
<h1>Thank you ::"+username+" From Splessons</h1>
");
out.println("
<h2>This is your Email ID::"+email+"</h2>
");
out.flush();
out.close();
}}
[/java]
Here the developer will pass the values dynamically by entering username and email,
flush will pump the accessible yield in the responsebuffer immediately to the program.
Output
Compile the code from HTML page and fill some data in the text box.
When click on submit button following result will be displayed.