J2EE – Servlet – Short Description


1.       J2EE is a technology to develop distributed, scalable, enterprise applications.
2.       J2EE is a technology that comprises or composed of many technologies
a.       Servlet
b.      EJB
c.       JSP
d.      JDBC
e.      JMS
f.        JCA
g.       JNDI
3.       J2EE is the number 1 technology in server side.
4.       J2EE is a component based architecture.
5.       WEB Container:
i.        It’s a runtime environment
j.        Servlets/JSP are known as web components
k.       EJB is known as Business Component
Advantages:
1.       Security Management
2.       Maintenance and Development Easy
3.       Deployment is easy
4.       Scalable
SERVLET CONTAINER -> WEB CONTAINER
*More than 1 JVM’s working together is known as distributed.
PL – Is about view of data – Presentation Logic
BL – Is about Functionality Performed on Data – Business Logic
DL – Is about data access logic or data logic or database logic
CGI and Servlet
1.       New Process is created each time.          V/s         New thread is created each time
2.       Slow                                                                      V/s         Fast
3.       Can crash the whole server                         V/s         Only effect the container
4.       Not Secure                                                         V/s         Secure
Ways to make request to http server:
1.       Get
2.       Post
3.       Head
4.       Delete
5.       Options
6.       Trace
7.       Connect
GET and POST
1.       Parameters are appended to the URL of the resource being asked           V/s         It becomes part of the body.
2.       Limited                                V/s                         Unlimited
3.       Not Secure                         V/s                         Secure
4.       Is Fast                                   V/s                         Slow
SERVLET
1.       It is a specification
2.       It is an interface
3.       Methods inside servlet interface
Init(), service(), destroy(), getServletConfig()
4.       Servlet(I)->GenericServlet(ac)->HttpServlet(ac)
5.       Servlet is a managed object. The life cycle of a servlet is managed by the web container which is a run time environment
6.       Servlet 3.0 is the latest specification
7.       Life Cycle of Servlet:
i.                    Init – First time only
ii.                  Service – Every time
iii.                Destroy – Server goes down, deployment/un deployment
·         Placing or associating components to a container is known as deployment.
1st Request:
          Servlet is loaded
          Servlet is instantiated
          ServletConfig is instantiated
          Init is called
          ServletRequest is instantiated
          Request Processing thread is started
          Service is called
2ndRequest:
          ServletRequest is instantiated
          Request Processing thread is started
          Service is called
Interfaces
1.       Servlet
2.       ServletRequest
3.       ServletResponse
4.       HttpServletRequest
5.       HttpServletResponse
ServletConfig – 1 instance for each servlet
ServletContext – 1 instance for the whole web application.
Directory representation
Tomcat Introduction
Deployment Descriptor: Gives details of the server where application has been deployed.
Deployment Types:
1.       Packaged (.war)
2.       Exploded (Webapps)
Default Servlet: HttpServlet (95% of time)
          Two service is present, one is public and the other is protected.
          Public service(ServletRequest sr, ServletResponse sres);
          Protected service(HttpServletRequest hsr, HttpServletResponse hsres);
          The public calls the protected and after request check, corresponding do method is called.
Examples:
1.       Welcome
2.       Parameters or form
3.       Interservlet Comm.
SCOPE in which attributes can be stored and retrieved from
1.       Request Scope
2.       Response Scope
3.       Session Scope
4.       Application Scope
Note:
          Parameters can be any string whereas attributes can be objects.
          To set an attribute in the servlet context is equivalent to placing it in application scope.
          E.g.:
ServletContext ctx=getServletContext();
ctx.setAttribute(“country”, “India”);
out.println(“More”);
                                ServletConfig sc=getServletConfig();
                                out.println(sc.getInitParameter(“one”));
                one
                test
“Above lines of code will be written or placed inside the web.xml file
In first file,
request.setAttribute(“color”, “red”);
In Second file,
String str=(String)request.getAttribute(“color”);
e.g. Scope
Web.xml
user
sudhanshu
Res.setContentType(“application/vnd.ms.excel”);
PrintWriter pw=res.getWriter();
out.println(“15\t30\t=sum(a1:b1)”);
e.g. Test1
State Management
Cookies
Hidden Form Fields
URL Re-Writing
HttpSession
Cookies:
          Is a class in java
          Are insecure to use
          Stored on the client side
          No sensitive information is stored
          Cookies can be timed
Persistent Cookies -> Cookies send to a file
Non-Persistent Cookies -> Cookies destroyed after closing the browser
*example
Hidden Form Fields
*example
URL Rewriting
*example
To encode url:
Res.encodeURL(“urlpattern”) in href.
HTTP Session
HttpSession hs=request.getSession();
If(hs.isNew()){
                out.println(“New Session”);
}else{
out.println(“Old Session”);
}
To expire session after a particular period of time.
10
To expire a session put negative values in times place.
To expire session setMaxInactiveInterval(time);
                             HttpSession hs=req.getSession()
                                hs.invalidate();