Thursday, December 20, 2012

RequestDispatcher Interface and SendRedirect() Method


RequestDispatcher Interface 

public interface RequestDispatcher
 
Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. 


public RequestDispatcher getRequestDispatcher(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. 
  
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException
 
Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes. 

login.java
 ResultSet rs=st.executeQuery("select * from login where Uname='"+s1+"'and Upass='"+s2+"'");  
    if(rs.next())  
        {  
         RequestDispatcher rd=request.getRequestDispatcher("validUser");  
         rd.include(request, response);  
       }  
    else  
     {  
         out.print("invalid user");  
      RequestDispatcher rd=request.getRequestDispatcher("Register.jsp");  
     rd.include(request, response);  
    }  


SendRedirect() Method

public void sendRedirect(String location) throws IOException

Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.

login.java
 ResultSet rs=st.executeQuery("select * from login where Uname='"+s1+"'and Upass='"+s2+"'");  
    if(rs.next())  
        {  
         response.sendRedirect("validUser");  
       }  
    else  
     {  
         out.print("invalid user");  
         response.sendRedirect("Register.jsp");
    }  

web.xml
  <?xml version="1.0" encoding="UTF-8"?>  
   <servlet>  
     <servlet-name>login</servlet-name>  
     <servlet-class>login</servlet-class>  
   </servlet>    
   <servlet>  
     <servlet-name>validUser</servlet-name>  
     <servlet-class>validUser</servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>login</servlet-name>  
     <url-pattern>/login</url-pattern>  
   </servlet-mapping>  
   <servlet-mapping>  
     <servlet-name>validUser</servlet-name>  
     <url-pattern>/validUser</url-pattern>  
   </servlet-mapping>  
   <session-config>  
     <session-timeout>  
       30  
     </session-timeout>  
   </session-config>  
   <welcome-file-list>  
     <welcome-file>index.jsp</welcome-file>  
   </welcome-file-list>  
 </web-app>  


Download Code Link 1
Download Code Link 2

No comments:

Popular Posts