Search code examples
javajspsessionstruts

General session handling in java


I work with multiple languages like PHP, C, JAVA etc. I am trying to fully understand how session is implemented in java.

Question : In php I can create a session and use the session in any class even utility classes. In Java why session is meant to be handled normally only in servlet classes? why not in any basic POJO classes without actually passing session or context object ? (I know other classes can, using complex techniques, but why not easily as servlet or as in other languages ).

I do know that the correct way to do things is use session in Servlet to retrieve object/values and pass object/values to other utility classes and get results.

Please let me know if there is in fact an easy way to get session in basic POJO classes.


Solution

  • I had flagged to delete this question as I found the answer in another SO link Retrieving Web Session from a POJO Outside the Web Container . Since it has not been deleted till now , I am answering my own question.

    What are servlet and why we use them ?

    A Java servlet is a Java program that extends the capabilities of a server. Although servlets can respond to any types of requests, they most commonly implement applications hosted on Web servers. Such Web servlets are the Java counterpart to other dynamic Web content technologies such as PHP and ASP.NET.

    More information about java servlet at the link https://en.wikipedia.org/wiki/Java_servlet

    Servlet vs POJO/normal Java classes ( why session access only in servlet ?)

    A servlet class too is a java class. Servlet is a java class which comply servlet specification, so that it can be run in a server. Java class not required to comply Java Servlet API

    A servlet container (e.g. Tomcat) is designed to be able to recognise servlet classes and, if configured correctly, treat them in a particular way. There's nothing magical about this - you can always write your own application that can treat any type of class in a specific way - but there's a standard that all servlet containers follow that means you can write a servlet class and know how it will be used.

    Java was designed to be a platform independent language to create software to be embedded in various consumer electronic devices. Soon it was apparent that the Java can be used in client/server programming environment because of its inherent portability. Hence servlet and applet were introduced in java. Java applets helped create dynamic, self-executing program on client side. Java servlet is a small program that executes on the server. Just as applets dynamically extend the functionality of a web browser, servlets dynamically extend the functionality of a web server.

    Since servlet part of java is used in server side programming , this has access to session. POJO classes are also used in server side programming but code which uses session is usually written in servlet.

    POJO Vs PHP classes(server side)

    Java is used in many development environments like desktop,mobile,embedded and Web. PHP is a server-side scripting language designed for web development.It can also be used for general purpose programming but I am limiting the discussion to class used in php in server side programming(ie .php pages). So it is better to compare php classes in php pages with servlet/JSP - server side development part of JAVA.

    Again servlet puts html code inside Java code. JSP puts java code inside html code. So JSP in java is more comparable to PHP . If we understand this then we can infer that there is no good reason to compare POJO in java to PHP classes (server side). It is like comparing applies to oranges. if you are interested in comparison between php and jsp please visit http://www.withoutbook.com/DifferenceBetweenSubjects.php?subId1=57&subId2=2&d=Difference%20between%20PHP%20and%20JSP

    In modern web development we use template engines ie instead of JSP we use servlet with template engine(Freemarker,Velocity). PHP has its own template engines.

    How to access session in POJO classes ? Why it is hard / Is there any easy way ?

    First of all it should not be done as it would be a bad programming practice. Secondly it can be done but there is hardly any scenario where this would be necessary. Since there is no need for the same the ways to implement it are not easy.

    It should not be done , If you have to there are two ways.

    1. We can pass session and request objects from servlet to POJO classes(via methods or constructors).
    2. Store the request in a ThreadLocal in a Filter (and clean it afterwards)

    Also look at the SO links for sample code

    get HttpSession|Request from simple java class not servlet class

    Retrieving Web Session from a POJO Outside the Web Container

    I asked this question when I was stuck at a particular scenario . I used to program in PHP and wondered why I never had the issue there and got confused. People who usually move from php to java , also have a hard time understanding the how it all works , so attaching one more part to the answer.

    Why servlets require servlet containers like tomcat while php runs on a http server/webserver (Apache).?

    The question is incorrect in assumptions made but the answer will highlight why .

    A web server is software that helps to deliver web content (web pages) to the clients (e.g. web browser) through the Internet using HTTP protocol. HTTP is a simple request /response protocol underpinning most web applications on the Internet, regardless of whether they are written in Java. An HTTP request can correspond to one of the seven HTTP methods: GET, POST, HEAD, OPTIONS, TRACE, PUT and DELETE

    The server side technologies like JSP/Servlets, ASP, and PHP etc. will require their software libraries to be installed at the server. Without these libraries a web server won’t be able to execute those server technologies and is just an HTTP Server. An HTTP server can handle HTML and other client side technologies which don’t require any server capability. The Apache HTTP Server is an example of an HTTP server.

    Regarding Servet/JSP : Servlet can be run on a servlet container.Tomcat is normally called a servlet container.A Servlet container is basically a web server for Java Servlets and JSP pages. Tomcat has an HTTP listener inside it, but in addition to that it has a servlet/JSP engine. A servlet, in the end, is a Java class. JSP files (which are similar to PHP, and older ASP files) are generated into Java code (Servlet), which is then compiled to .class files by the server and executed by the Java virtual machine.

    Regarding PHP : PHP will not work in pure Apache or any HTTP server. We are able to run php in apache because it is linked to Apache(SAPI - server API) Using mod_php5.so(or similar) modules. We do not normally start any php daemon. Apache starts the php interpreter along with itself.

    If oracle wants, it can develop a module similar to PHP module which will make it easier to host Java code on an HTTP server. But using a servlet container model has many benefits compared to the PHP interpreter model even with the disadvantage of increase in complexity.