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 has two methods:
1. include method
2. forward method
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.
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.
login.java
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
web.xml
Download Code Link 1
Download Code Link 2
More Servlet Topics :
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. RequestDispatcher has two methods:
1. include method
2. forward method
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.
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.
login.java
ResultSet rs=st.executeQuery("select * from login where Uname='"+s1+"'and Upass='"+s2+"'");
if(rs.next())
{
RequestDispatcher rd=request.getRequestDispatcher("validUser");
rd.forward(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
More Servlet Topics :
- Introduction : Servlet Interface
- Java Package javax.servlet(Interfaces,Classes and Methods)
- Java Package javax.servlet.http (Interfaces,Classes and Methods)
- How to Create a Web Application using NetBeans and Apache Tomcat Server
- Basic Program using HttpServlet Class
- Basic Login Program using JSP and GenericSevlet class
- Login using JDBC,Servlet with NetBeans and Oracle
- SevletConfig Interface using JDBC,NetBeans 6.9 and Oracle 10g
- Validation on TextField using JavaScript and Print command using servlet on NetBeans
- ServletContext Interface
- RequestDispatcher Interface and SendRedirect() Method
- Session Tracking
- Session Tracking using Cookies
- Session Tracking using HttpSession Interface
- Session Tracking using URL Rewriting
- Session Tracking using Hidden Forms Field
- Sql Query Tool using Servlet
No comments:
Post a Comment