91-9990449935 0120-4256464 |
ServletContextAware Interface ExampleThe ServletContextAware interface must be implemented by the Action class to store the information in the application scope. It contains only one method setServletContext. Syntax: Usage of ServletContextAware interfaceThere can be a lot of real usage of ServletContextAware interface. They are:
Example of ServletContextAware interfaceIn thie example, we are creating two links first page and second page. If you direct click on the second page, it will not show any data but if you have clicked on the first link, data will be stored in the ServletContext object and it can be obtained from another action class. In the second link, we are getting data stored in the ServletContext object. In this example, we are need following pages
1) Create index.jsp for inputThis jsp page creates two links, first link invokes first action class and second link invokes second action class. index.jsp2) Define action and result in struts.xmlThis xml file defines one package and 2 actions. struts.xml3) Create the action class to store dataThis action class implements the ServletContextAware interface and overrides the setServletContext method to store the information in the application scope. FirstAction.java4) Create the action class to get dataThis class gets the information from the application scope, if any information is found in the session scope with login name, it returns success otherwise false. SecondAction.java5) Create view componentsThere are many view components:
This page creates the login form. secondsuccess.jspThis page prints the welcome message with the username. seconderror.jspThis page displays the error message.
Next TopicStruts2 With i18n Example Tutorial
|