Day 4

Quiz

A1:

Servlet chaining can be performed two ways, using the include() and forward() methods of the RequestDispatcher interface.

A2:

To write a filter for a servlet, you must override the doFilter(), getFilterConfig(), and setFilterConfig() methods of the Filter interface. The actual functionality of the filter is written in the doFilter() method. After you write and compile the filter, it must be associated with a servlet. You do so in the web.xml deployment descriptor file.

A3:

ServletContext events such as creation, termination, adding an attribute, removing an attribute, and replacing an attribute can be intercepted by a listener of a ServletContext.

A4:

Creation, termination, adding an attribute, removing an attribute, and replacing an attribute are events of the HttpSession object that can be intercepted using a listener.

A5:

Listeners must always be defined before servlets in the deployment descriptor file.

Exercises

A1:

Add the following method to the filter DemonstrateFilters.java to perform the logging functionality:

 public synchronized static void log(ServletRequest req) throws          IOException {     try     {         RandomAccessFile file = new RandomAccessFile                 (LOG_FILE_NAME , "rw" );     file.seek(file.length());     Enumeration reqEnum = req.getParameterNames();     while(reqEnum.hasMoreElements())     {         String paramName = (String)reqEnum.nextElement();         String paramValue = req.getParameter(paramName);         String displayString = paramName + ":" + paramValue+"\n";         file.writeBytes(displayString);     }     file.close();     } catch ( IOException io )     {         System.out.println                 ("Error occurred while writing to a log file");         throw io;     } } 

In the DemonstrateFilters.java file, add the following line where the instance variables of the servlet are declared:

 final static String LOG_FILE_NAME = "LogFile.txt";  

The log file will be created in the directory where the WebLogic Server was started.

A2:

It is possible to write such a program. This code can be used to forward to an HTML file:

 RequestDispatcher rd = sc.getRequestDispatcher  ("/forwardedHtml.html");rd.forward(req, res); 

Note that static HTML cannot show any information retrieved from this request object. When might you require a servlet to forward a request to a static HTML file? For example, an online shopping Web site for white papers might require first-time users to register. After the user has registered and the servlet has saved the user information in a database, the user would be shown a static HTML page containing a list of links to all the existing white papers.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net