Servlet

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

Servlet Life Cycle

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.

HTTP methods — doGet and doPost

  1. Let’s extend our Servlet class to HttpServlet. This extension allows us to use the doGet method and doPost. Get and Post are common methods of the HTTP to indicate the kind of action that we want. Here are definition of some of the methods commonly used:
  • GET — Fetches data from server
  • POST — Sends data to server
  • PUT — Replaces current representation of target resource with uploaded content
package com.hello;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
public void init() throws ServletException {
// Servlet initialization code here
super.init();
}

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

// Set response content type
response.setContentType("text/html");

// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>Hello</h1>");
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

}

@Override
public void destroy() {
// resource release
super.destroy();
}
}

Deployment Description (web.xml)

  1. In the index.html form, when we click on submit button and ask for /hello path, we really want to call HelloServlet but our application does not know to do that. Hence we need to link both via deployment description of web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.hello.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
  1. sendRedirect
  1. Use request.setAttribute to pass data using RequestDispatcher and perform url rewriting using sendRedirect .

JavaServer Pages (JSP)

We can see to build a form in Server, we have to wrap each line of the HTML with PrintWriter but we have to work allot. We can further improve this by using JSP.

  • Reduction in the length of Code :- In JSP we use action tags, custom tags etc.
  • Connection to Database is easier :-It is easier to connect website to database and allows to read or write data easily to the database.
  • Make Interactive websites :- In this we can create dynamic web pages which helps user to interact in real time environment.
  • Portable, Powerful, flexible and easy to maintain :- as these are browser and server independent.
  • No Redeployment and No Re-Compilation :- It is dynamic, secure and platform independent so no need to re-compilation.
  • Extension to Servlet :- as it has all features of servlets, implicit objects and custom tags
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<%
String hello = "Hello";
%>
<h1><%= hello %></h1>
<br/>
</body>
</html>
Syntax:- 
<%! Dec variable %>
Example:-
<%! int variable = 10; %>
Syntax:- 
<% java code %>
Syntax:- 
<%= expression %>
Example:-
<% num1 = num1+num2 %>
<h1><%= expression %></h1>
Syntax:- 
<% -- Comments %>

Full Stack Web Application and Software Developer