MVC for Servlets Jan 13 2019 MVC One of the most common Design Patterns is ModelView Controller MVC The model does all the computational work The controller tells the model what to do It is input output free All communication with the model is via methods User input goes to the controller The view shows results it is a window into the model The view can get results from the controller or The view can get results directly from the model 2 Advantages of MVC One advantage is separation of concerns Another advantage is flexibility The GUI if one is used can be completely revamped without touching the model in any way Another big advantage is reusability Computation is not intermixed with I O Consequently code is cleaner and easier to understand The 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 recommended 3 MVC for servlets The model as usual does all the computational work and no I O The servlet class the one that extends HttpServlet acts as the controller The model can consist of multiple classes The servlet gives any relevant information from the user request to the model The servlet takes the results and passes them on to the view The view that is the HTML page that is returned to the user is frequently created by JSP 4 Web applications A web application typically consists of Some Java class acting as the controller that extends HttpServlet The model code also Java The view code ultimately Java but we write it as JSP Plus of course the web xml file All these parts need to communicate with one another That s what the rest of this lecture is mostly about 5 web xml servlet Jan 13 2019 Servlet life cycle methods public void init public void service ServletRequest request ServletResponse response Called after the servlet is constructed but before the servlet is placed into service As the name implies a good place to do initializations Called when a servlet request is made The HttpServlet service method will dispatch the request to doGet doPost or one of the other service methods public void destroy Called when a servlet is terminated Can be used to clean up any resources files databases threads etc 7 ServletConfig You 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 useful The main purpose of ServletConfig is to provide initialization information to the servlet ServletConfig has these methods public public public public java lang String getServletName ServletContext getServletContext Enumeration getInitParameterNames String getInitParameter String name Our interest will be in getting initialization parameters 8 Servlet init parameters Where does a servlet get its initialization information Inside servlet From the web xml file of course init param param name myName param name param value myValue param value init param In the servlet code String myValue getServletConfig getInitParameter myName 9 web xml entire web application Jan 13 2019 Multiple servlets A web application can consist of multiple servlets We just saw how to send configuration information to a single servlet Context init parameters can send configuration information to all servlets in a web application Not 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 11 Servlet vs context init parameters Servlet init parameters are Defined within a servlet tag Written within an init param tag Retrieved 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 tags Written within a context param tag Retrieved from a ServletContext object which you get by calling getServletContext Read from the ServletContext object by calling getInitParameter name 12 Public ServletContext methods String 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 13 servlet JSP Jan 13 2019 The ServletRequest object You ve seen these methods of the ServletRequest object ServletRequest also has these methods public Enumeration getParameterNames public String getParameter String name public String getParameterValues String name public Enumeration getAttributeNames public Object getAttribute String name public void setAttribute String name Object object You can use attributes to send information to the JSP 15 Dispatching to the JSP request setAttribute name object Notice that we put the information on the request RequestDispatcher view request getRequestDispatcher result jsp We ask the request object for a dispatcher We supply as a String a path to the JSP file If the path begins with a slash it is relative to the current context root Otherwise it is relative to the servlet location view forward request response Having added the result information to the HttpRequest object we forward the whole thing to the JSP The JSP does the rest it will send out the HTML page 16 Aside redirect vs forward The previous slide showed how a servlet could forward a request to JSP or to another servlet This is all done on the server side response sendRedirect URL sends a response back to the browser that says in effect I can t handle this request you should go to this URL instead You cannot use this method if you have already written something to the response The URL can be relative to the location of this servlet 17 Attributes Jan 13 2019 Parameters are not attributes You can get parameters from the Deployment Descriptor You cannot set these parameters You can get request parameters getServletConfig getInitParameter name getServletContext getInitParameter name request getParameter String name Parameter values are always Strings Attribute values are always Objects When you get an attribute you have to cast it to the type you want 19 Attribute scopes Servlets can access three scopes Application scope Session scope All servlets in the web application have access Attributes are stored in the ServletContext object Available for
View Full Document