Day 5

Quiz

A1:

The different types of tags that can be used in a JSP page are directive, declaration, expression, scriptlet, and custom tags.

A2:

A directive tag to set the MIME type for sending a GIF image file is

 <%@ page contentType="image/gif">  

A3:

The include directive tag should be used in this case. Here is an example:

 <%@ include file="/myhost/myapp/mySecondJSP.jsp" >  

A4:

To define global variables in a JSP, that is, variables outside the service() method, the declaration tag should be used. An example of this is given here:

 <%!  // java variables instantiated here String myStringVar = new String(); int intVar = 0; %> 

A5:

The simplest tag to use for displaying values in a JSP is the expression tag. An example of this is

 Value of the variable is <%=myStringVar>.  

A6:

The scriptlet tag should be used to write Java statements in a JSP. The java statements written in scriptlet tags are placed in the service() method of the generated servlet. An example of this is

 <%  // write java statements here %> 

A7:

The different implicit variables available in a JSP are request, response, out, session, pageContext, application, config, and page.

Exercises

A1:

The following code demonstrates the technique to send an image file on the browser. After importing the required files, the response type is set to "image/jpeg". The file is then read into a BufferedInputStream object. The size of the file is obtained using the available() method of BufferedInputStream class. This BufferedInputStream object is then read into a byte array. The OutputStream object is then obtained, and the byte array is written onto this OutputStreamObject. The response object is then sent back to the browser.

 <html>  <head> <title>Display Image File</title> </head> <body bgcolor=#FFFFFF> <center>    <strong><font size=3 face='arial' color=#336600>       <H1>Image File Display</H1>    </font></strong> </center> <p> <%@ page import=" java.io.*, java.sql.*, javax.servlet.*, javax.servlet.http.*, "%> <%     String IMAGE_FILE_LOCATION = "C:\\Book1Image.jpg";     //setting content type     try{         response.setContentType("image/jpeg");         FileInputStream file = new FileInputStream(IMAGE_FILE_LOCATION);         BufferedInputStream bin = new BufferedInputStream(file);         int fSize = bin.available();         byte[] buff = new byte[fSize];         bin.read(buff, 0, fSize);         bin.close();         OutputStream myOut = response.getOutputStream();         int count=0;         while(count < fSize)             myOut.write(buff[count++]); }     catch(IOException ioe){         out.println("File cannot be read.");     }     catch(Exception e){         out.println("General error.");     } %> </body> </html> 

A2:

The getMethod() method of the HttpServletRequest class is used to detect the kind of request. The following code illustrates the use of this method to obtain the request type:

 <html>    <head>      <title>Detect request method</title>   </head>   <body bgcolor=#FFFFFF>   <center>      <strong><font size=3 face='arial' color=#336600>           <H1>Detect Request Method</H1>      </font></strong>   </center>   <p>   <%@ page import="   java.io.*,   java.sql.*,   javax.servlet.*,   javax.servlet.http.*,   "%>   <%      String reqType = request.getMethod();      if(reqType.equals("GET")){           out.println("This is a get request.");      }      else if(reqType.equals("POST")){           out.println("This is a post request.");      }   %>   </body> </html> 



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