Java Servlet Project Setup with example

 


Java Servlet Setup

Introduction to Java Servlet, how do you setup a Java Servlet Project
  • Servlet is a Java technology that is used for developing Web Applications and managed by a container called a servlet engine.
  • It generates dynamic content and interacts with the client through Request and Response.
  • Servlet extends the functionality of a web server.
  • Java Servlet API is available in javax.serlet package
  • In this article, we will see how to setup java servlet

Web Application Architecture (Three Tier)

  • Presentation Layer
    • Client layer: to view the application)
  • Application Layer
    • Business Logic Layer : interacts with the Database layer and sends required information to the Presentation layer
  • Data Layer
    • The data is stored in this layer. The application layer communicates with the Database layer to retrieve the data

Java Servlet Life-Cycle

  • The Java Servlet Life cycle includes three stages,
    • init() : gets invoked is when the servers are fired up
    • service() : the service requests from the client end
    • destroy() : Servlet performs the cleanup activities

Creating Dynamic Java Servlet Project using Eclipse

  • Type a Project Name and Choose the run time environment as "Apache=>Tomcat Version 9"
  • Enable web.xml in the next page 

  • Java Servlet Project Setup with example

  • Click Finish for creating project

  • Now create a new package by right clicking on the /src/main/java folder (E.g. org.vit.web)

  • Now create a Java Class by right clicking on the org.vit.web package and add the following content
package org.vit.web;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

public class GreetUser extends HttpServlet{
  public void service(ServletRequest request, ServletResponse response) throws IOException {
      response.setContentType("text/html");
      response.getWriter().println("Welcome to Java Servlet");
  }
}
  • Right click on the project and the Choose "Run As-> Run On Server" Option
  • In Next Popup window, choose Local Server as TOmcat and click Finish
  • Point the http://localhost:8080/Demo/welcome URL in Web Browser for output.

Deployment Descriptor (web.xml)

  • web.xml is used for defining name of the servlet and URL mapping
  • It is under src->main->webapp->WEB-INF Folder
  • welcome-file tag is used for specifying landing or home page
  • Sample Snippet ```xml <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
       version="4.0">
    
      <servlet-name>greet</servlet-name>
      <servlet-class>org.vit.web.GreetUser</servlet-class>
    
      <servlet-name>greet</servlet-name>
      <url-pattern>/welcome</url-pattern>
    
        <welcome-file>test.jsp</welcome-file>  
    </welcome-file-list>  
    
## What is JSP?
- Java Server Pages (JSP) is a collection of technologies developed by Sun Microsystems.
- It is used to develop web pages by inserting Java code into the HTML pages by making special JSP tags.

### JSP Scriptlets
- Scriptlet tags are the easiest way to put java code in a JSP page. A scriptlet tag starts with <% and ends with %>.

```java
<%
  Date d = new Date();
  out.println("Current Date="+d);
%>

How to run servlet program in java.

JSP Directives

  • JSP Directives are used to give special instructions to the container while JSP page is getting translated to servlet source code. JSP directives starts with <%@ and ends with %>
  • For Example <%@ page import="java.sql.Date" %>

JSP Declaration

  • used to declare member methods and variables of servlet class. JSP Declarations starts with <%! and ends with %>.
<%! public static int count=0; %>

JSP Expression

  • JSP Expression starts with <%= and ends with %>.
  • <% out.print("Kumar"); %> can be written using JSP Expression as <%= "Kumar" %>