Simple Login Form With Servlet Forwarding
For Creating a login form with Servlet we have require some files
For fornt End:-index.html
For Forwarding control: Demo.java (Create as a servlet not a normal java file)
-----
index.html
---
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
@author ananddw
-->
<html><body>
<form action="welcome" method="get" align="center">
Enter your name<input type="text" name="name"> <br>
Enter you password<input type="password" name="password"> <br>
<input type="submit" value="login">
</form>
</html>
============= Demo.java================
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ananddw
*/
public class Demo extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String name=req.getParameter("name");
System.out.println("Name is "+name);
String password=req.getParameter("password");
System.out.println("Password is "+password);
pw.println("Welcome :-"+name +",with password:-"+password);
}
}
===============web.xml=======================
<web-app>
<servlet>
<servlet-name>anand</servlet-name>
<servlet-class>Demo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anand</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
============================================
Run this programme on server make sure that while creating a project you create dynamic web project else you will get Error .
For fornt End:-index.html
For Forwarding control: Demo.java (Create as a servlet not a normal java file)
-----
index.html
---
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
@author ananddw
-->
<html><body>
<form action="welcome" method="get" align="center">
Enter your name<input type="text" name="name"> <br>
Enter you password<input type="password" name="password"> <br>
<input type="submit" value="login">
</form>
</html>
============= Demo.java================
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ananddw
*/
public class Demo extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String name=req.getParameter("name");
System.out.println("Name is "+name);
String password=req.getParameter("password");
System.out.println("Password is "+password);
pw.println("Welcome :-"+name +",with password:-"+password);
}
}
===============web.xml=======================
<web-app>
<servlet>
<servlet-name>anand</servlet-name>
<servlet-class>Demo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anand</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
============================================
Run this programme on server make sure that while creating a project you create dynamic web project else you will get Error .
Comments
Post a Comment