Singleton Design Pattern – Struts Tutorial

Singleton Java Class:-

  • In java class that allows creating only one object per JVM is called as “singleton java class”. Instead of creating multiple objects per a class on a single JVM having same data. It is recommended to create only one object for that class and use it for multiple times by taking support of singleton java class.
  • Every servlet program is a single instance multiple threads component. That means when multiple request are given to a servlet program the servlet container creates only one object of our servlet program and starts multiple threads on that object requesting multiple requests. 

Question: every servlet is a single instance multiple threads component. Action servlet is also servlet then what is the need of giving Action Servlet class as singleton java class?

  1. Some servers violates above said servlet specification principle by creating multiple objects for single servlet class when that servlet gets huge number of request parallel or concurrently. (300+)
  • When struts application is deployed in this kind of servers to see only one object for Action Servlet the creators of struts s/w have made Action Servlet as singleton java class even though Action Servlet gets huge number of requests simultaneously or concurrently.

Note: – Our normal servlet classes are not singleton java classes but ActionServlet is explicitly design singleton java class based Servlet program.

  • Every Servlet program of web application will be identified through its url-pattern there are three ways to provide url-pattern to a servlet program.
  1. Exact match
  2. Directory match
  3. Extension match

Exact match:-

This url-pattern must begin with “/” symbol and should not contain “*” character. Multiple words or multiple letters can be there separated with “/” symbol.

1)      <url-pattern> / abc</url pattern>

2)      <url-pattern>/ abc/xyz</url-pattern>

3)      <url-pattern>/</url-pattern>

  • Examples request urls from browser window to given request to a servlet

Program whose url pattern is /abc.

http:// localhost: 2020/Test App/abc is valid.

                                                                     /Test App/xyz/abc is invalid.

                                                                     /Test App/xyz is invalid.

                                                                     /Test App/ is invalid.

Directory match:-

Example: – <url-pattern>/x/y/*</url-pattern>

  • This url-pattern must begin ’/’ symbol and must end with ‘*’ symbol.

Example: – Request urls from browser window to give request to a servlet program whose url pattern is “/x/y/*”.                  

                         http://localhost:2020/Test App/x/y/abc is valid

                                                                      /Test App/x/y/123/* is valid  

                                                                      /Test App/y/x/x/y/abc is invalid

                                                                      /Test App/x/y is valid

                                                                      /Test App/x is invalid

                                                                      /Test App/y/x is invalid

Other example directory match url-pattern:-

1)      <url-pattern> /xyz/abc/*</url-pattern>

2)      <url-pattern> /xyz/*</url-pattern>

3)      <url-pattern> /x/y/abc/*</url-pattern>

Extension match: –   this <url-pattern> must begin with “*” symbol and must end with extension word or letter.

Example: – <url-pattern> *.do</url-pattern>


Example request urls from browser window to give request to servlet program url pattern is “*.do”

             http://localhost:2020/TestApp/ is valid

                                                    /TestApp/a/b/ is valid

                                                    /TestApp/x/a/ is valid

                                                    /TestApp/.do is valid

                                                    /TestApp/ is invalid

                                                    /TestApp/ is invalid

Other examples of extension match url-patterns:

Example: –

1)      <url-pattern> *.c</url-pattern>

2)      <url-pattern> *.cpp</url-pattern>

3)      <url-pattern> *.xyz</url-pattern>

Note: – you cannot frame <u-p> to a servlet by mixing up multiple styles.

Example: – <u-p> /x/ y/*.do </u-p>

         The url-pattern formation itself invalid in web.xml file.bc3 the above url-pattern is not according to above specified styles.

Leave a Reply

Your email address will not be published. Required fields are marked *