Sunday, November 24, 2019

Difference Betwixt Jsp:Include Too Jsp:Forward Action- Example

JSP provides measure actions to create things without using Java within the scriptlet. Two of such measure actions, which help JSP to interact alongside other server resources e.g. approximately other JSP, Servlet or HTML files are, include too frontward actions. The <jsp:forward> activeness enables yous to frontward an HTTP asking to a static HTML file, a servlet, or approximately other JSP. It has an attribute called page, which accepts the URL of approximately other resources equally shown below:

<jsp:forward page="URL" /> 


Let's tell yous direct maintain 2 JSP files, hello.jsp too header.jsp inwards your Java spider web application too if yous desire to forward an HTTP request from hello.jsp to header.jsp, yous tin add together next lines into hello.jsp:

<jsp:forward page="header.jsp"></jsp:forward>

The path to the resources is relative, equally asking itself direct maintain cognition of electrical flow URL.  Btw, what is the departure betwixt include too frontward activeness is ane of the mutual JSP interview questions, too if yous are preparing for Java or JEE developer interviews too hence yous should live aware of many such oft asked questions. One agency to laid upward good is to refer a expert mass similar Java Programming Interviews exposed, which contains questions too answers from all of import Java topics too frameworks, including Servlet too JSP.

 JSP provides measure actions to create things without using Java within the scriptlet Difference betwixt jsp:include too jsp:forward action- Example



Worth noting is that the JSP that contains the <jsp:forward> activeness stops processing, clears its buffer, too forwards the asking to the target resource. Note that the calling JSP should non write anything to the response prior to the <jsp:forward> action. Anything written would acquire away too non seem inwards lastly response which comes from the header.jsp now.  Remember, include activeness is unlike than include directive, which too includes response from approximately other resources merely at translation time, non at the asking time.

You tin too exceed additional parameters to the target resources using the <jsp:param> tag.



<jsp:forward page="footer.jsp" >     <jsp:param name="copyright" value="2016" />     <jsp:param name="credit" value="java" />  </jsp:forward> 

In this example, the footer.jsp tin access the value of copyright too credit parameter past times calling the request.getParameter("copyright").

On the other paw <jsp:include> activeness is used to charge the content of approximately other JSP, Servlet or HTML file into electrical flow JSP. <jsp:include> too execute the code too forcefulness a level of the buffer inwards the output stream.
If a.jsp has the code like

<jsp:include page="header.jsp" flush="true" >    <jsp:param name="key" value="value" />  </jsp:include> 

too hence the header.jsp execute too the output is placed inwards the a.jsp

Here is a amount listing of supported measure activeness past times JSP, yous tin too read Head First JSP too Servlet to acquire to a greater extent than almost Java Server pages:

 JSP provides measure actions to create things without using Java within the scriptlet Difference betwixt jsp:include too jsp:forward action- Example


That's all almost difference betwixt <jsp:include> too <jsp:forward> activeness inwards JSP. In curt include action is used to include contents of approximately other Servlet, JSP or HTML files, piece the forward action is used to frontward the electrical flow HTTP asking to approximately other Servlet or JSP for farther processing. They are precisely similar to include() too forward() method of RequestDispatcher too Servlet.

Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets too JDBC for Beginners: Build a Database App

No comments:

Post a Comment