Skip to main content

Replace deprecated Jakarta Servlet methods and classes

org.openrewrite.java.migrate.jakarta.RemovalsServletJakarta10

This recipe replaces the classes and methods deprecated in Jakarta Servlet 6.0.

Recipe source

GitHub, Issue Tracker, Maven Central

info

This recipe is composed of more than one recipe. If you want to customize the set of recipes this is composed of, you can find and copy the GitHub source for the recipe from the link above.

This recipe is available under the Moderne Source Available License.

Definition

  • Change type
    • oldFullyQualifiedTypeName: javax.servlet.http.HttpServletRequest
    • newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletRequest
  • Change type
    • oldFullyQualifiedTypeName: javax.servlet.http.HttpServletRequestWrapper
    • newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletRequestWrapper
  • Change type
    • oldFullyQualifiedTypeName: javax.servlet.http.HttpServletResponse
    • newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletResponse
  • Change type
    • oldFullyQualifiedTypeName: javax.servlet.http.HttpServletResponseWrapper
    • newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletResponseWrapper
  • Change type
    • oldFullyQualifiedTypeName: javax.servlet.http.HttpSession
    • newFullyQualifiedTypeName: jakarta.servlet.http.HttpSession
  • Change type
    • oldFullyQualifiedTypeName: javax.servlet.ServletContext
    • newFullyQualifiedTypeName: jakarta.servlet.ServletContext
  • Change type
    • oldFullyQualifiedTypeName: javax.servlet.UnavailableException
    • newFullyQualifiedTypeName: jakarta.servlet.UnavailableException
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpServletRequest isRequestedSessionIdFromUrl()
    • newMethodName: isRequestedSessionIdFromURL
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpServletRequestWrapper isRequestedSessionIdFromUrl()
    • newMethodName: isRequestedSessionIdFromURL
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpServletResponse encodeUrl(String)
    • newMethodName: encodeURL
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpServletResponseWrapper encodeUrl(String)
    • newMethodName: encodeURL
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpServletResponse encodeRedirectUrl(String)
    • newMethodName: encodeRedirectURL
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpServletResponseWrapper encodeRedirectUrl(String)
    • newMethodName: encodeRedirectURL
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpSession getValue(String)
    • newMethodName: getAttribute
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpSession getValueNames()
    • newMethodName: getAttributeNames
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpSession putValue(String, Object)
    • newMethodName: setAttribute
  • Change method name
    • methodPattern: jakarta.servlet.http.HttpSession removeValue(String)
    • newMethodName: removeAttribute
  • Delete method argument
    • methodPattern: jakarta.servlet.http.HttpServletResponse setStatus(int, String)
    • argumentIndex: 1
  • Delete method argument
    • methodPattern: jakarta.servlet.http.HttpServletResponseWrapper setStatus(int, String)
    • argumentIndex: 1
  • Reorder method arguments
    • methodPattern: jakarta.servlet.ServletContext log(Exception, String)
    • newParameterNames: [ex, str]
    • oldParameterNames: [str, ex]
    • matchOverrides: true
  • Updates getRealPath() to call getContext() followed by getRealPath()
  • Delete method argument
    • methodPattern: jakarta.servlet.UnavailableException <constructor>(jakarta.servlet.Servlet, String)
    • argumentIndex: 0
  • Delete method argument
    • methodPattern: jakarta.servlet.UnavailableException <constructor>(int, jakarta.servlet.Servlet, String)
    • argumentIndex: 1
  • Reorder method arguments
    • methodPattern: jakarta.servlet.UnavailableException <constructor>(int, String)
    • newParameterNames: [str, in]
    • oldParameterNames: [in, str]
    • matchOverrides: true

Examples

Example 1
Before
import java.io.IOException;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.SingleThreadModel;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionContext;
import jakarta.servlet.http.HttpUtils;

class TestJakarta extends HttpServlet implements SingleThreadModel {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.isRequestedSessionIdFromUrl();

res.encodeUrl("");
res.encodeRedirectUrl("");

res.setStatus(0, "");

res.setStatus(0);

HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
reqWrapper.isRequestedSessionIdFromUrl();

HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);

resWrapper.encodeUrl("");
resWrapper.encodeRedirectUrl("");

resWrapper.setStatus(0, "");

HttpSession httpSession = req.getSession();
httpSession.getSessionContext();
httpSession.getValue("");
httpSession.getValueNames();
httpSession.putValue("", null);
httpSession.removeValue("");

ServletContext servletContext = getServletContext();

servletContext.getServlet("");
servletContext.getServlets();
servletContext.getServletNames();

servletContext.log(null, "");
}
}
After
import java.io.IOException;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.SingleThreadModel;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionContext;
import jakarta.servlet.http.HttpUtils;

class TestJakarta extends HttpServlet implements SingleThreadModel {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.isRequestedSessionIdFromURL();

res.encodeURL("");
res.encodeRedirectURL("");

res.setStatus(0);

res.setStatus(0);

HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
reqWrapper.isRequestedSessionIdFromURL();

HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);

resWrapper.encodeURL("");
resWrapper.encodeRedirectURL("");

resWrapper.setStatus(0);

HttpSession httpSession = req.getSession();
httpSession.getSessionContext();
httpSession.getAttribute("");
httpSession.getAttributeNames();
httpSession.setAttribute("", null);
httpSession.removeAttribute("");

ServletContext servletContext = getServletContext();

servletContext.getServlet("");
servletContext.getServlets();
servletContext.getServletNames();

servletContext.log("", null);
}
}

Example 2
Before
import java.io.IOException;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.SingleThreadModel;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionContext;
import jakarta.servlet.http.HttpUtils;

class TestJakarta extends HttpServlet implements SingleThreadModel {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.isRequestedSessionIdFromUrl();

res.encodeUrl("");
res.encodeRedirectUrl("");

res.setStatus(0, "");

res.setStatus(0);

HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
reqWrapper.isRequestedSessionIdFromUrl();

HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);

resWrapper.encodeUrl("");
resWrapper.encodeRedirectUrl("");

resWrapper.setStatus(0, "");

HttpSession httpSession = req.getSession();
httpSession.getSessionContext();
httpSession.getValue("");
httpSession.getValueNames();
httpSession.putValue("", null);
httpSession.removeValue("");

ServletContext servletContext = getServletContext();

servletContext.getServlet("");
servletContext.getServlets();
servletContext.getServletNames();

servletContext.log(null, "");
}
}
After
import java.io.IOException;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.SingleThreadModel;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionContext;
import jakarta.servlet.http.HttpUtils;

class TestJakarta extends HttpServlet implements SingleThreadModel {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.isRequestedSessionIdFromURL();

res.encodeURL("");
res.encodeRedirectURL("");

res.setStatus(0);

res.setStatus(0);

HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
reqWrapper.isRequestedSessionIdFromURL();

HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);

resWrapper.encodeURL("");
resWrapper.encodeRedirectURL("");

resWrapper.setStatus(0);

HttpSession httpSession = req.getSession();
httpSession.getSessionContext();
httpSession.getAttribute("");
httpSession.getAttributeNames();
httpSession.setAttribute("", null);
httpSession.removeAttribute("");

ServletContext servletContext = getServletContext();

servletContext.getServlet("");
servletContext.getServlets();
servletContext.getServletNames();

servletContext.log("", null);
}
}

Usage

This recipe has no required configuration options. It can be activated by adding a dependency on org.openrewrite.recipe:rewrite-migrate-java in your build file or by running a shell command (in which case no build changes are needed):

  1. Add the following to your build.gradle file:
build.gradle
plugins {
id("org.openrewrite.rewrite") version("7.5.0")
}

rewrite {
activeRecipe("org.openrewrite.java.migrate.jakarta.RemovalsServletJakarta10")
setExportDatatables(true)
}

repositories {
mavenCentral()
}

dependencies {
rewrite("org.openrewrite.recipe:rewrite-migrate-java:3.8.0")
}
  1. Run gradle rewriteRun to run the recipe.

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Source files that had results

org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

Column NameDescription
Source path before the runThe source path of the file before the run. null when a source file was created during the run.
Source path after the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
Parent of the recipe that made changesIn a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all.
Recipe that made changesThe specific recipe that made a change.
Estimated time savingAn estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds.
CycleThe recipe cycle in which the change was made.

Contributors

Anu Ramamoorthy, Jonathan Schnéider, Tim te Beek