Servlet Container

1. What is a Web Server? To know what is a Servlet container, we need to know what is a Web Server first. A web server uses HTTP protocol to transfer data. In a simple situation, a user type in a URL (e.g. http://www.programcreek.com/static.html) in browser (a client), and get a web page to read….

threadsafe servlet

When the Servlet container starts, it: reads web.xml; finds the declared Servlets in the classpath; and loads and instantiates each Servlet only once. Roughly, like this: String urlPattern = parseWebXmlAndRetrieveServletUrlPattern(); String servletClass = parseWebXmlAndRetrieveServletClass(); HttpServlet servlet = (HttpServlet) Class.forName(servletClass).newInstance(); servlet.init(); servlets.put(urlPattern, servlet); // Similar to a map interface. Those Servlets are stored in memory and reused every…

thread-safe servlets

Servlets are not thread safe. If you want to make it Servlet as Thread safe, you can implement SingleThreadInterface which is a blank Interface there is no methods (this is not recomend method, because it could slow the performance of your page) or you can synchronize methods by using synchronized keyword. The SingleThreadModel interface is deprecated @…

filter 和 aop

现在AOP的设计开发理念在软件开发中用的越来越广泛,在我们开发的软件中也广泛进行了使用。而最常用的就是filter和interceptor。 Filter 该过滤器的方法是创建一个类XXXFilter实现此接口,并在该类中的doFilter方法中声明过滤规则,然后在配置文件web.xml中声明他所过滤的路径 <filter> <filter-name>XXXFilter</filter-name> <filter-class> com.web.util.XXXFilter </filter-class> </filter> <filter-mapping> <filter-name>XXXFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> Interceptor 该过滤器的方法也是创建一个类XXXInterceptor实现此接口,在该类中intercept方法写过滤规则,不过它过滤路径的方法和Filter不同,它与strut.xml结合使用(其他开发框架各自有自己的切入点), 创建一个strus.xml的子配置文件struts-l99-default.xml,它继承与struts2的struts-default,此配置文件是其他子配置文件的父类,只要是继承与该文件的配置文件所声明的路径都会被它过滤 如下 <package name=”XXX-default” namespace=”/” extends=”struts-default”> <interceptors> <interceptor name=”authentication” /> <interceptor-stack name=”user”> <interceptor-ref name=”defaultStack” /> <interceptor-ref name=”authentication” /> </interceptor-stack> <interceptor-stack name=”user-submit”> <interceptor-ref name=”user” /> <interceptor-ref name=”token” /> </interceptor-stack>   <interceptor-stack name=”guest”> <interceptor-ref name=”defaultStack” /> </interceptor-stack>   <interceptor-stack name=”guest-submit”> <interceptor-ref name=”defaultStack”…

JSP Servlet Filter

Basic Servlet Filters are the latest components that are added in Servlet 2.3 specifications. These filters are used basically for intercepting and modifying requests and response from server.  Consider a scenario where you want to check session from the every users request and if it is valid then only you want to let the user…