<c: url> in JSTL
In servlet when we want to use the session firstly it is important to get a session object either by creating it or by or by using the existing one. If the browser doesn't support the cookies then the container will automatically use the URL rewriting. In servlets while the url rewriting we still have to tell the container to append the jsessionid in the end of the URL.
We can do the same thing by using the <c: url> core action tag. By using the <c: url> we are doing the URL rewriting but by using the <c: param> core action tag inside the <c: url> our requirement for URL rewriting and URL encoding get fulfilled.
In this example we are going to use <c: url> to construct a URL. Go through this example properly.
The code of the program is given below:
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> <html> <head> <title>Creating a URL</title> </head> <body> <form method="POST"> <center> <table border="1" > <tr> <td width="50%">Enter URL:</td> <td width="50%"> <input type="text" name="url" value="http://www.roseindia.net"/> </td> </tr> <tr> <td width="50%">Enter your name</td> <td width="50%"> <input type="text" name="name"/> </td> </tr> <tr> <td width="50%">Enter the password</td> <td width="50%"> <input type="password" name="pwd"/> </td> </tr> <tr> <td width="50%">Enter your email address</td> <td width="50%"> <input type="text" name="email"/> </td> </tr> <tr> <td width="100%" colspan="2"> <p align="center"> <input type="submit" value="Submit" name="submit" /> <input type="reset" value="Reset" name="reset" /> </p> </td> </tr> </table> </center> </form> <c:if test="${pageContext.request.method=='POST'}"> <hr> <c:url value="${param.url}" var="url"> <c:param name="name" value="${param.name}"/> <c:param name="pwd" value="${param.pwd}"/> <c:param name="email" value="${param.email}"/> </c:url> <br/><b>The resulting URL is:</b> <c:out value="${url}"/> </c:if> <hr> </body> </html> |
The output of the program is given below: