What is difference between stateless and stateful session beans?


In this post, we will see the differences b/w stateless and stateful session beans.

Stateless Session Bean:

  • Stateless session bean do not maintain state across method calls and transactions
    The EJB server transparently reuses instances of the Bean to service different clients at the per-method level (access to the session bean is serialized and is 1 client per session bean per method.
  • Used mainly to provide a pool of beans to handle frequent but brief requests. The EJB server transparently reuses instances of the bean to service different clients.
  • Do not retain client information from one method invocation to the next. So many require the client to maintain on the client side which can mean more complex client code.
  • Client passes needed information as parameters to the business methods. Performance can be improved due to fewer connections across the network.

Stateful Session Bean :

  • A stateful session bean holds the client session’s state.
  • It is an extension of the client that creates it.
  • Its fields contain a conversational state on behalf of the session object’s client.
    This state describes the conversation represented by a specific client/session object pair.
  • lifetime of these are controlled by the client and cannot be shared between clients.

Can the same object os Stateless Session bean be shared by multiple clients? Can the same object os Stateful Session bean be shared by multiple clients?


It is possible to share the same object of stateless session bean by multiple clients.
A Server maintains a pool of objects in case of stateless session bean. And every When ever we send a request to the JSP file it checks the servlet representing the JSP file. Then the web container runs the jsp compiler that  generates the servlet code
time for a request it uses one of the pool of objects which is free at that time. The server gets back that object in to the pool only after the client work on that object is Directory over [ client says that by invoking ejbremove() method]

After the client release that object, the server may used the same object to handle another client request, thus the same object to handle another client request. Thus the same object of stateless session can be shared by multiple clients But sharing the same object for multiple clients is not possible in case of state full session bean. Here server should maintain an object for every clients to handle / maintain the state of that object.

Difference between JSP & Servlets?


Both are web applications used to produce web content that mean dynamic web pages. Bot are used to take the requests and service the clients. The main difference between them is, In servlets both the presentation and business logic are place it together. Where as in jsp both are separated by defining by java beans .
In jsp’s the overall code is modulated so the developer who doesn’t know about java can write jsp pages by simply knowing the additional tages and class names.
One more important to be considered is servlet take less time to compile. Jsp is a tool to simplify the process and make the process automate.

<jsp:useBean> tag and it’s properties?


To separate the code from the presentation it would be good idea to encapsulate the code in java object instantiate it and use it in our jsp. This instance is variable is called ‘id’.
We can also specific life time of this object by using ‘scope’ attribute
<jsp:useBean id=’name’ scope=”scope” class=”beanclass”/>

Properties:

id: a case sensitive name is used.
Scope: The scope with in which the reference is available. page, request,session, application are possible values
page is default

class: the fully qualified classes name
bean name: The name of the java bean class
type: This type must be super class of the beans classes

JSP Action Tags, Directive Tags & Scripting Tags?


Action Tag: These tags effect the run time behavior of the jsp page and the response
set back to client tags are :

<jsp: usebean> instantiate a new jsp bean class
< jsp: setProperty>: setting the properties at run time
<jsp:param>:used to sending parameters
<jsp:include>include a resource at run time
<jsp:forward>forward the control to specific resource
<jsp:plugin>:to plugin a web resource url applet

Directives:
These tags effect the overall structure of the servlet that result from translation. 3 types of directives are defiend.
Page directive: used to define and manipulate the no of page dependent attributes that effect the whole jsp.
<%@page…….%>
include directive: This instructs the container to include the content of the resource in the current jsp
.<%include file=”—–”/>
this file is parsed once, when translation.
Taglib directive: To use tag extensions to define custom tags.

Scriptlets: There are used to include java code in jsp page these are of 3 types
scriptlet: used to write pure java code
<%———-%>
declarations: used to declare java variables and methods
<%!int num=3;
public void hello(String str)
{
return str;
}
%>
Expressions:
This send the value of a java expression back to client. These results is converted to string and displayed at client.

JSP implicit Objects


The objects which are internal to JSP page are called implicit objects. These are available with jsp page once the jsp invoked.

1)out: used to send the content to the browser
ex: out.println(“xx”)
2)Response: Represent HttpServlet Reques
3) Response: Represent HttpServletResponse
4) Page Context: used to access the objects like session, servlet context, Request, Response etc…
5) Session: Points to HttpSession objects
6) Application: Points to servlet context objects
7)Page: Points to current servlet objects
8) Exception: Points to Exception

What is key diffrence between using a <jsp:forward> and HttpServletResponse.sendRedirect()?


The <jsp:forward> action allows the request to be forwarded to another resource (servlet/jsp/HTMl> which are available in the same web-application (context).
Where as in the case of Response.sendRedirect() the request to be forwarded/redirected to another resource in the same web-application or some other web-application in the same server, or to a resource on some other web-server. The key difference is by using <jsp:forward> single request from a browser can process multiple resources. Where as by using SendRedirect() multiple requests take place in order to process multiple resources.

Why DB connections are not written directly in JSP’s?


The main purpose of JSP is to separate the business logic & presentation logic. DB connections deal with pure java code. If write this kind of code in JSP, servlet container converts the JSP to servlet using JSP compiler which is the time consuming process so this is better to write code which deals with DB connections, Authentication and Authorization etc. in the java bean and use the <jsp:bean> tag to perform the action