Java Web Applications web.xml Basic Fundamentals

0

Java Web Applications

      use a deployment descriptor file to determine how URLs map to servlets
      web.xml is the file is the deployment descriptor
      some URL’s require authentication
      resides in the WEB-INF folder
      describes the classes, resources and configuration of the application
      maps the URL of the request to the code that handles the request

    Example

    Below, the java class is mapped to the url’s here.

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
        <servlet>
            <servlet-name>comingsoon</servlet-name>
            <servlet-class>mysite.server.ComingSoonServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>comingsoon</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>
    

    Example

    Below, the web.xml defines key value pairs

     <servlet>
            <servlet-name>redteam</servlet-name>
            <servlet-class>mysite.server.TeamServlet</servlet-class>
            <init-param>
                <param-name>teamColor</param-name>
                <param-value>red</param-value>
            </init-param>
            <init-param>
                <param-name>bgColor</param-name>
                <param-value>#CC0000</param-value>
            </init-param>
        </servlet>
    String teamColor = getServletConfig().getInitParameter("teamColor");
    

    JSP

    JSP’s are servlets also :). That define static content mixed with Java code.

    Filters

    A filter is a class that acts on a request like a servlet, but may allow the handling of the request to continue with other filters or servlets. A filter may perform an auxiliary task such as logging, performing specialized authentication checks, or annotating the request or response objects before calling the servlet.

    A filter implements javax.servlet.Filter, includes doFilter() method.

    Error Handlers

      <error-page>
            <error-code>500</error-code>
            <location>/errors/servererror.jsp</location>
        </error-page>
    
    Advertisements