Wednesday, March 6, 2019

Starting with Servlet


Talking about the Servlets, it all starts with Dynamic Web.
In traditional programming, applets were used to access a database and get dynamic data,but they were too slow and inefficient. So we use dynamic web which in terms introduces Common Gateway Interface (CGI) programs  and client-side scripting to deal with that problem.
  • CGI programs reside on the server and they accept requests, use the server-side resources, and generate an HTML page as a response. They can be written in a variety of language, such as Java,C++,Perl and Visual Basic. Examples of CGI programs are JSPs, Servlets, ASPs etc.
  • Client-side scripting uses scripting language at client side for small business applications,such as form validation.
Here we'll elaborate Dynamic Web applications using servlets on J2EE platform. J2EE specifications were written by Sun Microsystems. It is made up of 13 technologies : 
1.    Java Server Pages (JSPs)
2.    Servlets
3.    Java messaging Service (JMS)
4.    Java Database Connectivity (JDBC)
5.    Java Naming and Directory Interface (JNDI)
6.    Java Transaction Service (JTS)
7.    Java Transaction API (JTA)
8.    JavaMail
9.    JavaBeans Activation Framework (JAF)
10. Remote Method Invocation (RMI)
11. Enterprise JavaBeans (EJB)
12. Extensible Markup Language (XML)
13. Java Integrated Definition Language (Java IDL)
Recently J2EE Connector Architecture is added by Sun to the J2EE.
Further J2EE uses XML for deployment descriptors. A complaint application server supports all the technologies defined in the J2EE. This helps developers because they can write applications to the specification and then choose what application server to deploy on.
We'll choose front end of the Dynamic Web Application (client-side) to be written in JSP which includes HTML,CSS and JavaScript. But to run JSP, there needs to be an environment.
We'll use Apache's Tomcat as our JSP server. Apache Tomcat is a free Web server that can handle requests for HTML, JSPs, and Servlets. Another runtime environment you could use is BEA WebLogic Server.
Download Apache Tomcat 8.5 from here : https://tomcat.apache.org/download-80.cgi and configure it. Change tomcat users id and password in xml file (conf/tomcat-users.xml) for admin and manager-gui users as like this: 
        <tomcat-users>
        <role rolename="manager-gui"/>
        <user username="admin" password="" roles="manager-gui"/>
        <role rolename="admin-gui"/>
        <user username="tomcat" password="s3cret" roles="admin-gui"/>
        </tomcat-users>
After configuring apache tomcat, we'll add it as runtime environment in our IDE for writing JSPs which may be Eclipse J2EE , Netbeans or other IDE providing J2EE technologies.
Servlets for Dynamic Web Application:
Servlet is a CGI program. In most common terms, a servlet is a Java class that implements the Servlet Interface (refer to Interfaces in Java) and accepts requests and generate responses. The requests come from Java classes, Web clients, or other Servlets.
One doubt anyone can acquire is that How JSPs become Servlets ?
Answer to this query is quite simple, it's all about mapping things out there.
·        In your servlet container, the JSP servlet is mapped to any URL that ends in .jsp (usually)
·        When one of those .jsp  URLs is requested, the request goes to the JSP servlet. Then, this servlet checks if the JSP is already compiled.
·        If the JSP is not compiled yet, the JSP servlet translates the JSP to some Java source code implementing the Servlet interface. Then it compiles this Java source code to a .class file. This .class file usually is located somewhere in the servlet container's work directory for the application.
·        Once the JSP servlet has compiled the servlet class from the JSP source code, it just forwards the request to this servlet class.
The thing is, unless you specifically precompile your JSP, all this happens at runtime, and hidden in the servlet container's work directory, so it is "invisible". Also have in mind that this is what happens "conceptually", several optimizations are possible in this workflow.
In the next section we'll talk about how to create a complete Web Application.


No comments:

Post a Comment

Event Handling in Spring

Spring's event handling is single-threaded so if an event is published,  until and unless all the receivers get the message, the process...