Sunday, November 4, 2012

Java Web Server


How Java Web Servers Work


Servlet Overview

Servlets and the Servlet Container

Unlike a Java client program, a servlet has no static main() method. Therefore, a servlet must execute under the control of an external container.
Servlet containers, sometimes referred to as servlet engines, execute and manage servlets.

Adding JDBC lib/Framework into project


17.1. Adding a library (.jar) to your project

The following describes how to add Java libraries to your project. Java libraries are distributed via "jar" files. It assumes that you have a jar file available; if not feel free to skip this step.
Create a new Java project de.vogella.eclipse.ide.jars. Then, create a new folder called lib, by right clicking on your project and selecting New → Folder.
Creating a new folder
From the menu select File → Import → General → File System. Select your jar and select the lib folder as target. Alternatively, just copy and paste your jar file into the lib folder.

Right click on your project and select Properties. Under Java Build Path → Libraries select the Add JARs button.
The following example shows how the result would look like, if the junit-4.4.jar file had been added to the project.

(Or simply add External JARs - one step)
Adding a jar to the current project
Afterwards you can use the classes contained in the jar file in your Java source code.

Adding JDBC lib into project


没有import jdbc lib会出现classNotFoundException

MySQL Command Line Client




Add the lib into your project: EX: Servlet framework; JDBC Driver
  • Right-click the project, click Properties.
  • Choose Java Build Path
  • Click Add JARs...  
  • Find servlet-api.jar in your project and select it.
  • Click OK to update the build path.

Saturday, November 3, 2012

Running a Java Program from Command Prompt

http://www.skylit.com/javamethods/faqs/javaindos.html

Eclipse has its own compiler and can run on a non-JDK JRE. So, I gotta download JDK by myself.

Run Command Prompt (found under All Programs/Accessories in the Start menu).  Type
C:\> cd \mywork
This makes C:\mywork the current directory.
C:\mywork> dir
This displays the directory contents.  You should see HelloWorld.java among the files.
C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
This tells the system where to find JDK programs.
C:\mywork> javac HelloWorld.java
This runs javac.exe, the compiler.  You should see nothing but the next system prompt...
C:\mywork> dir
javac has created the HelloWorld.class file.  You should see HelloWorld.java andHelloWorld.class among the files.
C:\mywork> java HelloWorld
This runs the Java interpreter.  You should see the program output:
Hello, World!


My java path:
C:\Program Files\Java\jdk1.7.0_02\bin

* The compiler is smart. It can detect the relation of class. So, just javac the .java with main() and then it will compile the related .java

(computer) port

http://compnetworking.about.com/od/basiccomputerarchitecture/g/computer-ports.htm


Definition: In computer networking, the term port can refer to either physical or virtual connection points.
Physical network ports allow connecting cables to computers, routers, modems and other peripheral devices. Several different types of physical ports available on computer network hardware include:
Virtual ports are part of TCP/IP networking. These ports allow software applications to share hardware resources without interfering with each other. Computers and routers automatically manage network traffic traveling via their virtual ports. Network firewalls additionally provide some control over the flow of traffic on each virtual port for security purposes.

BufferedReader


A BufferedReader is a simple class meant to efficiently read from the underling stream. Generally, each read request made of a Reader like a FileReader causes a corresponding read request to be made to underlying stream. Each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Efficiency is improved appreciably if a Reader is warped in a BufferedReader.
BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads.
A scanner on the other hand has a lot more cheese built into it; it can do all that a BufferedReader can do and at the same level of efficiency as well. However, in addition a Scanner can parse the underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice. It can also do forward scanning of the underlying stream disregarding the delimiter!
A scanner however is not thread safe, it has to be externally synchronized.
The choice of using a BufferedReader or a Scanner depends on the code you are writing, if you are writing a simple log reader Buffered reader is adequate. However if you are writing an XML parser Scanner is the more natural choice.
Even while reading the input, if want to accept user input line by line and say just add it to a file, a BufferedReader is good enough. On the other hand if you want to accept user input as a command with multiple options, and then intend to perform different operations based on the command and options specified, a Scanner will suit better.

Thursday, November 1, 2012

Java variables

Cup Size -- a story about variables

1.In Java, objects are created on the heap 
 In Java, if you want to stick an object in a variable, remember that the object is created out on the garbage-collectible heap. Always. 

2. References
References store a value which the Java Virtual Machine (JVM) uses to get to your object. It sure looks and feels a lot like a pointer, and it might very well be a pointer to a pointer, or...
You Can't Know. It's an implementation detail that you, as a programmer, can't access. Don't even think about it. There's no way to use that value other than to access the methods and variables of the actual object the reference refers to. That's part of what makes Java safer than C/C++. You can not go directly to any arbitrary memory location. The JVM allocates memory on your behalf, for your object, and stores an address-like thing in the reference cup (which is most likely a 32-bit cup, but not guaranteed to be).


Pass-by-Value Please (Cup Size continued)

How does pass-by-value work with references?
Java passes everything by value.
With primitives, you get a copy of the contents. 
With references you get a copy of the contents.
When you pass an object reference into a method, you are passing a COPY of the REFERENCE.  A clone of the remote control. 
The object is still sitting out there, waiting for someone to use a remote.The object doesn't care how many remotes are "programmed" to control it. Only the garbage collector cares (and you, the programmer).

Java Applet / Plug-in

Java Applet就是用Java语言编写的小应用程序,可以直接嵌入到网页中,并能够产生特殊的效果.

1. Applet被下载到用户的计算机上执行.
2. Applet还提供了名为抽象窗口工具箱(Abstract Window Toolkit, AWT)的窗口环境开发工具.
3. 含有Applet的网页的HTML文件代码中部带有<applet> 和</applet>这样一对标记.
3. Applet不需要main()方法,由web browser中内嵌的JVM执行.
4. Applets are designed just for handling the client site problems. while the java applications are designed to work with the client as well as server. 



Java Plug-in Technology


Java Plug-in technology, included as part of the Java Runtime Environment, Standard Edition (Java SE), establishes a connection between popular browsers and the Java platform. This connection enables applets on Web sites to be run within a browser on the desktop.
 

So, to run applets on a browser, you have to install JRE, which contains Java Plug-in.