Thursday, October 25, 2012
Big O for java Datastructures
Monday, October 22, 2012
AWT notes
| Component | Event it generates |
|---|---|
| Button, TextField, List, Menu | ActionEvent |
| Frame | WindowEvent |
| Checkbox, Choice, List | ItemEvent |
| Scrollbar | AdjustmentEvent |
| Mouse (hardware) | MouseEvent |
| Keyboard (hardware) | KeyEvent |
| Listener Interface | Component |
|---|---|
| WindowListener | Frame |
| ActionListener | Button, TextField, List, Menu |
| ItemListener | Checkbox, Choice, List |
| AdjustmentListener | Scrollbar |
| MouseListener | Mouse (stable) |
| MouseMotionListener | Mouse (moving) |
| KeyListener | Keyboard |
public class ButtonDemo extends Frame implements ActionListener { public ButtonDemo() { Button btn = new Button("OK"); btn.addActionListener(this); add(btn); } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); } } |
Wednesday, August 8, 2012
Understanding Page Directives
Three Key Directives
<% @ page ..... %>
<% @ include .... %>
<% @ tag lib ... %>
Page Directive :
format <% @ page attribute="value" %>
Attributes :
buffer
Note :
Refference:
http://www.oreillynet.com/pub/a/oreilly/java/news/jsptips_1100.html
Tuesday, July 3, 2012
Interesting one liners
2. What happen if you declare a static method as final
Ans : Stops you from declaring a static method with the same name and parameter types in a derived class
3. "this" keyword cannot be used inside static methods/blocks
4. Static final variables will have to be initialized with the declaration and cannot be left blank
5. final variables can initialized only during declaration or in the consturctor
6. Anonymous classes do not have constructors
7. few methods in Object class are-
clone, equals,hashcode,finalize,toString, notify,notifyall,wait
8. how to call garbage collector explicitly
System.gc();
9.Difference btw Interface & Abstract
all methods in the Interface and implicitly abstract
10. How to create a deamon thread
setDeamon()
Sythetic Field
http://tns-www.lcs.mit.edu/manuals/java-api-1.1beta2/guide/innerclasses/html/innerclasses.doc.html#19265
Servelet
Life Cycle of a Servelet
a) A server loads and initializes the servlet by init () method.
b) The servlet handles zero or more client’s requests through service() method.
c) The server removes the servlet through destroy() method.
What is session tracking and how do you track a user session in servlets?- Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are:
a) User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password.
b) Hidden form fields - fields are added to an HTML form that are not displayed in the client’s browser. When the form containing the fields is submitted, the fields are sent back to the server.
c) URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.
d) Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.
What is Server-Side Includes (SSI)?-
Server-Side Includes allows embedding servlets within HTML pages using a special servlet tag. In many servlets that support servlets, a page can be processed by the server to include output from servlets at certain points inside the HTML page. This is accomplished using a special internal SSINCLUDE, which processes the servlet tags. SSINCLUDE servlet will be invoked whenever a file with an. shtml extension is requested. So HTML files that include server-side includes must be stored with an . shtml extension.
e) HttpSession- places a limit on the number of sessions that can exist in memory. This limit is set in the session. maxresidents property.
| Q: | What are the common mechanisms used for session tracking? | |
| A: | Cookies SSL sessions URL- rewriting | |
What is the difference between doGet() and doPost()?
#
| doGet() | doPost() |
|---|---|---|
1
| In doGet() the parameters are appended to the URL and sent along with header information. | In doPost(), on the other hand will (typically) send the information through a socket back to the webserver and it won't show up in the URL bar. |
2
| The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters. | You can send much more information to the server this way - and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects! |
3
| doGet() is a request for information; it does not (or should not) change anything on the server. (doGet() should be idempotent) | doPost() provides information (such as placing an order for merchandise) that the server is expected to remember |
4
| Parameters are not encrypted | Parameters are encrypted |
5
| doGet() is faster if we set the response content length since the same connection is used. Thus increasing the performance | doPost() is generally used to update or post some information to the server.doPost is slower compared to doGet since doPost does not write the content length |
6
| doGet() should be idempotent. i.e. doget should be able to be repeated safely many times | This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable. |
7
| doGet() should be safe without any side effects for which user is held responsible | This method does not need to be either safe |
8
| It allows bookmarks. | It disallows bookmarks. |
Creating a servelet :
1.Servlet needs to extend HttpServlet
2. Constructor calls super()
3. Has doGet() and doPost() methods
4. Web.xml has to updated with an entry for the servlet
ex :
<servlet>
<description></description>
<display-name>TestServeletOne</display-name>
<servlet-name>TestServeletOne</servlet-name>
<servlet-class>com.cisco.capacityplanning.servelet.TestServeletOne</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServeletOne</servlet-name>
<url-pattern>/TestServeletOne</url-pattern>
</servlet-mapping>
url for testing this servlet:
http://localhost:8080/CapacityPlanning/TestServeletOne
Saturday, June 30, 2012
Singleton Class
1.Define the constructor as private
2.Expose a method that can be used to create the object - getObject()
3. Synchronize the exposed method getObject method
4. Define a private Static - which would store a single instance.
Nested Class
1. how do nested classes act when overridden
- Non-static nested classes are also called inner classes have access to other members of the enclosing class, even if they are declared private
- Static nested classes do not have access to other members of the enclosing class
- As a member of the
OuterClass, a nested class can be declaredprivate,public,protected, or package private
Why Use Nested Classes?
There are several compelling reasons for using nested classes, among them:- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.
Non Static Nested Inner Class :
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass(); An instance ofInnerClasscan exist only within an instance ofOuterClassand has direct access to the methods and fields of its enclosing instance. Also, because an inner class is associated with an instance, it cannot define any static members itself. Static Nested Class : As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
| Type | Scope | Inner |
|---|---|---|
| static nested class | member | no |
| inner [non-static] class | member | yes |
| local class | local | yes |
| anonymous class | only the point where it is defined | yes |
Note :
- Interfaces are never inner .
- Non Static inner classes cannot define static variables ex static int abc =0 will result in compile time error
- Non Static inner classes can define compile time constatns ex static final int abc=0
Tuesday, March 20, 2012
Connecting to Database
a) Loading the driver : To load the driver, Class. forName() method is used. Class. forName(”sun. jdbc. odbc. JdbcOdbcDriver”); When the driver is loaded, it registers itself with the java. sql. DriverManager class as an available database driver.
b) Making a connection with database: To open a connection to a given database, DriverManager. getConnection() method is used. Connection con = DriverManager. getConnection (”jdbc:odbc:somedb”, “user”, “password”);
c) Executing SQL statements : To execute a SQL query, java. sql. statements class is used. createStatement() method of Connection to obtain a new Statement object. Statement stmt = con. createStatement(); A query that returns data can be executed using the executeQuery() method of Statement. This method executes the statement and returns a java. sql. ResultSet that encapsulates the retrieved data: ResultSet rs = stmt. executeQuery(”SELECT * FROM some table”);
d) Process the results : ResultSet returns one row at a time. Next() method of ResultSet object can be called to move to the next row. The getString() and getObject() methods are used for retrieving column values: while(rs. next()) { String event = rs. getString(”event”); Object count = (Integer) rs. getObject(”count”);
Interface
No ( there is no point in defining a private variable/methods in the interface)
Can an interface have an abstract method defined ?
YES
Can a method in an interface be static or final
NO
Can a variable in an interface be static
YES
Setting up TOMCAT on Unix
How To Install Apache Server on your Unix Account
Following are steps to install Apachee Server in your area and you get the chance to become your own server administrator
Step 1: visit http://www.apache.org/
Step 2: Click on Apache Server (seventh item from the left).
Step 3: Click on Download.
Step 4: Click on apache_1.3.28.tar.z this the 12th item from the left and it is for Unix. Remember the place where you saved it on your PC. For the sake of explanation assume that you saved it on the desk_top. Thus on your desk_top now you have apache_1.3.28.tar.z . Notice z tells that the file is zipped and tar tells that the file is an archieve file contains more than one file.
Step 5: log in into your Unix account.
Step 6: type the following two commands to create apache directory and to make it to be the current working directory
mkdir apache
cd apache
Step 7: Use an FTP facility such as CuteFTP 32 or lite Ftp to transfer apache_1.3.28.tar.z from the desk_top to your directory in Unix. To achieve that do the following items:
a. click on the FTP facility
b. Login into your Unix account on FTP,.
c. Click on apache directory in your home directory (in FTp).
d. Click on C: then click on WINDOWS, click on desk_top, click on apache_1.3.28.tar.z on some FTP
facilities you also have to click on -->> to transfer it from desk top to your account. Now you have
apache_1.3.28.tar.z in apache directory on your home directory. Close the FTP facility.
Step 8: Make sure you are at apache directory by typing pwd command. If you are not at apache directory type
command cd apache.
Step 9: To unzip the file issue the command
gunzip apache_1.3.28.tar.z
Now the file is unzipped and if you type ls command you see you have only apache_1.3.28.tar file in apache
directory.
Step 10: To extract files from apache_1.3.28.tar type the following command:
tar xvf apache_1.3.28.tar
Notice xvf are options to tar command.
Step 11: Since files are extracted from apache_1.3.28.tar, now delete apache_1.3.28.tar file by typing:
rm apache_1.3.28.tar
Step 12: type the following command
Change directory to Apache_1.3.28 by executing
cd Apache_1.3.28
Now execute the command
./configure --prefix=/usr/users/USERDIR/apache
Notice replace USERDIR by your home director. Thus this command for user ks6241 will be:
./configure --prefix=/usr/users/students/ks6241/apache
Step 13: Type the command
make
Step 14: Type the command
make install
Step 14a: Type the command - this will save you a significant amount of disk space!
make clean
Step 15: Edit apache/conf/httpd.conf by using an editor such as pico or vi. Assume that we use pico editor, then type
pico apache/conf/httpd.conf
make the following changes while you are at pico:
Listen 3000 Notice any port number above 1000 will be fine.
Now make the following changes:
MinSpareServers 1
MaxSpareServers 2
StartServers 1
If you done every right the following message. Notice the installation takes approximately 40 minutes.
--------------------------------------------------------------------
| You now have successfully built and installed the
| Apache 1.3 HTTP server. To verify that Apache actually
| works correctly you now should first check the
| (initially created or preserved) configuration files
|
| /usr/users/cis/khailany/apache/conf/httpd.conf
|
| and then you should be able to immediately fire up
| Apache the first time by running:
|
| /usr/users/cis/khailany/apache/bin/apachectl start
|
| Thanks for using Apache. The Apache Group
| http://www.apache.org/
----------------------------------------------------------------------
After installing apache server you have to make the following changes
1. Edit 'httpd.conf' file. Assuming that you are at your home directory.
type
pico apache/conf/httpd.conf
use other path if you 'httpd.conf' is in another directory
Notice in pico control c tells in which line the editor is.
2. Comment "Listen" by using '#', since "Listen" is not being used
3. Find value "Port" (line 236) and make sure it is uncommented. The default value is '8080' which is the EMU unix web server, and therefore can not be used. Change that value to a UNIQUE value that is specific to you server.
Notice to find all port numbers which are used type the command:
netstat -a
4. Find values "User" and "Group" (line 250). Change them to your own username and group.
5. Make sure that "world" can access your apache/htdocs directory which is root directory for html web pages. In other words change the protections of apache/htdocs by typing chmod 755 apache/htdocs
6. The default cgi-bin directory is apache/cgi-bin. To change it go to line# 535 and change the value of the directory here and couple of line below to the one that you want to be your cgi-bin.
7. Close pico
8. If your apache server is running stop it by typing
apache/bin/apachectl stop
9. Restart apache by typing
apache/bin/apachectl start
Notice: If someone using your server you may not want to stop it immediately. In such cases use gracefull
command by typing
apache/bin/apachectl gracefull