Using JSP forward XML Tag

The following example illustrates using <jsp:forward> tag. This tag is used to forward the current jsp page to another page (any page jsp or html). Just like redirect. But here, you can transfer the data from one jsp page to another jsp or servlet page by setting the request attributes. Here is the syntax

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

Here page is a mandatory attribute

Folder structure

webapps  
	|_ jsp5  
		|_ index.jsp  
		|_ sample.jsp

index.jsp

<html>
    <body>
    This is in the html body<br/>
    <jsp:scriptlet>
    request.setAttribute("sample","this is sample text......");
    </jsp:scriptlet>
    <jsp:forward page="sample.jsp"/>
    </body>
</html>

Here, using the setAttribute() we have set an attribute to the corresponding request. The request object here is carried to the sample.jsp file because of the <jsp:forward> tag. The request here is forwarded.

sample.jsp

<%
    out.println(request.getAttribute("sample"));
%>

ధన్యవాదాలు (Thanks)

No comments: