Home > FAQs > Cookbook > Application, Session, Request objects in vm |
$req.session.servletContext.getAttribute(...) $req.session.getAttribute(...) $req.getAttribute(...)
To get parameters from the QueryString or from a POSTed form, do not use getAttribute, use:
$req.getParameter(...)
But that's quite obvious, since $req is the request object and we all know how it works.
Example:
_test.jsp_:
<html><head></head><body> <% session.setAttribute("sessionFoo", "sessionBar"); session.getServletContext().setAttribute("applicationFoo", "applicationBar"); %> <p>The following information should be available when sending the form below: <ul> <li>Request parameter 'querystringFoo' with value 'querystringBar';</li> <li>Request parameter 'formFoo' with value 'formBar';</li> <li>Session attribute 'sessionFoo' with value 'sessionBar';</li> <li>Application attribute 'applicationFoo' with value 'applicationBar'.</li> </ul> </p> <form action="test.vm?querystringFoo=querystringBar" method="post"> <input type="hidden" name="formFoo" value="formBar"> <p><input type="submit" value="Test!"></p> </form> </body></html>
_test.vm_:
<html><head></head><body> #set ($ses = $req.getSession()) #set ($app = $ses.getServletContext()) <p>applicationFoo = $!app.getAttribute("applicationFoo") <code>(app.getAttribute("applicationFoo"))</code></p> <p>sessionFoo = $!ses.getAttribute("sessionFoo") <code>(ses.getAttribute("sessionFoo"))</code></p> <p>formFoo = $!req.getParameter("formFoo") <code>(req.getParameter("formFoo"))</code></p> <p>querystringFoo = $!req.getParameter("querystringFoo") <code>(req.getParameter("queryStringFoo"))</code></p> </body></html>