Answer the following questions. Each question carry equal marks:
1 Part 1
In what type of applications/scenarios forward () and include () methods are used?(Marks 5)
forward() and include() methods
Include method is used when we want to include some external files
in our Servlet (eg: html, javascripts). If you want to show some
advertisements in your sevlet code you can include the avertisement page
as an image or html file.
We use forward method to navigate the request and response object to another resouce on the same server.
suppose we want to assign a jsp to display the contents we can navigate
from the servlet to jsp using forward method. here servlet is reponsible
for data processing and jsp is for display (MVC pattern follows this)
2 Part 2
Suppose you are going to develop a learning management system in which different users access the system like student, teachers and admin. . They have different views
(interfaces) based on their privileges. Their login request is sent
to some authentication page which authenticates the users on the basis
of their provided information from database. If they
are authenticated successfully they are shown the respective view
otherwise they are sent back to the login page.
You are using templates for views (header, footer, navigation, etc) that are already developed by some other student. You also have to add some “news and information” portion which your university has subscribed to RSS readers and get daily news headlines.
Identify the parts of this application where you will use include,
forward and redirect methods to implement the required features. If
you are selecting or preferring any method over the other, give its
short reason.(Marks 5)
solution
Response.sendRedirect () This function is used, when we want to
redirect the client request to some other site (i.e out of our context)
or when ever we want to redirect errors. If you are using
sendRedirect (), then it will be visible to the client that means the
URL which you have been redirected will be visible in the address bar.
Redirect response to the client using the specified redirect location
URL.
RequestDispatcher Interface:
Forward() : This can be done in two ways by Request &
ServeletContext. Forwarding a request from a servlet to another resource
(servlet, JSP file, or HTML file) on the server. Forward is done at
server side, without the client’s knowledge. When you invoke a forward
request, the request is sent to another resource on the server, without
the client being informed that a different resource is going to process the request. This process occurs completely with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser
issues a completely new request any object that are stored as request
attributes before the redirect occurs will be lost. This extra round
trip a redirect is slower than forward.