Building REST web service using Jersey

Since Java 5, JAX-RS specification has been announced in order to support building REST web services according to the REST architectural pattern. JAX-RS is a set of interfaces or APIs which provides the building blocks for building REST services in java. Although you can’t build a fully functional RESTful service using these interfaces, there exists a lot of popular frameworks...

forward() vs sendRedirect() vs include()

It is a common practice that a controller redirects to another resource after processing a request, the resource could be either a view which displays the result of processing or even another controller which needs to do further processing on the request. In this tutorial, we explain the different ways of redirecting requests from servlet to another resource. Basically we...

Pass data from servlet to jsp

JSP is mostly used as the view component in any java-based MVC application, its main usage is to present dynamic data processed and generated from server-side controllers like servlet. In this tutorial, we explain the different ways of passing attributes/parameters from servlet to JSP, along with several examples specific for passing data types like: objects, arrays, lists and maps. 1....

Pass data from javascript to servlet

In modern web applications, the data exchange between front-end and back-end is usually done dynamically through javascript. In this tutorial, we explain 2 ways of exchanging data between javascript and java servlets, we also provide an example and business cases for the usage of each way. 1. Submit form programatically The first case is when you already have an html...

Pass data from html to servlet

In this tutorial we explain the common way of passing data from html to java servlets, we mostly focus on passing form data entered by end-users through pages like: login, sign-up, contact-us .. 1. Pass form fields to servlet Consider the following login.html:

Users interact with web applications through forms, the “form” element is the common way for asking end-users...

JSP – Hello World Tutorial

In this tutorial, we provide a step-by-step guide for creating a hello world web application using JSP technology. Prerequisites: Eclipse IDE (Mars release) Java 1.7 Apache tomcat 7 1. Create dynamic web project Open eclipse, then select File -> New -> Dynamic Web Project. In the next screen, fill the mandatory fields as above, you are free to choose any...

Servlet 3.0 – Hello World Tutorial

In this tutorial, we provide a step-by-step guide for creating a hello world web application using Servlet 3.0 technology. Servlet 3.0 supports defining and exposing servlets using annotations, if you still love to define servlets manually, refer to our previous tutorial about creating web applications using servlet 2.5. Prerequisites: Eclipse IDE (Mars release) Java 1.7 Apache tomcat 7 1. Create...

Servlet 2.5 – Hello World Tutorial

In this tutorial, we provide a step-by-step guide for creating a hello world web application using Servlet 2.x technology. Prerequisites: Eclipse IDE (Mars release) Java 1.7 Apache tomcat 7 1. Create dynamic web project Open eclipse, then select File -> New -> Dynamic Web Project. In the next screen, fill the mandatory fields as above, in case you didn’t previously...

StringBuilder VS StringBuffer

Since String is immutable, it is very costly to use it when constructing a dynamic character string due to the fact that a new memory location will be allocated at each assignment or modification. Since its first release, Java supports a mutable class called StringBuffer which constructs dynamic character strings through allocating just 1 memory location, this saves a lot...

Java try-with-resources

Java 7 supports a new statement called try-with-resources which extends the behavior of the traditional try/catch block for the sake of automatic resource management, since Java 7 developers are able to access resources (files, db connections, sockets) inside a try-with-resources block without the need to worry about closing them afterwards, the resource closure is done automatically. 1. Resource management using traditional...