Wednesday 27 April 2011

7) Simple Login project Using JSP and SERVLET using forward 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 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.forward(request, response);
/*forward
public void forward(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException

Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This 
method allows one servlet to do preliminary processing of a request and another resource to generate the 
response. 
For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and
parameters adjusted to match the path of the target resource. 
forward should be called before the response has been committed to the client (before response body output has 
been flushed). If the response already has been committed, this method throws an IllegalStateException. 
Uncommitted output in the response buffer is automatically cleared before the forward. 

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 represents the request the client makes of the servlet
response - a ServletResponse object that represents the response the servlet returns to the client
Throws:
ServletException - if the target resource throws this exception
java.io.IOException - if the target resource throws this exception
IllegalStateException - if the response was already committed
--------------------------------------------------------------------------------

*/ 
}
else
RequestDispatcher view = request.getRequestDispatcher("Failure.jsp");
view.forward(request, response);
}
   //failure
   }
}


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