Sie sind auf Seite 1von 5

J2EE Interview Questions with Answer:

J2EE Interview question 1: What is the lifecycle of an J2EE component in an application? J2EE Interview answer 1: Lifecycle of J2EE Component deals with component creation, component assembly and application deployment. Component creation is like writing application classes like Servlet, EJB, Connections and deployment descriptors etc. Component assembly is to assemble or create Jar and War file and bundling all these components created in component creation cycle. Application deployment is to deploy or make the assembled file available in J2EE container, for these J2EE components to get active. J2EE Interview question 2: Is it advisable to have a class file present in a single Jar and or war file for once in a single EAR file? J2EE Interview answer 2: Yes, as J2EE application server may have two or more classloaders present like WAR class loader, EJB Jar classloader. Then if same class file is placed in war as well as Jar file separately, these two classloader will load two versions of same class file in two classloaders. And this could be potential classcast problem J2EE Interview Question 3: Suppose there is a requirement to use scheduler thread from the Servlet, and within JTA transaction transaction, will these scheduler threads run within scope of JTA transaction or not? J2EE Interview Answer 3: No, as per J2EE specification, if web component creates a thread, J2EE platform must ensure that the newly created thread is not associated/executed within this JTA transaction. J2EE Interview question 4: Suppose two EJB components running in two different J2EE container instances, will the transaction initiated by one EJB component, be propagated to another EJB component running in another J2EE container instance? J2EE Interview answer 4: No, one EJB component running in a J2EE Container instance cann't be able to propage transaction to another EJB component in another J2EE Container instance. J2EE Interview Question 5: Can Servlet filters and web application event listeners, start UserTransaction and use transaction outside doFilter method? J2EE Interview answer 5: No, Servlet filter and web application event listeners, should use transaction within doFilter method only (if anyone wants to use it). J2EE Interview Question 6: What is a major change in EJB3.0 over EJB2.0, that makes testing EJB components really easier?

J2EE Interview Answer 6: With anotations EJB3.0 session and entity beans are POJO (Plain Old Java Objects) and due to this it is ease to test POJO, as we can write JUnit testcases for session and entity beans and can run these testcases out of container environment. J2EE Interview Question 7: What are EJB anotations and how can we use those? J2EE Interview Answer 7: EJB anotations are many, some of the anotations I can think of are @Stateless, @Stateful, @Remote, @Local, @EJB etc. If any anotation is used in Session bean or Entity bean, it has to be in import section of the class file as well. If interested in using @Stateless in SessionBean, then this session bean has to import javax.ejb.Stateless class as well. And these bean can only be deployed onto EJB3.0 or JEE 5 compatible container/application server only. Servlet Question 8: Can a Java Thread be started from Servlet class, and what will be the implications? Servlet Answer 8: Yes, it is possible to write a Thread or start a Thread from Servlet. In fact a Servlet can be started on load of the web application by defining "load-on-startup" tag value as "1" in the "web.xml" file. One should not start/create a thread from a Servlet that is to be invoked by browser or Java client application. Reason is that web container has Threads running to delegate every client request to appropriate Servlet instance. If servlet instance starts a thread from every request, then it is like each thread spanning multiple threads. As HTTP is stateless protocol, and after response is sent back to client, job of servlet is over, and web container manages the lifecycle of the servlets, but not responsible for the lifecycle or management of the Threads those Servlets have created. If need is to to some kind of scheduling operation, then it is allows advisable to try for some good Scheduler frameworks available. Servlet interview Question 9: What is HTTP Session tracking and why is it important? Servlet interview Answer 9: HTTP Session tracking is the way Web server knows that the request is not a fresh or new request, and has a session already created in the web context. So when a request reaches web server, it looks for a variable called jsessionid, that should be associated with the request either in form of cookies, or URL rewriting. If your site visitor has blocked all cookies then redirect to another JSP or Servlet, will not be able to carry the same session, and all the data/object reference stored in HttpSession is/are lost. But is this redirect is done by using encodeURL method of HttpServletResponse object, then session id is attached as a part of URL and webserver attaches already created session to the new request. Servlet Question 10: What is session management, and how is it different from session tracking? Servlet Answer 10: HTTP session management is related to the mechanism, by which

application data or client state can be passed from one request to another request (As HTTP is stateless). Session Management is obviously comes after session tracking, as without session tracking, client request cannot be hooked onto one and only one session. But if requirement is to just pass reasonably small data/variables, from one request to another, then it can be done, by Hidden form field, and can be transported from browser to server, either by POST or GET as method in HTML FORM tag. Servlet Question 11: Can I use Hidden form field to track session? Servlet Answer 11: No, as Hidden form field sends its value as a request parameter, which is not same as URL rewriting (passing jsessionid as part of URL). Servlet Question 12: How can I pass method as request parameter to be called, instead of default doGet or doPost method of HttpServlet object, on submit of an URL? Servlet Answer 12: By overriding service method of HttpServlet object, and using reflection to invoke the desired method of the servlet that is passed as a request parameter. It is similar to the dispatcher mechanism implemented in Struts framework for dynamic dispatching of action. Servlet Question 13: In a architecture pattern like MVC, if it is mandatory that Servlet should be the controller, why not JSP? Servlet Answer 13: As JSP cannot be configured as a default mapping to be called based on a URL pattern in web.xml file. In that case, not all request goes through a JSP controller, and the very purpose of controller is not achieved. As Servlet can be mapped to a particular URL in web.xml like, for example: <servlet-mapping> <servletname>SampleController</servlet-name> <url-pattern>/*</url-pattern> </servletmapping> Servlet Question 14: Why JSP is used as View part of MVC ? Servlet Answer 14: As JSP is easily configurable at runtime and by using Tag library, XSL, and many more scripting language, productivity increases by using JSP instead of Servlet. Servlet Question 15: Can a database connection pool be instantiated in init method of servlet instead of server connection pool? What will be the problem? Servlet Answer 15: It is true that connection pool can be instantiated in init method of Servlet and is available for the web application to use, but generally there can be many web application required to use database connection pool, and multiple web applications do not have access beyond ServletContext. So it is desirable to have external resources defined at server level, not at an application level. Servlet Question 16: How to implement Servlet singlethread model, by not using

singlethreadmodel? JPA interview question 17: In Java Persistence API, as per Enterprise JavaBeans Specification version 3.0, What is an Entity class? JPA interview answer 17: In java Persistence API, Entity class is the Persistable class that can be mapped to a database table with a primary key defined in table column. JPA Java Persistence API Interview Question 18: Can Entity class in JPA Java Persistence API, be an interface or can an interface be acted like an Entity? JPA Java Persistence API Interview Answer 18: No, Entity class must be class only, not an interface, Entity class must not be declared final, and Entity class must be the top level class in an class hierarchy. Entity class must have a no-argument public or protected constructor.

JPA Java Persistence API interview question 19: Do Entity class supports inheritence? JPA Java Persistence API interview answer 19: Yes, Entity class supports inheritence and inheritence could have Entity class extending non entity class or non-Entity class can extend Entity class. JPA Java Persistence API interview question 20: Can an abstract class be acted like an Entity class? JPA Java Persistence API interview answer 20: Yes, abstract class can be an Entity class as well. JPA Java Persistence API interview question 21: How to define an abstract class or concrete class as JPA Entity class? JPA Java Persistence API interview answer 21: By placing annotations @Entity in the class level for the Entity class or by defining the Entity class in deployment descriptor.

How Spring and Hibernate with JPA enabled enterprise applications can be helpful to the business?
Some of the key points I'd say is cost (it's open source, which means no licensing fees), speed of application development (turnaround time from getting an idea to having a deliverable is much shorter compared to other frameworks), portability (you can move the application to any server and it will more likely than not just work out of the box), flexibility (making changes to business logic is quick and easy), maintainability (java has been around for a long time and probably will be around for much longer, finding

developers to work on the project in the future won't be that difficult as it is when using legacy code), stability (since the architecture is build upon a stable platform, downtime should not be an issue), scalability (if the userbase grows rapidly, moving to a cloud environment is straight forward, no extra cost changing the application) What is JPA ? JPA is the Java Persistence API, the entity persistence model for EJB 3.0 Standardized persistence framework which is implemented by Hibernate (or TopLink, Cayenne, etc.) JPA Annotations and persistence.xml provide vendor independent configuration EntityManager provides vendor independent access to persistence Replaces vendor specific query languages (HQL) with standard (JPQL)

Why JPA ? JPA is the standard, and standards are good! Using JPA does not tie you to Hibernate. JPA gives you most of the features of plain old Hibernate, except: No criteria queries in JPA 2.0. Criteria query is a neat feature of Hibernate that constructs query using Java-based combinators instead of alternate query language, getting the benefit of IntelliSense and Eclipses refactoring tools. JPA doesnt have Hibernates DeleteOrphan cascade type. Delete Orphan is a useful annotation that directs Hibernate to deletes entities in a collection if the parent is deleted, preventing orphaning. JPA doesnt have an equivalent to Hibernates ScrollableResults

Das könnte Ihnen auch gefallen