JSP User Name Password Login Example

In this first example, client sends user name and password to JSP on the Server. Server receives, validates and sends back the validation result to client as response.

JSP User Name Password Login Example

Following is the client-side HTML file calling Validation.jsp

File Name: UserPass.html

Enter User Name
Enter Password

<form method="get" action="http://localhost:8888/india/Validation.jsp">

In the action attribute Validation.jsp file is given to invoke. Assume localhost is the server. The port number on which Tomcat is running is 8888. india is the folder created in Tomcat. The attribute method can be given two values of either get or post; here, get is given.

Following is the server-side JSP file

File Name: Validation.jsp

Validating User Name and Password

<% String str1=request.getParameter("t1"); String str2=request.getParameter("t2"); if(str1.equalsIgnoreCase("snrao") && str2.equals("java")) { out.println("

Thankyou, you are VALID

"); } else { out.println("

Sorry, you are INVALID

"); } %>

String str1=request.getParameter("t1");
String str2=request.getParameter("t2");

t1 and t2 are the HTML field names in the client HTML file, UserPass.html. request is the JSP implicit object of HttpServletRequest.

out.println("<h3>Thankyou, you are VALID</h3>");

out is an implicit object of JspWriter (like PrintWriter of Servlet), a subclass of Writer.

out object sends the response to client.

<% some code %> is known as Scriptlet. Any code placed within Scriptlet is dynamic code.

Scriptlet is a JSP coding element. Maximum dynamic content exists in scriptlet only. Other scripting elements are Expression and Declaration.

Following is the Servlet version for the above JSP file. It is just for comparison.

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

public class Validation extends HttpServlet
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    PrintWriter pw = res.getWriter( );
    String str1 = req.getParameter("t1");
    String str2 = req.getParameter("t2");
    if(str1.equalsIgnoreCase("snrao") && str2.equals("java"))

Thankyou, you are VALID

"); } else { pw.println("

Sorry, you are INVALID

"); } pw.close( ); } }

See the length of coding between a Servlet and JSP doing the same job.

HTML screen when User name and Password are entered.


Screenshot of JSP response when correct User name and Password are entered.


Screenshot of JSP response when wrong User name and Password are entered.


Execution of above JSP in Tomcat

Copy Validation.jsp to india folder. Client program can remain in client folder itself. Start Tomcat. Open browser. Open the client program UserPass.html through File Menu. Enter user name and password and click submit button. You will get result.

Leave a Comment

Your email address will not be published.