DOC PREVIEW
Penn CIT 597 - MVC for Servlets

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MVC for ServletsMVCMVC 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 Servlets2MVCOne of the most common Design Patterns is Model-View-Controller (MVC)The model does all the computational workIt is input/output freeAll communication with the model is via methodsThe controller tells the model what to do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 model3MVC for servletsThe model, as usual, does all the computational work, and no I/OThe model can consist of multiple classesThe servlet class (the one that extends HttpServlet) acts as the control l er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 JSP4Web 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) aboutJan 14, 2019web.xml  servlet6Servlet life-cycle methods public void init()Called after the servlet is constructed but before the servlet is placed into serviceAs the name implies, a good place to do initializationspublic void service(ServletRequest request, ServletResponse response)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.)7ServletConfig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 java.lang.String getServletName()public ServletContext getServletContext() public Enumeration getInitParameterNames() public String getInitParameter(String'name)Our interest will be in getting initialization parameters8Servlet init parametersWhere 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 application10Multiple 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");11Servlet 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)12Public 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)Jan 14, 2019servlet  JSP14The ServletRequest objectYou’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 JSP15Dispatching 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 page16Aside: 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 servletJan 14, 2019Attributes18Parameters are not attributesYou can get parameters


View Full Document

Penn CIT 597 - MVC for Servlets

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download MVC for Servlets
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MVC for Servlets and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MVC for Servlets 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?