WTAD Solutions 2

WTAD Solutions 2 (11-20)


WTAD Solutions 2 Q-11 How a constructor can be used to initialize user defined objects in JavaScript?

  • Constructor is a method that have a same name as class name.
  • Constructor is used to initialize the data member of the object.



<script language=”text/html”>

var person = new Object();
person.firstName = “John”;
person.lastName = “Doe”;
person.age = 50;
person.eyeColor = “blue”;



WTAD Solutions 2 Q-12 What is the difference between getHeaderNames and getHeaders method of HttpServletRequest?


  • GetHeaderNames()Returns an enumeration of all the header names this request contains. If the request has no headers, this method returns an empty enumeration.
  • Some servlet containers do not allow servlets to access headers using this method, in which case this method returns null


  • Returns all the values of the specified request header as an Enumeration of String objects.
  • Some headers, such as Accept-Language can be sent by clients as several headers each with a different value rather than sending the header as a comma separated list.


WTAD Solutions 2 13.Describe the two situation in which one might use URLs that refer to one’s own site?

Situation 1=> Relative Reference:

  • In this Situation you might refer the address of other pages or images using relative reference with current directoy.
  • Eg: <img src=”../images/main.png”> </img>
  • Above example will load image named as “main.png” from the images directory, Images directory should be parent directory, where you have writtin above code.

Situation 2=>Absolute Reference:

  • In this Situation you might refer the address of other pages or images using absolute reference means your address url will consist of complete url of the image or page.
  • Eg: <img src=”www.google.com/images/main.png”> </img>
  • Above example will load image named as “main.png” from the images directory of google.com


WTAD Solutions 2 14) Explain the various types of inputs provided by HTML.

Ans. 1.Form




WTAD Solutions 2 15) What is meant by Referer header? What is the value of request.getHeader(“Referer”), if the user simply types the address of a page in the browser?

Ans. The Referer header designates the location of the page users were on when they clicked a link to get to the current page.

If users simply type the address of a page,the browser sends no Referer at all and request.getHeader(“Referer”) returns null.


Q-16: Write the lines which creates cookie with userID E00123 and sets the maximum age to 15 days.What is the interpretation if maximum age is 0?

Cookie userCookie = new Cookie(“userID”, “E00123”); userCookie.setMaxAge(60*60*24*15); // Store cookie for 15 Day response.addCookie(userCookie);


Maximum age 0 means cookie will be session cookie and will expire once you close the browser.


Q-17 Write specific steps required by the servlet to generate JPEG images.

First, let us summarize the two main steps servlets have to perform to build multimedia content.

  1. Inform the browser of the content type they are sending.To accomplish this task, servlets set the Content-Type response header by using the setContentType method ofHttpServletResponse .
  2. Send the output in the appropriate format.This format varies among document types, of course, but in most cases you send binary data, not strings as you do with HTML documents. Consequently, servlets will usually get the raw output stream by using the getOutputStreammethod, rather than getting a PrintWriter by using getWriter

The specific steps required to generate JPEG images.

  1. Create aBufferedImage .

You create a java.awt.image.BufferedImage object by calling the BufferedImage constructor with a width, a height, and an image representation type as defined by one of the constants in theBufferedImage class.

int width = …;

int height = …;

BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

  1. Draw into the BufferedImage .

You accomplish this task by calling the image’s getGraphics method, casting the resultantGraphics object to Graphics2D , then making use of Java 2D’s rich set of drawing operations, coordinate transformations, font settings, and fill patterns to perform the drawing.

Graphics2D g2d = (Graphics2D)image.getGraphics();            g2d.set Xxx (…);            g2d.fill( someShape );            g2d.draw( someShape );

  1. Set the Content-Type response header.

As already discussed, you use the setContentType method of HttpServletResponse for this task. The MIME type for JPEG images is image/jpeg . Thus, the code is as follows .


  1. Get an output stream.

As discussed previously, if you are sending binary data, you should call                                            the getOutputStreammethod of HttpServletResponse

rather than the getWriter method.    For instance:

OutputStream out = response.getOutputStream();

  1. Send the BufferedImage in JPEG format to the output stream.


Before JDK 1.4, accomplishing this task yourself required quite a bit of work. So, most people used a third-party utility for this purpose. In JDK 1.4 and later, however, the ImageIO class greatly simplifies this task.

For example:



ImageIO.write(image, “jpg”, out);

} catch(IOException ioe)


println(“Error writing JPEG file: ” + ioe);



Q-18: What is the significance of 302 and 404 status code returned by HttpServletRespose? Which methods can be used to set this?

302 Found:

Similar to 301, except that the new URL should be interpreted as a temporary replacement, not a permanent one. Very useful header, since browsers automatically follow the link to the new URL. This status code is so useful that there is a special method for it, sendRedirect.  Using response.sendRedirect(url) has a couple of advantages over doingresponse.setStatus(response.SC_MOVED_TEMPORARILY) and response.setHeader(“Location”, url). First, it is easier. Second, with sendRedirect, the servlet automatically builds a page containing the link (to show to older browsers that don’t automatically follow redirects). Finally, sendRedirect can handle relative URLs, automatically translating them to absolute ones.

404 NotFound:

No resource could be found at that address. This is the standard “no such page” response. This is such a common and useful response that there is a special method for it inHttpServletResponse: sendError(message). The advantage of sendError over setStatus is that, with sendError, the server automatically generates an error page showing the error message.


Q-19 Explain use of “referer” header with reference to request headers with suitable example .


The Referer header designates the location of the page users were    on when they

clicked a link to get to the current page.

If users simply type the address of a page,the browser sends no Referer at all and request.getHeader(“Referer”) returns null.

 Example:  String referer = request.getHeader(“Referer”);

if (referer == null)


referer = “<I>none</I>”;


String title = “Referring page: ” + referer;

String imageName;

if (contains(referer, “JRun”))


imageName = “jrun-powered.gif”;


else if (contains(referer, “Resin”))


imageName = “resin-powered.gif”;

} else


imageName = “tomcat-powered.gif”;



Q-20 Explain use of setStatus() with suitable example .

The setStatus method takes an int (the status code) as an argument, but instead of using explicit numbers, for readability and to avoid typos, use the con- stants defined in HttpServletResponse. The name of each constant is derived from the standard HTTP 1.1 message for each constant, all upper case with a prefix of SC (for Status Code) and spaces changed to underscores


String p = req.getParameter(“p”);
if (p == null)

res.sendError(res.SC_NOT_FOUND, “Requested page {” + p
+ “} not found.”);




Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative