Day 3

Quiz

A1:

A Java servlet can be initialized in two ways: programmatically and declaratively. In programmatic initialization, the instance variables of the servlet are initialized with the init() method. The declarative method of initialization uses the web.xml file to define the initialization parameters and their values.

A2:

The three methods of an HttpServlet that can be overridden are service(), doGet(), and doPost().

A3:

To retrieve multiple values in a servlet, use the getParameterValues() method of the HttpServletRequest class. The getParamaterValues() method returns an array of String values that represent the multiple values selected in the HTML form.

A4:

Session handling in servlets can be done in four ways: hidden variables, URL rewriting, cookies, and the HttpSession API.

A5:

The following settings are required to register a servlet in the deployment descriptor file web.xml:

 <servlet>         <servlet-name>BookShoppingServlet</servlet-name>        <servlet-classes>com.sams.learningweblogic7.servlets</servlet-                 classes> </servlet> 

A6:

Use the encodeURL() method of the HttpServletResponse class to enable encoding of the data sent from the servlet to the browser. In this way, the WebLogic Server seamlessly handles encoding the data sent by the servlet and appending the session identifier information.

A7:

To retrieve data or objects stored in a session using the HttpSession API, use the getAttribute() method.

A8:

Session duration is a feature specific to the WebLogic Server. Hence, the following settings need to be specified in the weblogic.xml file:

 <session-descriptor>      <session-param>     <session-param>         <param-name>TimeOutSecs</param-name>         <param-value>3600</param-value>     </session-param> </session-descriptor> 

The valid values are between 1 and 3600.

Exercises

A1:

package com.sams.learnweblogic7.servlets.exercises;

 import javax.servlet.*;  import javax.servlet.http.*; import weblogic.servlet.security.*; import java.util.*; import java.io.*; import java.sql.*; public class DisplayServletInfo extends HttpServlet {     static final String LOG_FILE_NAME = "ServletInfoLog.txt";     public void doGet(HttpServletRequest req, HttpServletResponse res)             throws ServletException, IOException{         doPost(req, res);     }     public void doPost(HttpServletRequest req, HttpServletResponse res)         throws ServletException, IOException{         res.setContentType("text/html");         PrintWriter out;         out = res.getWriter();         log(req);         out.println("Please read your "+LOG_FILE_NAME+"                 for the details of this servlet access.");     }//end of doPost     public void log(ServletRequest req) throws IOException     {         try {             RandomAccessFile file = new RandomAccessFile(LOG_FILE_NAME , "rw" );             file.seek(file.length());             file.writeBytes("Remote Host name : "+req.getRemoteHost()+"\n");             file.writeBytes("Remote Host Address : "+req.getRemoteAddr()+"\n");             file.writeBytes("Protocol : "+req.getProtocol()+"\n");             file.writeBytes("Content Type : "+req.getContentType()+"\n");             file.writeBytes("Content Length : "+req.getContentLength()+"\n");             file.writeBytes("Scheme : "+req.getScheme()+"\n");             file.writeBytes("Server Name : "+req.getServerName()+"\n");             file.writeBytes("Server Port : "+req.getServerPort()+"\n");             file.close();         } catch ( IOException io )         {             System.out.println("Error occurred while writing to a log file");             throw io;         }     } } 

Register your servlet in the web.xml using the following tags:

 <servlet>  <servlet-name>DisplayServletInfo</servlet-name> <servlet-class>       coms.sams/learnweblogic7.servlets.exercises.DisplayServletInfo </servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayServletInfo</servlet-name> <url-pattern>/DisplayServletInfo/*</url-pattern> </servlet-mapping> 

A2:

package com.sams.learnweblogic7.servlets.exercises;

 import javax.servlet.*;  import javax.servlet.http.*; import weblogic.servlet.security.*; import java.util.*; import java.io.*; public class DisplayServiceMethod extends HttpServlet {     public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{         res.setContentType("text/html");         if((req.getMethod()).equalsIgnoreCase("GET"))         {             doGet(req,res);         }         else if ((req.getMethod()).equalsIgnoreCase("GET"))         {             doPost(req,res);         }     }     public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{         PrintWriter getOut = res.getWriter();         getOut.println("GET REQUEST CALLED");     }     public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{         PrintWriter postOut = res.getWriter();         postOut.println("POST REQUEST CALLED");     }//end of doPost } <servlet> <servlet-name>DisplayServiceMethod</servlet-name> <servlet-class>      com.sams.learnweblogic7.servlets.exercises.DisplayServiceMethod </servlet-class> </servlet> 

A3:

To deploy a servlet without building the Java archive file, you need to copy the ShoppingApp directory into the application directory of your server. This is the exploded format. Hence, for Exercise 2 the directory structure would be

 ShoppingApp (inside application directory)      |_ WEB-INF and images directories         |_classes(dir), lib(dir), web.xml and weblogic.xml         |_com.sams.learnweblogic7.servlets.examples.DisplayServiceMethod.java 

When you save the ShoppingApp directory, you can access it in exactly the same way. You have to save the directory only after compiling the classes.



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