Thursday, June 24, 2010

JSP CONTINUES ... 2

QUESTION: How do I mix JSP and SSI #include?
ANSWER: If you're just including raw HTML, use the #include directive as usual inside your .jsp file.



But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. Ronel Sumibcay (ronel@LIVESOFTWARE.COM) says: If your data.inc file contains jsp code you will have to use


The is used for including non-JSP files.

QUESTION: How can you store international / Unicode characters into a cookie?
ANSWER: One way is, before storing the cookie URLEncode it.

URLEnocder.encoder(str);

And use URLDecoder.decode(str) when you get the stored cookie.
 
QUESTION: What are the implicit objects?

ANSWER: Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:

  1. request
  2. response
  3. pageContext
  4. session
  5. application
  6. out
  7. config
  8. page
  9. exception

 QUESTION: Is JSP technology extensible?

 ANSWER: YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries

 
QUESTION: How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?

ANSWER: You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive

  within your JSP page.

With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine.

 
More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition.

SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use . You should try really hard to make them thread-safe the old fashioned way: by making them thread-safe

 
QUESTION: How does JSP handle run-time exceptions?

 ANSWER: You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example:

 

 
redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, if you indicate that it is an error-processing page, via the directive:

 

 

the Throwable object describing the exception may be accessed within the error page via the exception implicit object.

Note: You must always use a relative URL as the value for the errorPage attribute.

 
QUESTION: How do I prevent the output of my JSP or Servlet pages from being cached by the browser?

ANSWER: You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.

 

 
QUESTION: How do I use comments within a JSP page?

ANSWER: You can use "JSP-style" comments to selectively block out code while debugging or simply to comment your scriptlets. JSP comments are not visible at the client. For example:

 

 --%>

 You can also use HTML-style comments anywhere within your JSP page. These comments are visible at the client. For example:

 

 Of course, you can also use comments supported by your JSP scripting language within your scriptlets. For example, assuming Java is the scripting language, you can have:

 

 QUESTION: Response has already been commited error. What does it mean?

 ANSWER: This error show only when you try to redirect a page after you already have written something in your page. This happens because HTTP specification force the header to be set up before the lay out of the page can be shown (to make sure of how it should be displayed...content-type="text/html" or "text/xml" or "plain-text" or "image/jpg", etc...) When you try to send a redirect status (Number is line_status_402), your HTTP server cannot send it right now if it hasn't finished to set up the header. If not starter to set up the header, there are no problems, but if it 's already begin to set up the header, then your HTTP server expects these headers to be finished setting up and it cannot be the case if the stream of the page is not over... In this last case it's like you have a file started with some output (like testing your variables...) ... and before you indicate that the file is over (and before the size of the page can be setted up in the header), you try to send a redirect status... It s simply impossible due to the specification of HTTP 1.0 and 1.1

 

QUESTION: How do I use a scriptlet to initialize a newly instantiated bean?

ANSWER: A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.

The following example shows the "today" property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the jsp:setProperty action.