Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 111 |Likes:
Published by api-3748960

More info:

Published by: api-3748960 on Oct 15, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





1. How can I implement a thread-safe JSP page?
You can make your JSPs thread-safe by having them implement the
SingleThreadModel interface.
This is done by adding the directive

<%@ page isThreadSafe="false" %> 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.

2. How does JSP handle run-time exceptions?

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: <%@
page errorPage="error.jsp" %> 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: <%@ page
isErrorPage="true" %> 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.

3. What JSP lifecycle methods can I override?

You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy().

4. How can I override the jspInit() and jspDestroy() methods within a JSP

The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations: <%! public void jspInit() { . . .} %>

<%! public void jspDestroy() { . . . }%>
5. How do I include static files within a JSP page?

Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. The following example shows the syntax: <%@ include file="copyright.html" %>

Do note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.

6. How do I perform browser redirection from a JSP page?
You can use the response implicit object to redirect the browser to a different resource,
response.sendRedirect("http://www.foo.com/path/error.html"); You can also
physically alter the Location HTTP header attribute, as shown below:
String newLocn = "/newpath/index.html";
response.setHeader("Location",newLocn); %>
7. How does a servlet communicate with a JSP page?

The following code snippet shows how a servlet instantiates a bean and initializes it with FORM data posted by a browser. The bean is then placed into the request, and the call is then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream processing.

public void doPost (HttpServletRequest request, HttpServletResponse response) {
try { govi.FormBean f = new govi.FormBean();

String id = request.getParameter("id");
//use the id to compute additional bean properties like info maybe perform a db

query, etc....

("/jsp/Bean1.jsp").forward(request, response);
} catch (Exception ex) { . . .}
}The JSP page Bean1.jsp can then process fBean, after first extracting it from the

default request
scope via the useBean action.
<jsp:useBean id="fBean" class="govi.FormBean" scope="request"/>
<jsp:getProperty name="fBean" property="name" />
<jsp:getProperty name="fBean" property="addr" />
<jsp:getProperty name="fBean" property="age" />
<jsp:getProperty name="fBean" property="personalizationInfo" />

8. Show me an example of POST request chaining using JSPs and servlets.

The following code example demonstrates how request chaining can be implemented using a combination of JSPs and servlets. Consider the following JSP page, say Bean1.jsp, which essentially instantiates the bean fBean, places it in the request, and forwards the call to the servlet JSP2Servlet. Observe the way the bean is instantiated - here we automatically call the bean's setter methods for properties which match the names of the posted form elements, while passing the corrosponding values to the methods.

<jsp:useBean id="fBean" class="govi.FormBean" scope="request"/>
<jsp:setProperty name="fBean" property="*" />

<jsp:forward page="/servlet/JSP2Servlet" />
The servlet JSP2Servlet now extracts the bean passed to it from the request, makes
changes using the appropriate setters, and forwards the call to another JSP page
Bean2.jsp using a request dispatcher. Note that this servlet, acting as a controller, can also
place additional beans if necessary, within the request.

public void doPost (HttpServletRequest request, HttpServletResponse response) {
try {
FormBean f = (FormBean) request.getAttribute ("fBean");

// do whatever else necessary
forward(request, response);

} catch (Exception ex) { . . . }

The JSP page Bean2.jsp can now extract the bean fBean (and whatever other beans that
may have been passed by the controller servlet) from the request and extract its

Within JSP2
<jsp:useBean id="fBean" class="govi.FormBean" scope="request"/>
<jsp:getProperty name="fBean" property="name" />

9. there a way to reference the "this" variable within a JSP page?
Yes, there is. Under JSP 1.0, the page implicit object is equivalent to "this", and returns a
reference to the servlet generated by the JSP page.
10. How do I set a cookie within a JSP page?

Setting cookies from within a JSP page is similar to the way they are done within
servlets. Forexample, the following scriptlet sets a cookie "mycookie" at the client: <%
Cookie mycookie = new Cookie("aName","aValue");

Typically, cookies are set at the beginning of a JSP page, as they are sent out as part of
the HTTP headers.

11. How can I delete a cookie from within a JSP page?

A cookie, mycookie, can be deleted using the following scriptlet:
<% Cookie killMyCookie = new Cookie("mycookie", null);

12. Is there a way I can set the inactivity lease period on a per-session basis?

Typically, a default inactivity lease period for all sessions is set within your JSP engine admin screen or associated properties file. However, if your JSP engine supports the Servlet 2.1 API, you can manage the inactivity lease period on a per-session basis. This is done by invoking the HttpSession.setMaxInactiveInterval() method, right after the session


<% session.setMaxInactiveInterval(300);%>would reset the inactivity period for this session to 5 minutes. The inactivity interval is set in seconds.

13. Can I stop JSP execution while in the midst of processing a request?

Yes. Preemptive termination of request processing on an error condition is a good way to maximize the throughput of a high-volume JSP engine. The trick (asuming Java is your scripting language) is to use the return statement when you want to terminate further processing. For example, consider: <% if (request.getParameter("foo") != null) {

// generate some html or update bean property

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->