Tuesday, March 13, 2012
Web container
Web container (also known as a Servlet container) is the component of a web server that interacts with the servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights. A web container implements the web component contract of theJava EE architecture, specifying a runtime environment for web components that includes security, concurrency, lifecycle management,transaction, deployment, and other services
Sunday, March 11, 2012
Java IO
Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from
InputStream and OutputStream.InputStream.read();
OutputStream.write(c)
both are abstract classes.
FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt");Closing a stream when it's no longer needed is very important — so important thatCopyBytesuses afinallyblock to guarantee that both streams will be closed even if an error occurs. This practice helps avoid serious resource leaks.
all other stream types are built on byte streams. --------------All character stream classes are descended fromReaderandWriter.
Reader.read();
Writer.write(c)
Line-Oriented IO has to use bufferedReader and BufferedWriter
--
Above are unbuffered IO,This means each read or write request is handled directly by the underlying OS.
to reduce overhead, the Java platform implements buffered I/O streams.
using wrapping approach
inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));Decorator Design pattern!!!-----
Objects of type Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type. s = new Scanner(new BufferedReader(new FileReader("xanadu.txt"))); while (s.hasNext()) { System.out.println(s.next()); }s.useDelimiter(",\\s*");s.hasNextDouble();---Stream objects that implement formatting are instances of eitherPrintWriter, a character stream class, orPrintStream, a byte stream class.System.outis PrintStream
Decorator Pattern
Attaches additional responsibilities to an object dynamically. Decorators provides a flexible alternative to subclassing for extending functionality.
http://javapapers.com/design-patterns/decorator-pattern/
(abstract class) compontent{
m1();
m2();
}
concreteComponent{
m1();
m2();
}
(abstract) Decorator extends component{
m1();
m2();
}
you can use the decorator to decorate concreteComponent to concreteNewComponent with something additional
Design pattern book 95~98
Beverage beverage = new Espresso();
Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2); // double mocha
beverage2 = new Whip(beverage2); // with whip cream
HOW PL/SQL GETS EXECUTED
http://comsci.liu.edu/~vasilaky/db2/lec1.htm
the PL/SQL engine on the server where it is compiled. The named PL/SQL
block is compiled only at the time of its creation, or if it has been
changed. The compilation process includes syntax checking, binding,
and p-code generation.
Oracle guarantees a read-consistent view
of the data. Until that point, all data that has been inserted or updated
will be held in memory and only be available to the current user. The
rows that have been changed will be locked by the current user and will
not be available for updating to other users until the locks have been re-leased.
the PL/SQL engine on the server where it is compiled. The named PL/SQL
block is compiled only at the time of its creation, or if it has been
changed. The compilation process includes syntax checking, binding,
and p-code generation.
Oracle guarantees a read-consistent view
of the data. Until that point, all data that has been inserted or updated
will be held in memory and only be available to the current user. The
rows that have been changed will be locked by the current user and will
not be available for updating to other users until the locks have been re-leased.
Saturday, March 10, 2012
Difference between web server and app server
http://www.javaworld.com/javaworld/javaqa/2002-08/01-qa-0823-appvswebserver.html
A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.
A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.
Wednesday, March 7, 2012
Monday, March 5, 2012
Servlet basic
SampleServlet extends HttpServlet{
init();
service(httprequest, httpresponse);
}
server engine call service() to handle request, return response.
when the servlet first loaded, init() is called once for initialization
after service() is done, servlet waits till next request
the server can decide to unload this servlet by calling destroy(), you want to
release all the shared resources that were created in init()
two packages
javax.servlet
javax.servlet.http
HttpServlet class methods
service()--serve all requests,defaut implementation is calling doGet, doPost,doDelete,doPut in response to http request
doHeader() defaut calling doGet(), chop off body, only return header
Thread safe Servlet
An application is thread safe if it always behaves predictablly regardless of the number of concurrent threads running in this
process space.
Synchronization in java is the process of locking a variable so that it can be read and modified by only a single thread at a time.
The synchronized keyword eliminates race conditions by locking variables for a specific duration
synchronized(this){
...
..
}
it is possible to synchronize the entire service() method rather than blocks of code, in this way, only on thread at a time
can execute the method. the more code is synchronized, the greater the performance penalty. To aoid this penalty,minimie the amount
of code.
a new servlet object is not instantiated for each new client request, Rather, a single servlet usually services all requests.
This behavior explains how instance variables can often be shared
Because every call to service(), doget(), doPost() is executed in its own thread, variables local to these methods are not shared between
requests and are automatically thread safe.
public class AServlet extends HttpServlet
{
//instance variable
String UserName; // not thread safe
}
public class BServlet extends HttpServlet
{
//instance variable
String userName; // not thread safe
public void service(){
String userName; //thread safe
}
or use SingleThreadModel interface
public class CServlet extends HttpServlet implements SingleThreadModel
{
//instance variable
String userName; // thread safe
}
If servlet implemnts the SinglethreadModel interface, the server guarantees that no more than on thread can execute the service,
doGet, doPost method at a time for a particular servlet instance. this gurantees thread safe.
Http redirects
An Http redirect is a set of instructions included in the header of an Http response that instruct the browser
to issue a new request to a new URL.
An HTTP redirect response uses an HTTP status code of either '301 Moved Permanently" or "302 Moved Temporarily" accompanied
by a Location header containing a valid URL.the browser receives an Http response with status code 301,302,it will
immediately request the resource at the URL designated by the Location Header
sample redirect response
HTTP/1.1 301 Moved Permanently
..
Location:http://www..../....html
sending a redirect with the servlet API
two ways:
service(HttpServletRequest request,HttpServletResponse response){
..
reponse.status(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", someurl);
}
or
response.setRedirect(url); //this way only set 302, url has to be full qualified url
Subscribe to:
Posts (Atom)