Table of contents
1.
Introduction
2.
Methods of ServletActionContext Class
3.
Methods and Description
4.
Methods Inherited from com.opensymphony.xwork2.ActionContext class 
5.
How HTTPServletRequest and HTTPServletResponse works?
6.
Obtain Instance of HttpSession
7.
Frequently Asked Questions
7.1.
What is ServletActionContext Class?
7.2.
What is the difference between ServletActionContext and ServletContext?
7.3.
What is HttpServletRequest and HttpServletResponse?
7.4.
What is the life cycle of the servlet?
7.5.
How can you get the HttpServletRequest object?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

ServletActionContext Class

Author Gunjan Batra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, you will understand about ServletActionContext class. We will cover the methods that are present in the ServletAction. It extends the class ActionContext.

Struts2 img

We will see how these methods help in getting a response from the servlet.  Access to elements such as the action name, value stack, etc. is made possible through this class's subclass of ActionContext.

Methods of ServletActionContext Class

💥HttpServletRequest

💥HttpServletResponse

          💥ServletContext and

💥HttpSession objects.

Methods and Description

The following methods are used in every file.

Methods img

Must Read: What is Servlet

Methods Inherited from com.opensymphony.xwork2.ActionContext class 

These methods are present inside the action context class which helps in action execution. The values that are stored inside the ActionContext are unique per thread. The most common were discussed above.  There are further methods that are present inside the ActionContext class and the inks are attached for further reading.
getgetActionInvocationgetApplicationgetContainergetContextgetContextMapgetConversionErrorsgetInstancegetLocale,

getName, setName, setParameterssetSessionsetValueStack, setConversionErrorssetLocale, , setContext, setContainer

getParametersgetSessiongetValueStackputsetActionInvocationsetApplication.

How HTTPServletRequest and HTTPServletResponse works?

HttpServletRequest and HttpServletResponse objects are created by the servlet container and passed as arguments to the servlet service () method when the client sends a request to the web server. You can prepare and send the client a response using the response object.

Obtain Instance of HttpSession

To get an instance of HTTPSession, You use the getSession() function of the HttpServletRequest interface after you have an instance of the HttpServletRequest class, you can obtain an instance of the HttpSession.

HttpServletRequest requ=ServletActionContext.getRequest();  
HttpSession session=req.getSession();
Response img

Frequently Asked Questions

What is ServletActionContext Class?

It is provided by Struts2, it provides a way to access web objects like servlet parameters, request attributes, and HTTP Session. It is a sub-class of ActionContext which provides access to things like the action name, value stack, etc. It provides static methods which help to access objects.

What is the difference between ServletActionContext and ServletContext?

ServletActionContext is unique to the action whereas ServletContext is shared by all the servlets.ServletActionContext is not a part of standard JSP/Servlet specs. It is provided by struts2. ServletContext interface has a set of methods that a servlet uses to communicate with its servlet container. ServletActionContext class provides certain.

What is HttpServletRequest and HttpServletResponse?

The interface HttpServletRequest extends the ServletRequest interface to provide request information for HTTP servlets. It helps you to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers and the response body.

What is the life cycle of the servlet?

The entire procedure leading up to and including servlet elimination is known as a servlet life cycle. An example of a servlet's pathways is shown below.
Making a call to the init() method initialises the servlet. To process a client's request, the servlet calls the service() function. Calling the destroy() method ends the servlet process. Finally, the trash collector of the JVM cleans up the servlet.

How can you get the HttpServletRequest object?

To obtain the HttpServletRequest in struts2 you can use the following two methods: 

The ServletActionContext helps in directly accessing the getRequest method from the  ServletActionContext. You can get the HttpServletRequest object directly from this link org, apache.struts2.ServletActionContext.and the other is ServletRequestAware which make your class implements the org.apache.struts2.interceptor.ServletRequestAware .

Conclusion

Congratulations on completing this blog! In this blog, you have understood what is ServletContextClass and its methods. You have further seen how these methods help in connecting to the server and getting the response for the same. 

To learn more about Struts2 refer to our code studio library or you can learn from the link mentioned below. What are Struts and  Struts2 Annotations.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Thankyou
Live masterclass