Monday, 9 February 2015

Best of Servlet Thread Safety:-

Servlet Concurrency:-

for more details look in below website:-
http://tutorials.jenkov.com/java-servlets/servlet-concurrency.html

Detailed Thread-Safety Questions concerning Java Servlets

  1. Your understanding is correct. Actually thread is created for each request. So HttpServletRequest and HttpServletResponse are local to thread. So no sharing of these two. You do not need to synchronize them. HttpServletRequestWrapper and HttpServletResponseWrapper are the classes which provide a convenient implementation of the HttpServletRequest and HttpServletResponse interfaces respectively. You do not need to worry about them in synchronization context.
  2. Definitely you have to provide synchronization for ServletContext object returned by getServletContext() because it is shared between servlets in your application.
  3. As HttpServletRequest is local to thread created by container to serve the request. So cookies are also local to that thread and those are not shared. So no need to provide synchronization to cookies.
Few concepts :
  1. Servlets are always singlton in your application. i.e. only one object is created.
  2. For each request a different thread is created(actually obtained from Thread Pool). Following things are local to thread : HttpServletRequest, HttpServletResponse
Reference:-

Is HttpSession is ThreadSafe:-

Multiple servlets executing request threads may have active access to the same session object at the same time. The container must ensure that manipulation of internal data structures representing the session attributes is performed in a threadsafe manner. The Developer has the responsibility for threadsafe access to the attribute objects themselves. This will protect the attribute collection inside the HttpSession object from concurrent access, eliminating the opportunity for an application to cause that collection to become corrupted.
This is safe:
// guaranteed by the spec to be safe
request.getSession().setAttribute("foo", 1);
This is not safe:
HttpSession session = request.getSession();
Integer n = (Integer) session.getAttribute("foo");
// not thread safe
// another thread might be have got stale value between get and set
session.setAttribute("foo", (n == null) ? 1 : n + 1);
This is not guaranteed to be safe:
// no guarantee that same instance will be returned,
// nor that session will lock on "this"
HttpSession session = request.getSession();
synchronized (session) {
  Integer n = (Integer) session.getAttribute("foo");
  session.setAttribute("foo", (n == null) ? 1 : n + 1);
}
I have seen this last approach advocated (including in J2EE books), but it is not guaranteed to work by the Servlet specification. You could use the session ID to create a mutex, but there must be a better approach.
Reference:-

How do servlets work? Instantiation, session variables and multithreading

please read the comment from stackoverflow:-

When the servletcontainer (like Apache Tomcat) starts up, it will deploy and load all webapplications. When a webapplication get loaded, the servletcontainer will create the ServletContext once and keep in server's memory. The webapp's web.xml will be parsed and every Servlet, Filter and Listener found in web.xml or annotated with respectively @WebServlet, @WebFilter and @WebListener will be created once and kept in server's memory as well. When the servletcontainer shuts down, it will unload all webapplications and the ServletContext and all Servlet, Filter and Listener instances will be trashed.
http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909

-------------------------------------------------------
http://stackoverflow.com/questions/2183974/difference-between-each-instance-of-servlet-and-each-thread-of-servlet-in-servle/2184147#2184147

http://www.javaworld.com/article/2072798/java-web-development/write-thread-safe-servlets.html

Sunday, 8 February 2015

What is the importance of "Charsequence" in java


Java CharSequence is an interface. As the API says, CharSequence has been implemented in CharBuffer, Segment, String, StringBuffer, StringBuilder classes. So if you want to access or accept your API from all these classes thenCharSequence is your choice. If not then String is very good for a public API because it is very easy & everybody knows about it. Remember CharSequence only gives you 4 method, so if you are accepting a CharSequence object through a method, then your input manipulation ability will be limited.
-----------------------------------------------------------------------------------------------------
It's just re factored from any existing implementations. One of the benefits is that you can "widen" the input whenever you actually only need one of its methods.
So instead of for example
public void printEveryChar(String string) {
    for (int i = 0; i < string.length(); i++) {
        System.out.println(string.charAt(i));
    }
}
you can have
public void printEveryChar(CharSequence charSequence) {
    for (int i = 0; i < charSequence.length(); i++) {
        System.out.println(charSequence.charAt(i));
    }
}
so that you can pass String, CharBuffer, StringBuilder, StringBuffer and other CharSequence implementations in.
This fact has however nothing to do with java.util.Regex, it only takes benefit of it =)
--------------------------------------------------------------------------------------------------
for more read:-
stackoverflow:-

Thursday, 22 January 2015

Java Servlet Session Management , Cookies, HttpSession and URL Rewriting


Wednesday, 21 January 2015

What is the difference between MySQl and Oracle and Which is better

MySQL is open source, and it's still pretty stable.
ORACLE isn't..As it is payble

MySQL=Free unless you use it in a commercial environment. MySQL is great for lightweight applications.
In some places there are missing features.When I looked at it a few years ago I thought that things
like ROLLBACK were missing but they appear to have been added now.

Oracle=very expensive but should be able to run the most demanding application.

but the ,major difference:-

Oracle is the leading data base managment system released by oracle corporation where as
Mysql sponsored by a company called MySql AB now acquired by Oracle corporation

http://www.rapidprogramming.com/questions-answers/What-is-the-difference-between-MySQL-and-Oracle--617