<c:import>

This is used for importing the response of other pages in the current page response at request processing time i.e. dynamic include.

Form1:

<c:import url = "second.jsp"/>

It can be absolute or relative path.

First.jsp:

<h1> this is first jsp </h1>
<c:import url = "second.jsp"/>

Second.jsp:

<h1> this is second jsp

output:

this is first jsp
this is second jsp.

Form2:

<c:import url= "/second.jsp" context = "/webapp2"/>

We can import the resources form outside of current application also i.e cross context import is also possible.

Example:

<h1> this is first jsp </h1>
<c:import url="/test.jsp" context = "jsp2"/>

Test.jsp in other web app (jsp2)
<h1> this is test </h1>

Error:

Unable to get request dispatcher for context:"/jsp2″ and URL:”/test.jsp” verify values and/ or enable cross context access.

Note:

Most of the web servers won’t provide support for cross context communication by default to meet security constrains and server level configuration changes are required to provide support for this.

Form3:

<c:import url= "second.jsp" var = "x"/>

We can store the result of imported page into a variable specified by var attribute. In the rest of the page where ever the same response is required we can use directly without importing once again.

Example:

<h1> this is first jsp </h1>
<c:import url="second.jsp" var = "x"/>
${x}
${x}
${x}

Second.jsp:

<h1> this is second jsp </h1>

o/p : this is first jsp
this is second jsp
this is second jsp
this is second jsp

Form4:

The more convenient way to store the result of <c:import> is to use reader object it is alternamtive to var attribute.

Example:

<c:import url ="second.jsp" var reader = "myreader"/>
Int i = r.read ();
While (i! = -1){
// use i based on or requirement
i = r.read();
}
%>

Example:

While performing import we can send parameters also to the target jsp.For this we have to use <c:param> tag.

First.jsp:

<h1> welcome to DSS </h1>
<c:import url = "second.jsp>
<c:param name ="x" value = "scjp"/>
</c:import>

Second.jsp:

The offered courses are : ${param.x} </h1>

<c:redirect>

We can use this tag to redirect the request from one jsp to another jsp. But strictly speaking this tag is internally implemented for forward purpose.

Form1:

<c:redirect url = "second.jsp"/>

url value can be Absolute or relative

Form2:

We can redirect the request to some other web application resource also (i.e cross context communication).

<c:redirect url = "/second.jsp" context = "/webapp2"/>

Form3:

While forwarding the request we can pass parameters also to the target resource by using <c:param> tag.

</c:redirect>
<c:redirect url="second.jsp">
<c:param name = "c" value = "scjp">
</c:redirect>

Example:

<c:redirect url = "second.jsp">
<c:param = "x" value = "c"/>
</c:redirect>

<c:url>

We can use this standard action rewrite url’s to appended session information & form parameters.

Form1:

<c:url value = "second.jsp" var = "x" scope = "session"/>

Form2:

<c:url value = "/second.jsp" context = "/webapp2" var = "x" scope = "request"/>

Form3:

<c:url value = "second.jsp" var = "x" scope = "session">
<c:param name = "c1" value = "scjp"/>
<c:param name = "c2" value = "SCWCD"/>
</c:url>

Example:

<h1> welcome to DSS </h1>
<c:url value = "second.jsp" var = "x" scope ="session">
<c:param name = "c1" value = "scjp"/>
<c:param name ="c2" value = "SCWCD"/>
</c:url>

The encoded url is : ${x}
<a href = "${x}"> click here to go for second.jsp</a>

Second.jsp:

<h1> the offered courses are:
${param.c1} and ${param.c2} <h1>

Summery of url related tags:

Tag Description Attributes
<c:import> For importing the response of Other pages at request processing time url,context,var,varReader,scope
<c:redirect> To redirect the request to other web components url,context
<c:url> To rewrite url for appending session information and from parameters value,var,scope,context
<c:param> To send parameters while forwarding and including name,value