Servlet and JSP.
Websites are collections of static files like text, images, graphics, and HTML files. These websites can be called web applications if they provide dynamic functions when hosted on the servers. So the role of Servlet and JSP is to make web applications.
Servlet
Servlets are server-side programs written in java which is used to develop enterprise application or web application. Servlet is a pre-defined interface present in javax.Servlet API.
When the client(browser) sends a request to a server, the server then sends it to the web container, then the web container forwards it to the corresponding servlet using the deployment descriptor.
Deployment descriptor — It does the mapping between URL and servlet. The web.xml file is the deployment descriptor.
Steps to create a Servlet Application
- Create a servlet(java) program.
- Set the path of Servlet_api.jar.
- Compile the java program.
- Create a client-side page(index.html).
- Create a directory structure.
- Create a deployment descriptor.
- Copy entire web application into web root (tomcat/webapps).
- Start the web server.
- Open the browser and execute the client-side application.
Life cycle of servlet.
- Loading a servlet.
- Servlet initalization.
- Request processing or service method provocation.
- Destroy the servlet.
Inter-Servlet Communication
- Using RequestDispatcher — The RequestDispatcher Interface is used for dispatching the request to a resource i.e Html, servlet, or JSP. The Contents of another resource can be included in this interface. There are two methods of RequestDispatcher forward() and include().
- Using sendRedirect() — This method redirects the response to another resource. This method actually makes the client(browser) create a new request to get to the resource.
JSP — Java Server Pages
Servlets is very good at business logic but not good for presentation. JSP is only an enhancement of servlet. Java code in HTML is known as JSP. Java provides 5 predefined tags and 9 implicit objects to develop JSP application.
Every JSP is internally a servlet ie: the JSP engine is responsible to convert the .jsp file to .java file. After converting the . java file servlet container is responsible to execute other steps.
JSP Life Cycle
Pre-defined Tags
- Scriplet: Scriplets are basically used to write a pure java code. Whatever the java code we write as a part of scriplet, that code will be available as a part of service () method of the servlet. <% …………..%>
2. Expression: Expression tags are used for writing the java valid expressions as a part of the JSP page. All the expression we write in expression tag they will be placed automatically in out.println () method and this method is available as a part of the service method.
3. Declaration: Whenever we use any variables as a part of JSP we have to use those variables in the form of a declaration tag i.e., declaration tag is used for declaring the variables on the JSP page. <%! ………………..%>
4. Directive: The directive tag is used to include JSP files or import a package into our current file. <%@ page attribute=value %>
5. Action: An action tag is used to control the flow between pages.
Implicit object in JSP
- request : HttpServletRequest
- response : HttpServletResponse.
- out : jspwriter~ PrintWriter object
- sessio : HttpSession
- application: ServletContext
- config : ServletConfig
- pagecontext: pageContext
In conclusion, Servlets are server-side programs and take care of the processing, whereas JSP is an interface that is built on top of Servlets to provide added functionalities.