Wednesday 27 April 2011

8) Simple Login project Using JSP and SERVLET using include method of RequestDispatcher

To run this project we need following files:

1)LForm.jsp
2)LoginServlet.java
3)Success.jsp
4)Failure.jsp



1)LForm.jsp



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Login Form Page</title>


  </head>
  
  <body>
   <h1>Please Login</h1>
   <hr>
   
   <form method="get" name="getForm" action="SelectForm.do">
   <!-- Never put '/' here like action="/SelectForm.do" 
        or u will get error HTTP Status 404 - /SelectForm.do
        The requested resource (/SelectForm.do) is not available.
   -->
      <table bgcolor="skyblue" >
        <tr>
          <td colspan="2"> Please provide correct UserId and Password</td>
        </tr>
        <tr>  
          <td>UserName</td>
          <td><input type="text" name="Uname" value="" /></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><input type="text" name="Pword" value="" /></td>
        </tr>  
        </table>
        <input type="submit" name="submitData" value="SUBMIT DATA" />
   </form>
  </body>
</html>

2)LoginServlet.java



package com.jexamples.web;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginServlet extends HttpServlet
{


private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();

String un = request.getParameter("Uname");
String pw = request.getParameter("Pword");
if( ("javaj2eepro".equals(un)) && ("jj").equals(pw))
{
//success
RequestDispatcher view = request.getRequestDispatcher("Success.jsp");
/* getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)


Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. 
A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a 
response. The resource can be dynamic or static. 
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the 
path begins with a "/" it is interpreted as relative to the current context root. This method returns null if 
the servlet container cannot return a RequestDispatcher. 


The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method
 can take a relative path.


Parameters:
path - a String specifying the pathname to the resource. If it is relative, it must be relative against the 
       current servlet.
Returns: a RequestDispatcher object that acts as a wrapper for the resource at the specified path, or null if 
         the servlet container cannot return a RequestDispatcher
*/
view.include(request, response);
out.println("<h1> <font color='blue'>Congrats..!!!</font><br></h1>");

/*include
include
public void include(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException
Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method 
enables programmatic server-side includes. 
The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included
servlet cannot change the response status code or set headers; any attempt to make a change is ignored. 


The request and response parameters must be either the same objects as were passed to the calling servlet's 
service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.


Parameters:
request - a ServletRequest object that contains the client's request
response - a ServletResponse object that contains the servlet's response
Throws:
ServletException - if the included resource throws this exception
java.io.IOException - if the included resource throws this exception


*/ 
}
else

//failure
RequestDispatcher view = request.getRequestDispatcher("Failure.jsp");
view.include(request, response);
out.println("<h1> <font color='red'>Failure..!!!</font><br></h1>");
}
   
   }
}
3)Success.jsp



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Success.jsp' starting page</title>
    
  </head>
  
  <body>
    Login Sucessfull...! <br>
  </body>
</html>

4)Failure.jsp



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'Failure.jsp' starting page</title>
    
  </head>
  
  <body>
    Login Failed...! <br>
  </body>
</html>

No comments:

Post a Comment