Here is the second part of the Q&A post. You can view the Part 1 from here.
Q17)What are advantages of using JSP?
-Performance is significantly better bacause JSP allows embedding dynamic elements in HTML pages itself.Q18)Is JSP technology extensible?
-JSP is always compiled before it's processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.
-JSP is built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC,JNDI,EJB,JAXP.
-JSP pages can be used in combination with Servlets that handle the business logic.
Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.Q19)How do you define application wide error page in JSP?
Error handling is an important subject in JSP. You can define two kinds of error pages in Java web application. One is using tag <error-page> in web.xml and the other is by using error page JSP which uses isErrorpage to declare that this jsp page can be used as error page. Other JSP uses that page by using attribute errorpage="error.jsp". Whenever you get an unhandled exception in JSP, request will be routed to error page.Q20)What are implicit objects?
Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. (request, response, pageContext, session, application, out, config, page, exception)Q21)What are the scopes available in <jsp:useBean>?
-Page scope: object is available for the entire JSP page but not outside the page.Q22)How many tags are provided in JSTL?
-Request scope:object is associated with a particular request and exists as long as the request exits.
-Application scope:object is available throughout the entire web application but not outside the application.
-Session scope:object is available throughout the session with a particular client.
There are 5 types of JSTL tags.(tags.core tags, sql tags, xml tags, internationalisation tags, function tags)Q23)What is the difference between PrintWriter and ServletOutputStream?
PrintWriter is a character-stream class whereas ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.Q24)What is the difference between RequestDispatcher.forward() and RequestDispatcher.include()?
The javax.servlet.RequestDispatcher interface has two methods of include() and forward(). Processing done on the server when both methods are called is completely not visible to client.
-include(): calling this method, we ask the web container to include the response of servlet 2(or a jsp,html file) in the response of servlet 1. As you can see above, response of servlet 2 is included in the response of servlet 1 that is being sent to the client. The included servlet(servlet 2) cannot change the response status code or set headers; any attempt to make a change is ignored. In include() method, servlet 1 gives the control to servlet 2 temporarily. After executing servlet 2 control comes back to servlet 1 again and the response of servlet 2 is included the response of servlet 1. This method works slower than the forward() method. It is better to use this method when static information is to be included. Client receives the response from the same servlet which he requested.
-forward():calling this method, servlet 1 passes the full control to the servlet 2(or a jsp,html file) to generate a response to the client. The control never returns to servlet 1 again. It is permanent shifting. The client will only see the response that comes from servlet 2.As you see above, response of second servlet is sent to the client. This method allows one servlet to do processing of a request and another resource to generate the response. After executing servlet 2, control returned to client. This method workes relatively faster than include() method. It is better to use this method when dynamic information is to be included. It can be used where a Servlet plays the role of a controller to process the client input and deciding what response page is to be returned. Client actually receives the response from a different servlet (not known to client).Q25)What is the difference between request.getRequestDispatcher.forward(ServletRequest request, ServletResponse response) and response.sendRedirect(String url)?
forward():
-Client doesn't know that he is getting response from a different Servlet as the URL will not change in client’s browser.
-Control changes to another Servlet on the same server without client being informed.
-Everything happens on server side within the Web container and client is not involved. Because of this, it works faster. This method works only within server.
-forward() sends the same request to another resource of the same Web application.
-Used when processing is done by another Servlet.
-Original request, response objects and additional data set with setAttribute() method(if any) are trasfered to another resource.
-If you would like to forward the client request to a new resource on the same server for further process, prefer forward() where data of the original resource can be passed to the new resource.
-Session is not lost in both cases.
sendRedirect():Q26)What is the differencein using request.getRequestDispatcher() and context.getRequestDispatcher()?
-Client can know easily as the URL (from where he is getting response) changes in the client browser’s prompt.
-Control changes to client.
-sendRedirect() causes the Web container to return to the client’s browser. Client inturn can redirect to different servers or domains. It works slower than forward(). This method works within and outside the server.
-Sends a new request because it uses the URL of the browser. By using sendRedirect() you can forward the request to any web application either in the same server or to the another one.
-Used when wanted to redirect the client request to another Web site (completely out of current context) or to redirect the errors to another resource.
-As client initiates a new request, the original request and response objects are lost and fresh ones are to be created.
-If you would like to transfer the control to a new server or domain where client treats as a new task, prefer sendRedirect(). If the data of the original resource (which client requested) is needed in the new resource, store them in Session object and reuse.
-Session is not lost in both cases.
-request.getRequestDispatcher(path): we need to give the relative path of the resource.Hope to see you in the next blog post.
-context.getRequestDispatcher(path): we need to give the absolute path of the resource.
Selen
No comments:
Post a Comment