The use of get and post method in JSP and Servlet

The development of dynamic website is considered as technology because dynamic websites like online reservation and registration are very useful. By visiting a website user can do reservation in hotel, airline ticket, resort reservation and others. There is no need to go to the place to do reservation which is very uncomfortable especially if the place is far and you have a limited time.

In creating a reservation or registration form using JSP and creating the logic of a web application, it is important to understand the form method that is use like the get and the post method. When you create a form and the method is not specified, automatically the method to be implemented is the get method. The get method is the default method of a form. In using the get method it is important for a web developer to know that never use the get method when the parameter or data to be sent in the server is sensitive like the password and other sensitive information because it will be shown in the address bar of the browser. When using the get method in a form, you need also to implement the doGet() method in your servlet. The example will be provided below to support the concept.

Other method that can be use in creating a form using JSP is the get method. The get method is the most secured method and best to use when sending sensitive information to server. When using the get method, you need to implement the doPost() method in your servlet. Below is the example JSP and Servlet the use the get and post method.

Using the get method example:

frmLogin.jsp

<html>
<head>
<title>Login Form</title>
</head>
<body>
<h2>Login</h2>
<p>Please enter your username and password</p>
<form method="GET" action="/servlet/demo.LoginServlet">
<table>
<tr>
<td>
Username
</td>
<td>
<input type="text" name="username" size="20">
</td>
</tr>
<tr>
<td>
Password 
</td>
<td>
<input type="password" name="txtpassword" size="20">
</td>
</tr> 
<tr>
<td>
<input type="submit" value="Submit" name="B1">
</td>
</tr>
</table>
</form>
</body>

</html>

LoginServlet.java

package demo;
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("<html>");
out.println("<body>");
out.println("Hello: "  + name + “<br>” );
out.println("Your passwoed is:  " + pass + "<br>");
out.println("</body></html>");
}
}


Using the post method example:

frmLogin.jsp

<html>
<head>
<title>Login Form</title>
</head>
<body>
<h2>Login</h2>
<p>Please enter your username and password</p>
<form method="POST" action="/servlet/demo.LoginServlet">
<table>
<tr>
<td>
Username
</td>
<td>
<input type="text" name="username" size="20">
</td>
</tr>
<tr>
<td>
Password 
</td>
<td>
<input type="password" name="txtpassword" size="20">
</td>
</tr> 
<tr>
<td>
<input type="submit" value="Submit" name="B1">
</td>
</tr>
</table>
</form>
</body>
</html>


LoginServlet.java

package demo;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet{
public void doPost(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("<html>");
out.println("<body>");
out.println("Hello: "  + name + “<br>” );
out.println("Your passwoed is:  " + pass + "<br>");
out.println("</body></html>");
}
}