HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Thread Safe Servlet

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
servletthreadsafe

Problem

I am working on a JSP MVC web application. I am confused about Thread-Safe Servlet concept. Following is my code, please tell me is it thread safe or not. Also, tell me the reason that why it is thread-safe or not thread-safe.

JSP Code;


Personal Profile

    
        First Name:
        Last Name:
        Email:Without @ part
        
    
    
    
    
    


Servlet Code;

package Servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.WelcomeName;

@WebServlet("/PersonalDataServlet")
public class PersonalDataServlet extends HttpServlet {

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

        String FirstNameServlet;
        String LastNameServlet;
        String EmailServlet;

        FirstNameServlet = request.getParameter("FirstNameField");
        LastNameServlet = request.getParameter("LastNameField");
        EmailServlet = request.getParameter("EmailField");

        System.out.println(FirstNameServlet + "\n" + LastNameServlet + "\n" + EmailServlet);

        WelcomeName WelcomeNameObject = new WelcomeName();
        WelcomeNameObject.Fullname(FirstNameServlet, LastNameServlet, EmailServlet);

        request.setAttribute("FullName", WelcomeNameObject.FullName);
        request.setAttribute("EmailAddress", WelcomeNameObject.EmailAddress);

        request.getRequestDispatcher("Profile.jsp").forward(request, response);

    }

}


Other Class Which is used to the actual calculation or Business Logic

```
package Model;

public class WelcomeName {

public String FullName;
public String EmailAddress;

public void Fullname(String FirstName, String LastName, String Email) {
FullName = (Fir

Solution

It is thread safe. It's because your Servlet class doesn't declare anything outside the scope of the doPost method.

You don't control how many Servlets you have or when they are created/destroyed, your Servlet container does, but, as long as you keep the class with no members it'll be thread safe. You might find this article useful.

Let's check out an example of a non thread safe servlet:

@WebServlet("/PersonalDataServlet")
public class PersonalDataServlet extends HttpServlet {

    boolean locked;

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

        if (!locked){
            locked = true;
            // Process
            locked = false
        }

    }

}


The code above wouldn't work in multi-threaded environments, because different threads could execute doPost at different times, so you couldn't trust in the lock field.

Code Snippets

@WebServlet("/PersonalDataServlet")
public class PersonalDataServlet extends HttpServlet {

    boolean locked;

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

        if (!locked){
            locked = true;
            // Process
            locked = false
        }

    }

}

Context

StackExchange Code Review Q#127072, answer score: 2

Revisions (0)

No revisions yet.