Wednesday, October 24, 2007

JSP Interview questions-part6


Question: How can my JSP page communicate with an EJB Session Bean?

Answer: The following is a code snippet that demonstrates how a JSP page can interact with an EJB session bean:

<%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %>

<%!//declare a "global" reference to an instance of the home interface of the session bean

AccountHome accHome=null;

public void jspInit() {

//obtain an instance of the home interface

InitialContext cntxt = new InitialContext( );

Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB");

accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class); }%>

<% //instantiate the session bean

Account acct = accHome.create();

//invoke the remote methods

acct.doWhatever(...);

// etc etc... %>

Question: How can my application get to know when a HttpSession is removed?

Answer: Define a Class HttpSessionNotifier which implements HttpSessionBindingListener and implement the functionality what you need in valueUnbound() method.

Create an instance of that class and put that instance in HttpSession.

Question: How do I include static files within a JSP page?

Answer: 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: 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.

Question: What is a output comment?

Answer: A comment that is sent to the client in the viewable page source.The JSP engine handles an output comment as uninterpreted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser.

JSP Syntax

Example 1

Displays in the page source:

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com