Servlet Filter

- Filter help to block a malicious request if they are not allowed to access a backend resource.
- Filter also help to manipulate the responses that will be sent back to the client.

Different types of filters:
  1. Authentication-Blocking: Block or allow any requests based on user identity.
  2. Logging and auditing: Helps in user tracking.
  3. Image conversion: Helps with scaling maps
  4. Data compression: Reduces download size
  5. Localization: Helps support the code in various countries based on different languages and formatting. 
  6. Encryption filters
  7. Tokenizing filters
  8. Mime-type chaining filters
  9. Caching filters
  10. XSL/T Filters: Helps transforming XML requests.

- Filters are specified in web.xml and are executed in the order specified in the file.
- Filter java class implements javax.servlet.Filter and implements these 3 methods: init(), doFilter(), destroy()

import javax.servlet.*;
import java.util.logging.Filter;

public class TestFilter implements Filter {
    public void  init(FilterConfig config) throws ServletException {
        //Get and print init param                
        String testParam = config.getInitParameter("param1");
        System.out.println("Test Param: " + testParam);
    }

    public void  doFilter(ServletRequest request,
                          ServletResponse response,
                          FilterChain chain)
            throws java.io.IOException, ServletException {

        // Get the method of the request
        String method = request.getMethod();
        logger.info("Request Method is: ", method);

        // Pass request back down the filter chain
        chain.doFilter(request,response);
    }
    public void destroy( ){
        //close any resources here        }
}

Compile TestFilter.java in usual way and put your class file in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes

Make following config change in the web.xml
<filter>
    <filter-name>TestFilter</filter-name>
    <filter-class>TestFilter</filter-class>
    <init-param>
        <param-name>param1</param-name>
        <param-value>Test</param-value>
    </init-param>
</filter>
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

The TestFilter will apply to all the servlets since we specified url-pattern as /*. For example if we want to apply TestFilter to only a class called as TestFile then the config will look like
<filter>
    <filter-name>TestFilter</filter-name>
    <filter-class>TestFilter</filter-class>
    <init-param>
        <param-name>param1</param-name>
        <param-value>Test</param-value>
    </init-param>
</filter>
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-pattern>TestFile</url-pattern>
</filter-mapping>

Multiple filters
<filter>
    <filter-name>TestFilter</filter-name>
    <filter-class>TestFilter</filter-class>
    <init-param>
        <param-name>param1</param-name>
        <param-value>Value1</param-value>
    </init-param>
</filter>

<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>TestFilter2</filter-name>
<filter-class>TestFilter2</filter-class>
<init-param>
    <param-name>param</param-name>
    <param-value>Value</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>TestFilter2</filter-name>
<url-pattern>TestFile</url-pattern>
</filter-mapping>
In this example, TestFilter will be applied followed by TestFilter2.

You might also like:
Java Concepts
Java String concepts
Immutable classes in Java
Remove duplicates from the array
Telephonic phone technical interview questions
Telephonic phone technical interview questions - Part 2
Serialization & Deserialization
Jersey Package Dependencies
Observable and Observer Interface
Servlet Client Request
Spring dependencies for Maven
Java String Operations

Mercurial Commands CheatSheet

Mercurial Commands:
  1. Merging
    1. hg update default
    2. hg pull
    3. hg update
    4. hg merge branch_name
    5. hg commit 
    6. hg push
  2. Creating a new branch
    1. hg branch branch_name
    2. hg commit -m “Creating a new branch” 
    3. hg push - - new-branch
  3. Post a review on one of the published reviews
    1. hg postreview -i {repositoryId} -e {CodeReviewId}
  4. To rollback a merge:
    1. hg update -C . 
  5. Clone a repository:
    1. hg clone ssh://merc//home/hg/RepoName
  6. Undo a merge:
    1. hg update -C -r .

Jersey Package dependencies

Dependencies:
Package: Server side support for Grizzly Containers
<dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-grizzly2-http</artifactId>
   <version>2.22.2</version>
</dependency>

Package: Server side support for JDK Containers
<dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-jdk-http</artifactId>
   <version>2.22.2</version>
</dependency>


Package: Server side support for Simple Containers
<dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-simple-http</artifactId>
   <version>2.22.2</version>
</dependency>

Package: Server side support for Jetty Containers
<dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-jetty-http</artifactId>
   <version>2.22.2</version>
</dependency>
<dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-jetty-servlet</artifactId>
   <version>2.22.2</version>
</dependency>

Package: Grizzly connector for Jersey Client
<dependency>
   <groupId>org.glassfish.jersey.connectors</groupId>
   <artifactId>jersey-grizzly-connector</artifactId>
   <version>2.22.2</version>
</dependency>
 

Package: Apache connector for Jersey Client
<dependency>
   <groupId>org.glassfish.jersey.connectors</groupId>
   <artifactId>jersey-apache-connector</artifactId>
   <version>2.22.2</version>
</dependency>

Package: Jetty connector for Jersey Client
<dependency>
   <groupId>org.glassfish.jersey.connectors</groupId>
   <artifactId>jersey-jetty-connector</artifactId>
   <version>2.22.2</version>
</dependency>


Package: Jersey
<dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-servlet</artifactId>
   <version>2.22.2</version>
   <scope>provided</scope>
</dependency>
<!-- if you are using Jersey client specific features without the server side -->
<dependency>
   <groupId>org.glassfish.jersey.core</groupId>
   <artifactId>jersey-client</artifactId>
   <version>2.22.2</version>
   <scope>provided</scope>
</dependency>

Package: JAX-RS API
<dependency>
   <groupId>javax.ws.rs</groupId>
   <artifactId>javax.ws.rs-api</artifactId>
   <version>2.0.1</version>
   <scope>provided</scope>
</dependency>

Apache Commons dependency
<!-- http://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>


You might also like:
Java Concepts
Java String concepts
Immutable classes in Java
Remove duplicates from the array
Telephonic phone technical interview questions
Telephonic phone technical interview questions - Part 2
Serialization & Deserialization
Observable and Observer Interface
Servlet Filter
Servlet Client Request
Spring dependencies for Maven
Java String Operations

Observable and Observer interface


  1. Observable is a class.
  2. Observer is an interface.
  3. Observable class maintains a list of observers.
  4. If something changes in Observable then it will notify all its observers.

 
import java.util.Observable;
import java.util.Observer;

class NewPosts extends Observable {
  private String post;

  public String getPost() {
    return post;
  }

  public void updatePost(String post) {
    this.post = post;
    setChanged();
    notifyObservers(post);
  }

  public static void main(String[] args) {
    NewPosts posts = new NewPosts();
    User user1 = new User();
    User user2 = new User();
    posts.addObserver(user1);
    posts.addObserver(user2);
    board.updatePost("New Post is available");
  }
}

class User implements Observer {
  public void update(Observable o, Object arg) {
    System.out.println("New Post available: " + arg);
  }
}


In the above example, a user registers by adding himself to the list of observers. When a new post is available, the user will be notified. Similarly, when a new post is available the observable updates all the users in the observers list.

You might also like:
Java Concepts
Java String concepts
Immutable classes in Java
Remove duplicates from the array
Telephonic phone technical interview questions
Telephonic phone technical interview questions - Part 2
Serialization & Deserialization
Jersey Package Dependencies
Servlet Filter
Servlet Client Request
Spring dependencies for Maven
Java String Operations

CSS Properties

CSS has so many different properties that it is confusing to keep a track of it. Most of the tutorials available online are only useful if you try the examples. This blogspot is a cheatsheet of some of the CSS properties. List is long so I am attempting to cover only a few. If you like it, please comment down below and I will make more blogposts like these

Color:
It will change the color of the text in the tag. For ex:
p {
color: blue;
}
This will change the text color to blue in all paragraphs.

Table: 
It will help you to add table to the page.

  • Border-collapse: If you decide to collapse the borders of the table then you should use border-collapse property.
    • border-collapse can take up following values: 
      • initial - Initial will remove the set value and revert it to the default.
      • inherit - Inherit will take this property from its parent element.
      • collapse - Collapse will collapse the double edged border to single property. 
      • separate - Separate will separate the borders. This is the default value. 
      •  For ex: table { border-collapse: separate; }
  • Border-spacing: If you want to put some space between different table cells then you should use border-spacing property. 
    • border-spacing can take up following values: length, initial, inherit. 
    • length can be specified in px, cm, For ex:
    • For ex: table { border-spacing: 10px, 20px }
      • 10px will set the horizontal spacing 20px will set the vertical spacing. 
  • Caption: Tables are always captioned. So you can change the alignment of the captions to top, bottom, or inherit it or change it to initial value. 
    • For ex: caption { caption-side: bottom; }
  • Empty-Cells: If you want to hide or show the empty cells in a table then you can use the property of table called as empty-cells. 
    • For ex: table { empty-cells: hide; }
    • In above example, the values can be changed to hide, show, initial, or inherit. 
    • The default value is show. 
  • Table-Layout: 
    • Sometimes it happens that in a table you have some content that is too big to fit in its cells. You can expand the table width as much as you like but there will surely be a situation, where your table content doesn't fit in fully. 
    • You can fix it by make the table-layout property to auto. It will handle the situation, where you have unknown length of content. 
    • It can take following values: fixed, auto, inherit, initial
    • For ex: table { table-layout: fixed; }

Hope you liked this article. If you want to read more of these then please comment down below. I will be happy to write similar articles if that helps! 

NoSQL

NoSQL Data models: key-value  Aggregate model.  key or id is used to get the data.  Lookup is based on the key.   document Aggre...