This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Friday, 11 October 2019

Login Form with validation in java

Login Form with validation in java:

In this tutorial we will discuss how to create a simple login form in java by using Servlet conecpt in java. we will send the data on the server as well  by using servlet only.

Components:

In  order to create a  login form we need to create following files.
1. login.java
2. login .java
2.web.xml

IDE:

We will use eclipse IDE :

1.Login.html File

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="./IndexServlet">
UserName: <input type="text" name="uname"><br><br>
Password: <input type="password" name="pswd"><br><br>
Email-Id: <input type="text" name="em"><br><br>
<select>
<option value="male">male</option>
<option value="female">female</option>
<option value="other">other</option>

</select>
<br><br>
<input type="submit" value="submit">
</form>
</body>
</html>


2.Login.java File


package com.epert;

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 IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    
    public IndexServlet() {
        // TODO Auto-generated constructor stub
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.println("<h1 style='color:red'>This is ourlogin form</h1>");
pw.println("<br>");
String uname=request.getParameter("uname");
String pswd=request.getParameter("pswd");
String em=request.getParameter("em");
if(uname.equals("viren")&&pswd.equals("sharma")&&em.equals("viren@gmail.com"))
{
pw.println("login successfully");
pw.println("<br>");
pw.println("username:"+uname);
pw.println("<br>");
pw.println("password:"+pswd);
}
else
{
pw.println("login faild");
}

}


3.Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>expirement</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>

  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>IndexServlet</display-name>
    <servlet-name>IndexServlet</servlet-name>
    <servlet-class>com.epert.IndexServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>IndexServlet</servlet-name>
    <url-pattern>/IndexServlet</url-pattern>
  </servlet-mapping>

</web-app>


Start the server and access Our application:







Adbox