JSP User Name Password Login Example

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

<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

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.

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

HTML screen when User name and Password are entered.

ima

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

ima1

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

ima2

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 Reply

Your email address will not be published. Required fields are marked *