Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

ref: The AffableBean tutorial - creating the servlet controller

Package:
javax.servlet.annotation: The javax.servlet.annotation package contains a number of annotations that allow users to use annotations to declare servlets, filters, listeners and specify the metadata for the declared component.
 
Description:

Annotation used to declare a servlet.
This annotation is processed by the container at deployment time, and the corresponding servlet made available at the specified URL patterns.
This Annotation lets you define URLs patterns that invoke the servlet.

Annotation elements:

  • name: the name of the servlet resource;
  • loadOnStartUp: include the loadOnStartup element so that the servlet is instantiated and initialized when the application is deployed. A value of 0 or greater will cause this to happen (-1 is the default);
  • urlPatterns: the urls that this servlet will manage. For example, if you enter '/category', and your app is hosted has "http://localhost/AffableBean" you are directing the servlet to handle a request that appears as "http://localhost/AffableBean/category"; 
  • for other elements check the javadoc: javax.servlet.annotation#WebServlet

Ex.:
@WebServlet(name="ControllerServlet",
            loadOnStartup = 1,
            urlPatterns = {"/category",
                           "/addToCart",
                           "/viewCart",
                           "/updateCart",
                           "/checkout",
                           "/purchase",
                           "/chooseLanguage"})
public class ControllerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String userPath = request.getServletPath();

        // if category page is requested
        if (userPath.equals("/category")) {
            // TODO: Implement category request

        // if cart page is requested
        } else if (userPath.equals("/viewCart")) {
            // TODO: Implement cart page request

            userPath = "/cart";

        // if checkout page is requested
        } else if (userPath.equals("/checkout")) {
            // TODO: Implement checkout page request

        // if user switches language
        } else if (userPath.equals("/chooseLanguage")) {
            // TODO: Implement language request

        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
} 

XML alternative:
Instead of using the @Webservlet annotation you can also add this info in XML in the deployment descriptor (web.xml) of your project, ex:
<servlet>
    <servlet-name>ControllerServlet</servlet-name>
    <servlet-class>controller.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/category</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/addToCart</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/viewCart</url-pattern>
</servlet-mapping> 

Sem comentários:

Enviar um comentário