I do lots of unix shell scripting. It’s easy and I can get things done without hassle.
Okay, now this one is maybe easy. But I would like to call a subroutine defined in the library script file, let’s say “lib.inc”.

lib.inc

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
 
# This is our real simple sub routine returns 0 value when it is happy
is_happy ()
{
     #First arg $1, Second arg  $2, third $3 ... so on.
     if [ "$1" = ":)" ] ; then
     	return 0
     else
     	return 1
     fi
}

You can include the lib.inc by using “.” notation.
When you call the sub routine , call it just like you execute an external command. And the return value can be obtained using $?. Just like getting the return value of external command after execution.

This is main script calling the is_happy().

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
#including the lib.inc file
. lib.inc
 
#calling subroutine
is_happy ":)"
 
if [ "$?" -eq 0 ] ; then
        echo "I am happy!"
else
        echo "No I am not happy!"
fi

That was easy right? There will be more fun on shell scripting at my Coding Haven soon.

Finally after about a month of certification preparation, I took the test today.

But I was lost while getting to the test site.

So here is my lesson , be fully prepared :)

And I did pass the test with the average score.

By the way the passing grade is 70.

Good Luck if you plan to take the test. Try to solve mock exams as many as possible. They do help.

Chapter 13 Filters and wrappers

Filters can intercept request before servlet and/or process reponse after servlet is completed.
Filters can be chained with other filters.
You must implement all methods : init() , doFilter() , destroy().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import javax.servlet.* ; // Filter and FilterChain are located here
public class BeerRequestFilter implements Filter {
	private FilterConfig fc;
 
	//You must implement init & destroy methods
	public void init (FilterConfig config) throws ServletException {
		this.fc=config;
	}
	// doFilter args are not HttpServletRequest and HttpServletResponse.
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws
			ServletException, IOException {
		HttpServletRequest httpReq = (HttpServletRequest) req; 
		// This casting is possible because the req is in fact HttpServletRequest!
 
		String name = httpReq.getRemoteUser();
		if(name != null) {
			fc.getServletContext().log("User " + name + " is updating" );
		}
		chain.doFilter(req.resp); //calling next filter
	}
	//You are required to implement destroy()
	Public void destroy(){
	}
}

Read the rest of this entry »