Wednesday 27 April 2011

6) Simple Login project Using only JSP and SERVLET using sendRedirect method (This project is similar to project no. 4)

This project is quite similar to the  project no.4,  difference is that here we used only JSP unlike HTML used in project no.4, Here we demonstrated how to use sendRedirect method.  you need these files to run this project:



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

1)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <servlet-name>login servlet</servlet-name>
    <servlet-class>com.jexamples.web.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
     <servlet-name>login servlet</servlet-name>
     <url-pattern>/SelectForm.do</url-pattern>
</servlet-mapping>
  <welcome-file-list>
    <welcome-file>LForm.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2)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>

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>

5)LoginServlet.java

package com.jexamples.web;

import java.io.IOException;

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
response.sendRedirect("Success.jsp");  //dont use '/' here
/* sendRedirect
public void sendRedirect(java.lang.String location) throws java.io.IOException
Sends a temporary redirect response to the client using the specified redirect location URL. This method can 
accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending 
the response to the client. If the location is relative without a leading '/' the container interprets it as 
relative to the current request URI. If the location is relative with a leading '/' the container interprets
 it as relative to the servlet container root. 
If the response has already been committed, this method throws an IllegalStateException. After using this method,
 the response should be considered to be committed and should not be written to.

Parameters:
location - the redirect location URL
Throws: java.io.IOException - If an input or output exception occurs
  IllegalStateException - If the response was committed or if a partial URL is given and cannot be converted  into a valid URL
--------------------------------------------------------------------------------
*/
}
else
{
 response.sendRedirect("Failure.jsp"); //dont use'/' here
}
   //failure
   }
}

No comments:

Post a Comment