How to get ServletContext in Servlet, jsp, spring controller or struts action class is common need of any Java web developer. As ServletContext is an application wide object and used to store variables in global scope, getting a reference of ServletContext is pretty important. Every web application can have only one ServletContext though they can have multiple ServletConfig object. In this article we will see :
How to get Servlet Context inside Spring MVC Controller?
How to find Servlet Context inside Struts Action class?
How to get Servlet Context inside JSP File?
How to get Servlet Context inside Spring MVC Controller?
In our last article on Spring we have seen How to perform LDAP authentication in windows Active directory using Spring Security and here we will see how to retrieve ServletContext inside Spring Controller class. We often need ServletContext reference inside Spring Controller class to get a reference of Spring AppContext in order to retrieve bean defined inside applicationContext.xml. Spring web application has separate applicaitonContext called WebApplicationContext. In order to get this you need a reference of ServletContext object. Since every Controller in Spring has access to HttpRequest and HttpResponse object, we can get ServletContext from request object, but request object doesn't have direct access to ServletContext prior to Servlet Specification 3.0, hence you need to first get HttpSession object from request and than ServletContext object form session as shown in below code example:
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
ServletContext sc = session.getServletContext();
WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(sc);
return new ModelAndView("hello");
}
if you are running on web_app version 3.0 and has Servlet 3.0 API you can directly get ServletContext object form HttpServletRequest as shown in below example:
ServletContext sc = request.getServletContext();
How to get ServletContext object in JSP
In our last article on JSP we have seen how to define application wide error page and here we will see how to get Servletcontext object in JSP. This is also one of frequently asked JSP interview questions mostly for freshers and less experienced developers . Any way getting ServletContext reference in JSP is quite easy as compared to Spring or Struts. Since JSP has reference of ServletContext as implicit object and you can directly access it using "application" implicit object inside any scriptlet or expression. You can even access variable stored inside ServletContext by using expression language (EL) map called applicationScope. here is a code example of getting ServletContext inside JSP file:
<%
application.setAttribute("language", "Java");
%>
What is your favourite programming language : ${applicationScope['language']}
and it will print in HTML as "What is your favorite programming language : Java"
You can also use JSTL core set tag
How to get ServletContext object inside Servlet:
This question is also frequently asked in various Java Servlet interviews along with other J2EE technology. You can easily get reference of ServletContext inside your Servlet in the same way we have accessed ServletContext in our spring Controller example. Until you have access of ServletRequest or HttpServletRequest you can get session object from it and than you can get reference of ServletContext. Example is exactly similar to our earlier spring controller code. here is example of getting ServletContext inside Servlet:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleRequest(request, response);
}
protected void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
HttpSession session = request.getSession(true);
ServletContext context = session.getServletContext();
//ServletContext context = request.getServletContext(); will work on Servlet 3.0
} finally {
out.close();
}
}
How to get ServletContext object Action class Struts:
In our last article on Jakarta Struts we have seen Top 10 jakarta struts interview questions asked in J2EE interviews and here we will how to find ServletContext reference inside Struts Action class. Similar approach of Spring Controller and Servlet can be used to get ServletContext inside Struts Action class. Since Struts Action class contains execute method which contains HttpServletRequest as parameter, you can use HttpServletRequest for getting ServletContext just like above example. here is sample example of how to access servletContext object inside Struts Action class :
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
public class HelloAction extends org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
ServletContext context = session.getServletContext();
return mapping.findForward(SUCCESS);
}
}
Now we know how to get ServletContext in Spring Controller, Struts Action Class, Servlet and JSP. You see in reality all examples are similar they all get reference of ServletContext using HttpServletRequest or HttpSession. Servlet API 3.0 has even more convenient method inside HttpServletRequest which can be used to directly access ServletContext from request object.
Some other Java web tutorials you may like:
Tidak ada komentar:
Posting Komentar