How To Dice Servletcontext Inward Servlet, Jsp, Activeness Flat Together With Controller.

How to larn ServletContext inwards Servlet, jsp, boundary controller or struts activity class is mutual demand of whatever Java spider web developer. As ServletContext is an application broad object as well as used to shop variables inwards global scope, getting a reference of ServletContext is pretty important. Every spider web application tin accept alone 1 ServletContext though they tin accept multiple ServletConfig objects. In this article nosotros volition encounter :

How to larn Servlet Context within Spring MVC Controller?
How to find  Servlet Context within Struts Action class?
How to larn Servlet Context within JSP File?
How to discovery Servlet Context within HttpServlet Class?

How to larn Servlet Context within Spring MVC Controller?

 boundary controller or struts activity aeroplane How to larn ServletContext inwards Servlet, JSP, Action aeroplane as well as Controller.How to perform LDAP authentication inwards windows Active directory using Spring Security  and hither nosotros volition encounter how to recollect ServletContext within Spring Controller class. We often demand ServletContext reference within Spring Controller aeroplane to larn a reference of Spring AppContext inwards club to recollect edible bean defined within applicationContext.xml

Spring spider web application has split applicaitonContext called WebApplicationContext. In club to larn this, yous demand a reference of the ServletContext object. Since every Controller inwards Spring has access to HttpRequest as well as HttpResponse object, nosotros tin larn ServletContext from asking object, but asking object doesn't accept take away access to ServletContext prior to Servlet Specification 3.0, therefore yous demand to commencement larn HttpSession object from asking as well as than ServletContext object shape session equally shown inwards 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 yous are running on web_app version 3.0 as well as has Servlet 3.0 API yous tin straight larn ServletContext object shape HttpServletRequest equally shown inwards below example:

ServletContext sc = request.getServletContext();


How to larn ServletContext object inwards JSP

In our in conclusion article on JSP nosotros accept seen how to define application broad mistake page as well as hither nosotros volition encounter how to larn the Servletcontext object inwards JSP. This is too 1 of oftentimes asked JSP interview questions generally for freshers as well as less experienced developers . Anyway getting the ServletContext reference inwards JSP is quite slow equally compared to Spring or Struts. Since JSP has a reference of ServletContext equally an implicit object as well as yous tin straight access it using "application" implicit object within whatever scriptlet or expression. You tin fifty-fifty access variable stored within ServletContext past times using facial expression linguistic communication (EL) map called applicationScope. hither is a code instance of getting ServletContext within JSP file:

<%
    application.setAttribute("language", "Java");
%>
  
What is your favourite programming linguistic communication : ${applicationScope['language']}

and it volition impress inwards HTML equally "What is your favorite programming linguistic communication : Java"

You tin too purpose JSTL kernel ready tag  for putting variables as well as beans on application reach defined past times the ServletContext object.

How to larn ServletContext object within Servlet:

This enquiry is too oftentimes asked inwards diverse Java Servlet interviews along amongst other J2EE technology. You tin easily larn a reference of ServletContext within your Servlet inwards the same agency nosotros accept accessed ServletContext inwards our boundary Controller example. Until yous accept access of ServletRequest or HttpServletRequest yous tin larn session object from it as well as and then yous tin larn a reference of ServletContext. An instance is precisely similar to our before boundary controller code. hither is an example of getting ServletContext within 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(); volition run on Servlet 3.0
        } finally {          
            out.close();
        }
    }

How to larn ServletContext object Action aeroplane Struts:

In our in conclusion article on Jakarta Struts nosotros accept seen Top 10 jakarta struts interview questions asked inwards J2EE interviews as well as hither nosotros volition how to find ServletContext reference within Struts Action class. Similar approach of Spring Controller as well as Servlet tin last used to larn ServletContext within Struts Action class. Since Struts Action aeroplane contains execute method which contains HttpServletRequest equally parameter, yous tin purpose HttpServletRequest for getting ServletContext only similar higher upward example. hither is sample instance of how to access servletContext object within 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 {

    /* frontwards 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 nosotros know how to larn ServletContext inwards Spring Controller, Struts Action Class, Servlet, as well as JSP. You encounter inwards reality all examples are similar they all larn the reference of ServletContext using HttpServletRequest or HttpSession. Servlet API 3.0 has an fifty-fifty to a greater extent than convenient method within HttpServletRequest which tin last used to straight access ServletContext from the asking object.

Further Learning
Spring Framework 5: Beginner to Guru
Difference betwixt include directive as well as include activity inwards JSP


0 Response to "How To Dice Servletcontext Inward Servlet, Jsp, Activeness Flat Together With Controller."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel