Wednesday, February 11, 2009

Getting Form Information from PageContext in JSP Step 61

If you need to access form information without using a tag in your jsp page, you can do that by using the pageContext.

For Example, in your include-taglibs.jspf file, you can add this import.

%@ page import="com.form.UserForm"%>

In your UserForm class, you should add a test method:


public String getTest()
{
return "This is a test";
}


In your addbook.jsp page, you can add this code:

<%Object o=pageContext.getAttribute("userForm",PageContext.SESSION_SCOPE);
UserForm userForm=(UserForm)o;
System.out.println("testing: " userForm.getTest()); %>


This code will pull the userForm and call the method getTest. This can come in handy when a tag will not fulfill your needs.

You should make sure you use the proper scope for your application:
PageContext.PAGE_SCOPE
PageContext.REQUEST_SCOPE
PageContext.SESSION_SCOPE
PageContext.APPLICATION_SCOPE
Go To Step 62 Conclusion

2 comments:

Rashi February 19, 2010 11:37 AM  

<%Object o=pageContext.getAttribute("userForm",PageContext.SESSION_SCOPE);%>
I'm using this but getting an error saying "pageContext not found". Could you help?

Greg February 19, 2010 11:46 AM  

Try importing your Struts tag libraries:

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>