MVC for ServletsMVCAdvantages of MVCMVC for servletsWeb applicationsweb.xml servletServlet life-cycle methodsServletConfigServlet init parametersweb.xml entire web applicationMultiple servletsServlet vs. context init parametersPublic ServletContext methodsservlet JSPThe ServletRequest objectDispatching to the JSPAside: redirect vs. forwardAttributesParameters are not attributesAttribute scopesAttribute methodsThread safetyThread safety in servletsThread safety in class assignmentsProtecting context attributesProtecting session attributesGetting init parameters in JSPPageContextThe EndJan 14, 2019MVC for Servlets2MVCOne of the most common Design Patterns is Model-View-Controller (MVC)The model does all the computational workIt is input/output freeAll communication with the model is via methodsThe controller tells the model what to doUser input goes to the controllerThe view shows results; it is a “window” into the modelThe view can get results from the controller, orThe view can get results directly from the model3Advantages of MVCOne advantage is separation of concernsComputation is not intermixed with I/OConsequently, code is cleaner and easier to understandAnother advantage is flexibilityThe GUI (if one is used) can be completely revamped without touching the model in any wayAnother big advantage is reusabilityThe same model used for a servlet can equally well be used for an application or an applet (or by another process)MVC is widely used and recommended4MVC for servletsThe model, as usual, does all the computational work, and no I/OThe model can consist of multiple classesThe servlet class (the one that extends HttpServlet) acts as the control l erThe servlet gives any relevant information from the user request to the modelThe servlet takes the results and passes them on to the viewThe view—that is, the HTML page that is returned to the user—is frequently created by JSP5Web applicationsA web application typically consists of:Some (Java) class, acting as the controller, that extends HttpServletThe model code (also Java)The view code (ultimately Java, but we write it as JSP) Plus, of course, the web.xml fileAll these parts need to communicate with one anotherThat’s what the rest of this lecture is (mostly) aboutJan 14, 2019web.xml servlet7Servlet life-cycle methods public void init()Called after the servlet is constructed but before the servlet is placed into serviceAs the name implies, a good place to do initializationspublic void service(ServletRequest request, ServletResponse response)Called when a servlet request is madeThe HttpServlet service method will dispatch the request to doGet, doPost, or one of the other service methodspublic void destroy()Called when a servlet is terminatedCan be used to clean up any resources (files, databases, threads, etc.)8ServletConfigYou can override public void init()Servlet has the methods:public ServletConfig getServletConfig()You will probably use this if you override init()public String getServletInfo()By default, returns an empty string; override to make it usefulThe main purpose of ServletConfig is to provide initialization information to the servletServletConfig has these methods:public java.lang.String getServletName()public ServletContext getServletContext() public Enumeration getInitParameterNames() public String getInitParameter(String'name)Our interest will be in getting initialization parameters9Servlet init parametersWhere does a servlet get its initialization information?From the web.xml file, of course!Inside <servlet>:<init-param> <param-name>myName</param-name> <param-value>myValue</param-value></init-param>In the servlet code:String myValue = getServletConfig().getInitParameter("myName");Jan 14, 2019web.xml entire web application11Multiple servletsA web application can consist of multiple servletsWe just saw how to send configuration information to a single servletContext init parameters can send configuration information to all servlets in a web applicationNot inside a particular <servlet> tag:<context-param> <param-name>myName</param-name> <param-value>myValue</param-value></context-param>In any servlet:String myValue = getServletContext().getInitParameter("myName");12Servlet vs. context init parametersServlet init parameters are:Defined within a <servlet> tagWritten within an<init-param> tagRetrieved from a ServletConfig object, which you get by calling getServletConfig()Read from the ServletConfig object by calling getInitParameter(name )Context init parameters are:Defined outside all <servlet> tagsWritten within a<context-param> tagRetrieved from a ServletContext object, which you get by calling getServletContext()Read from the ServletContext object by calling getInitParameter(name)13Public ServletContext methodsString getInitParameter(String'name) Enumeration getInitParameterNames() Object getAttribute(String'name) Enumeration getAttributeNames() void setAttribute(String'name, Object'object) void removeAttribute(String'name) String getRealPath(String'path ) RequestDispatcher getRequestDispatcher(String path)Jan 14, 2019servlet JSP15The ServletRequest objectYou’ve seen these methods of the ServletRequest object:public Enumeration getParameterNames()public String getParameter(String name)public String[] getParameterValues(String name) ServletRequest also has these methods:public Enumeration getAttributeNames()public Object getAttribute(String name)public void setAttribute(String n ame, Object object)You can use attributes to send information to the JSP16Dispatching to the JSPrequest.setAttribute(name, object)Notice that we put the information on the requestRequestDispatcher view = request.getRequestDispatcher("result.jsp");We ask the request object for a dispatcherWe supply, as a String, a path to the JSP fileIf the path begins with a slash, it is relative to the current context rootOtherwise, it is relative to the servlet locationview.forward(request, response);Having added the result information to the HttpRequest object, we forward the whole thing to the JSPThe JSP does the rest—it will send out the HTML page17Aside: redirect vs. forwardThe previous slide
View Full Document