Loading
View RSS Feed

sachdev

Example Program to show the use of Servlet

Rate this Entry
login.html
Code:
<html>
<head>
<title>Example</title>
</head>
<body>
<h3>Please enter your userid and password to see your account statement:</
h3><p>
<form action="servlet/LoginServlet" method="POST">

Userid : <input type="text" name="userid"><br><br>
Password : <input type="password" name="password"><br><br>
<input type="submit" value="Show Statement">
</form>
</body>
</html>
LoginServlet.java
Code:
package chapter4;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet
{
Hashtable users = new Hashtable();
//This method will be called if somebody types the URL
//for this servlet in the address field of the browser.
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req, res);
}
//This method retrieves the userid and password, verifies them,
//and if valid, it forwards the request to AccountServlet.
//Otherwise, it forwards the request to the login page.
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String userid = req.getParameter("userid");
String password = req.getParameter("password");
if( userid != null && password != null &&
password.equals(users.get(userid)) )
{
req.setAttribute("userid", userid);

ServletContext ct = getServletContext();
RequestDispatcher rd =
ct.getRequestDispatcher("/servlet/AccountServlet");
rd.forward(req, res);
return;
}
else
{
RequestDispatcher rd =
req.getRequestDispatcher("../login.html");
rd.forward(req, res);
return;
}
}
//initialize some userids and passwords
public void init()
{
users.put("aks", "aks");
users.put("sach", "jaj");
users.put("sm", "mam");
}
}
AccountServlet.java

Code:
package chapter4;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AccountServlet extends HttpServlet
{
Hashtable data = new Hashtable();
//This method will be called if somebody types the URL
//for this servlet in the address field of the browser.
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws javax.servlet.ServletException, java.io.IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws javax.servlet.ServletException, java.io.IOException
{
String userid = (String) req.getAttribute("userid");
if(userid != null )
{
// Retrieve the data and generate the page dynamically.
String[] records = (String[]) data.get(userid);
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h3>Account Status for "+userid+"
at the start of previous three months...</h3><p>");
for(int i=0; i<records.length; i++)
{
pw.println(records[i]+"<br>");
}
pw.println("</body>");
pw.println("</html>");
}
else
{
//No user ID. Send login.html to the user.
//observe the use of relative path.
RequestDispatcher rd =
req.getRequestDispatcher("../login.html");
rd.forward(req, res);
}
}
//initialize some data.
public void init()
{
data.put("ann", new String[]{ "01/01/2002 : 1000.00",
"01/02/2002 : 1300.00", "01/03/2002 : 900.00"} );
data.put("john", new String[]{ "01/01/2002 : 4500.00",
"01/02/2002 : 2100.00", "01/03/2002 : 2600.00"} );
data.put("mark", new String[]{ "01/01/2002 : 7800.00",
"01/02/2002 : 5200.00", "01/03/2002 : 1900.00"} );
}
}

Submit "Example Program to show the use of Servlet" to Digg Submit "Example Program to show the use of Servlet" to del.icio.us Submit "Example Program to show the use of Servlet" to StumbleUpon Submit "Example Program to show the use of Servlet" to Google

Comments




Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.