Amazon

Monday, June 8, 2009

SCWCD Notes - Web App Deployment

Tag Files- Must be deployed in WEB-INF/tags folder or subfolder. In case of jar it should be in META-INF/tags folder or subfolder.
If tag file is put anywhere else the container will recognise it as a static content.

Library Dependencies - In WAR file, META-INF/MANIFEST.MF is used to declare the library dependencies. It gives the deploy-time check for whether the container can find the packages and classes our app depends on.
Even if we dont put the META-INF directory in WAR, after deployment when the WAR is exploded, then META-INF and MANIFEST.MF is created inside that.

Server returns 404 Page not found error, if anything inside META-INF or WEB-INF is requested by client. But we can keep here active contents which can be included or forwarded by other jsp page, but client cannot request them directly.

Container will always looks for classes in WEB-INF/classes directry then it will go to WEB-INF/lib directory for jars.

classloader's getResource() and getResourceAsStream() methods can be used to access a resource (text file, JPEG etc) that's also inside a JAR. These methods are also inServletContext, but they work only for the resources which are not deployed in the jar file.

Servlet Mapping - <servlet-name>is mapped to <url-pattern>. <servlet-name>is the key and every request is come to <url-pattern>which picks the servlet associated with <servlet-name>
Three types of url pattern are their. The container looks for the matches in the given below order of match-pattern. First it chooses exact match, if its not found then opts for directory match and after al last it goes for extension match.

  1. Exact Match: Must begin with slash(/), may have an extension but not mandatory. e.g.<url-pattern>/beer/selectBeer.do
  2. Directory Match: Must begin with an slash(/). The directory can be real or virtual. It ends with slash(/) or *. e.g. <url-pattern>/beer/*.
  3. Extension Match: Must begin with an * (never with slash) . After the * it must have an dot(.) extension. e.g. <url-pattern>*.do.

Lastly the most specific match wins. for example request for /foo/bar/mystuff.do will also matc /foo/bar/* and /foo/*. But the first one will be selected because its more specific to the request.

Configuring Welcome files in DD: welcome files are default page, they appear when only name of website is given in URL without specifying any URL. For example entering <a href="http://www.yahoo.com/">http://www.yahoo.com/</a> will take to yahoo home page, here home page is welcome file of the yahoo web application.

The files in the <welcome-file>do not start with a slash. Multiple welcome files <welcome-file>go in a single DD element <welcome-file-list>.<welcome-file-list>
<web-app...>
<welcome-file>
<welcome-file-list>
<welcome-file>index.html </welcome-file>
<welcome-file>deault.jsp </welcome-file>
</welcome-file-list>
</web-app>

Now if the web-app is having diffenerent direcotries, we don't need to specify default html or jsp pages for them separately. All default pages will be specified in on place in <welcome-file-list>and container will choose first page matching to the request. For every request container first looks for servlet url mapping, if iti doesnot finds any match then opts to scan <welcome-file-list>

webapps/MyTestApp/index.html
webapps/MyTestApp/search/default.jsp
webapps/MyTestApp/registration/index.html
webapps/MyTestApp/registration/newMember/foo.txt

Request: <a href="http://localhost:8080/MyTestApp/">http://localhost:8080/MyTestApp/</a>
Container: Choice: MyTestApp/index.html
Request: <a href="http://localhost:8080/MyTestApp/registation">http://localhost:8080/MyTestApp/registation</a>
Container: Choice: MyTestApp/registration/index.html
Request: <a href="http://localhost:8080/MyTestApp/search">http://localhost:8080/MyTestApp/search</a>
Container: Choice: MyTestApp/search/default.jsp(if index.html and default.jsp both would be in search direcotry then container will take index.html)
Request: <a href="http://localhost:8080/registration/newMember">http://localhost:8080/registration/newMember</a>
Container: Choice: When no files from <welcome-file-list>is specified the behaviou r vendor specific. Tomcat shows directory list, another container may shoe 404 error.

Configuring Error Pages in DD: Used to display freindly message when any error/exception happens.

catch-all error page: applies to everything in web app- not just in jsps. Anything thatsThrowable can be declared in <exception-type>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorPage.jsp</location>
</error-page>
(this can be overridden in original jsp by adding a page directive with an errorPage attribute)
Explicit exception error page: for example, follwing error page will be called only when there's ArithmeticExcption, for all other exceptions te first error page will be called (Throwable)
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/arithmeticError.jsp</location>
</error-page>
HTTP statuc code error page: for example, following error page will come when the status code for the response is 404. HttpServletResponse.sendError(402) can also be used to send own response code and pull the pages from <error-page> tag.
<error-page>
<error-code>404</error-code>
<location>/arithmeticError.jsp</location>
</error-page>
response.sendError(HttpServletresponse.SC_FORBIDDEN); and response.sendError(403); are same.

Note: We cannot have both status code and exception type in single <error-page>tag.

</web-app>Configuring Servlet initialization in DD: Servlets are initialized at first request. So when the first client requests a servlet, class loading, instantiation and initalization happens of a servlet, before service() method can be invoked.

To load servlet on deploy time <load-on-startup> element is used in DD.

<servlet>
<servlet-name>one</servlet-name>
<servlet-class>com.sanjeev.One</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet>
<servlet-name>two</servlet-name>
<servlet-class>com.sanjeev.Two</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

Now the servelt one will be loaded first then servlet two will be loaded durig deployment. Any number greater than zero in <load-on-startup> element assists the servlet to be initailized during deployment.

Writing XML compilant JSP:

  1. <%@page import="java.util.*" %> --- <jsp:directive.page import="java.util.*" />
  2. <%! int y=3; %> ---- <jsp:declaration>int y=3;</jsp:declaration>
  3. <% list.add("Hoop"); > ---- <jsp:scriptlet>list.add("Hoop");</jsp:scriptlet>
  4. There is no spoon ---- <jsp:text>There is no spoon</jsp:text>
  5. <= it.next() %> ---- <jsp:expression> it.next() </jsp:expression >

Refering EJBs in web-app:

Local Beans : <ejb-local-ref>
<ejb-ref-name>ejb/Customer</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>com.java.customerHome</local-home>
<local>com.java.Customer</local>
</ejb-local-ref>

Remote Beans :<ejb-ref>
<ejb-ref-name>ejb/Customer</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>com.java.customerHome</home>
<remote>com.java.Customer</remote>
</ejb-ref>

Env Entry: <env-entry>
<env-entry-name>rates/discountRate</env-entry-name>
<env-entry-type>java.lang.Integer<env-entry-type>
<env-entry-value>10</env-entry-value>
</env-entry>

<env-entry-name>- This will be used in code to lookup entry.
<env-entry-type>- It can be any type that takes a single String as a constructor parameter or a single character if the type is java.lang.Character.
<env-entry-value>- It will be passed in as String or a single character if the is java.lang.Character.

Note that its not only primitive type or wrapper type. It can be any type which have single String as parameter constructor.

Configuring Mime type mapping: mapping between extension and the mime type.

<mime-mapping>
<extension>mpg</extension> <!-- do not include the dot "." -->
<mime-type>video/mpeg</mime-type>
</mime-mapping>

No comments:

Post a Comment

Amazon Best Sellors

TOGAF 9.2 - STUDY [ The Open Group Architecture Framework ] - Chap 01 - Introduction

100 Feet View of TOGAF  What is Enterprise? Collection of Organization that has common set of Goals. Enterprise has People - organized by co...