How To Snuff It Servletcontext Inwards Servlet, Jsp, Activity Class Too Controller.

How to larn ServletContext inwards Servlet, jsp, restrain controller or struts activeness class is mutual demand of whatever Java spider web developer. As ServletContext is an application broad object together with used to shop variables inwards global scope, getting a reference of ServletContext is pretty important. Every spider web application tin bring entirely 1 ServletContext though they tin bring multiple ServletConfig objects. In this article nosotros volition run across :

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?

 restrain controller or struts activeness course of didactics How to larn ServletContext inwards Servlet, JSP, Action course of didactics together with Controller.How to perform LDAP authentication inwards windows Active directory using Spring Security  and hither nosotros volition run across how to recall ServletContext within Spring Controller class. We often demand ServletContext reference within Spring Controller course of didactics to larn a reference of Spring AppContext inwards companionship to recall edible bean defined within applicationContext.xml

Spring spider web application has split upwards applicaitonContext called WebApplicationContext. In companionship to larn this, yous demand a reference of the ServletContext object. Since every Controller inwards Spring has access to HttpRequest together with HttpResponse object, nosotros tin larn ServletContext from asking object, but asking object doesn't bring conduct access to ServletContext prior to Servlet Specification 3.0, thus yous demand to outset larn HttpSession object from asking together with 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 together with has Servlet 3.0 API yous tin conduct larn ServletContext object shape HttpServletRequest equally shown inwards below example:

ServletContext sc = request.getServletContext();


How to larn ServletContext object inwards JSP

In our lastly article on JSP nosotros bring seen how to define application broad fault page together with hither nosotros volition run across how to larn the Servletcontext object inwards JSP. This is too 1 of often asked JSP interview questions generally for freshers together with 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 together with yous tin conduct access it using "application" implicit object within whatever scriptlet or expression. You tin fifty-fifty access variable stored within ServletContext yesteryear using seem 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 role JSTL meat laid upwards tag  for putting variables together with beans on application reach defined yesteryear the ServletContext object.

How to larn ServletContext object within Servlet:

This query is too often asked inwards diverse Java Servlet interviews along alongside other J2EE technology. You tin easily larn a reference of ServletContext within your Servlet inwards the same means nosotros bring accessed ServletContext inwards our restrain Controller example. Until yous bring access of ServletRequest or HttpServletRequest yous tin larn session object from it together with and so yous tin larn a reference of ServletContext. An instance is precisely similar to our before restrain 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 course of didactics Struts:

In our lastly article on Jakarta Struts nosotros bring seen Top 10 jakarta struts interview questions asked inwards J2EE interviews together with hither nosotros volition how to find ServletContext reference within Struts Action class. Similar approach of Spring Controller together with Servlet tin live used to larn ServletContext within Struts Action class. Since Struts Action course of didactics contains execute method which contains HttpServletRequest equally parameter, yous tin role HttpServletRequest for getting ServletContext simply similar higher upwards 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 {

    /* frontward 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, together with JSP. You run across 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 live used to conduct access ServletContext from the asking object.

Further Learning
Spring Framework 5: Beginner to Guru
Difference betwixt include directive together with include activeness inwards JSP



Sumber https://javarevisited.blogspot.com/

0 Response to "How To Snuff It Servletcontext Inwards Servlet, Jsp, Activity Class Too Controller."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel