To get the desired result firstly we have to make one html form which will have only one field named as name in which we will enter the name. And we will also have one submit button, on pressing the submit button the request will go to the server and the result will be displayed to us.
In the servlet which will work as a controller here picks the value from the html page by using the method getParameter(). The output will be displayed to you by the object of the PrintWriter class.
The code of the program is given below:
Login
Please enter your username and password
LoginServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
String pass = request.getParameter("password");
out.println("");
out.println("");
out.println("Thanks Mr." + " " + name + " " + "for visiting roseindia
" );
out.println("Now you can see your password : " + " " + pass + "
");
out.println("");
}
}
web.xml file for this program:
The output of the program is given below: