There is a good article on springsource which describes how Spring is Simplifying Social Network Interactions Socializing Spring Applications

Constructors | Methods | |
| Purpose | Create an instance of a class | Group Java statements |
| Modifiers | Cannot be abstract, final, native, static, or synchronized | Can be abstract, final, native, static, or synchronized |
| Return Type | No return type, not even void | void or a valid return type |
| Name | Same name as the class (first letter is capitalized by convention) -- usually a noun | Any name except the class. Method names begin with a lowercase letter by convention -- usually the name of an action |
| this | Refers to another constructor in the same class. If used, it must be the first line of the constructor | Refers to an instance of the owning class. Cannot be used by static methods. |
| super | Calls the constructor of the parent class. If used, must be the first line of the constructor | Calls an overridden method in the parent class |
| Inheritance | Constructors are not inherited | Methods are inherited |
Class Methods | Instance Methods |
Class methods are methods which are declared as static. The method can be called without creating an instance of the class | Instance methods on the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword. Instance methods operate on specific instances of classes. |
Class methods can only operate on class members and not on instance members as class methods are unaware of instance members. | Instance methods of the class can also not be called from within a class method unless they are being called on an instance of that class. |
Class methods are methods which are declared as static. The method can be called without creating an instance of the class. | Instance methods are not declared as static. |
Situation | public | protected | default | private |
Accessible to class from same package? | yes | yes | yes | no |
Accessible to class from different package? | yes | no, unless it is a subclass | no | no |
static type varIdentifier;
public String getReCaptchaHtml() {
ReCaptcha recaptcha = createReCaptcha();
return recaptcha.createRecaptchaHtml("You did not type the captcha correctly", new Properties());
}
private ReCaptcha createReCaptcha() {
String publicKey = //your public key
String privateKey = //your private key
return ReCaptchaFactory.newReCaptcha(publicKey, privateKey, true);
}
${actionBean.reCaptchaHtml}@ValidationMethod(on = "submit")
public void captchaValidation(ValidationErrors errors) {
ReCaptchaResponse response = createReCaptcha().checkAnswer(context.getRequest().getRemoteAddr(),
context.getRequest().getParameter("recaptcha_challenge_field"),
context.getRequest().getParameter("recaptcha_response_field"));
if (!response.isValid()) {
errors.add("Captcha", new SimpleError("You didn't type the captcha correctly!"));
}
}



// From subclass super.overriddenMethod();super is a keyword which is used to access the method or member variables from the superclass. If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword. In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword. this.xxx variables or methods to compute its parameters. public final void exampleMethod() {
// Method statements
}public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.public and abstract modifiers are allowed for methods in interfaces.java.io.Serializable interface and Cloneable are typical marker interfaces. These do not contain any methods, but classes must implement this interface in order to be serialized and de-serialized.Abstract Class | Interfaces |
An abstract class can provide complete, default code and/or just the details that have to be overridden. | An interface cannot provide any code at all,just the signature. |
In case of abstract class, a class may extend only one abstract class. | A Class may implement several interfaces. |
An abstract class can have non-abstract methods. | All methods of an Interface are abstract. |
An abstract class can have instance variables. | An Interface cannot have instance variables. |
An abstract class can have any visibility: public, private, protected. | An Interface visibility must be public (or) none. |
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. | If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. |
An abstract class can contain constructors . | An Interface cannot contain constructors . |
Abstract classes are fast. | Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. |
package com.blogspot.aoj.servlet3;Note:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.apache.log4j.Logger;
@WebServlet(urlPatterns = "/fileUpload")
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
private static Logger logger = Logger.getLogger(FileUploadServlet.class);
public FileUploadServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
for (Part part : request.getParts()) {
logger.info(part.getName());
InputStream is = request.getPart(part.getName()).getInputStream();
int i = is.available();
byte[] b = new byte[i];
is.read(b);
logger.info("Length : " + b.length);
String fileName = getFileName(part);
logger.info("File name : " + fileName);
FileOutputStream os = new FileOutputStream("c:/temp/logs/" + fileName);
os.write(b);
is.close();
}
}
private String getFileName(Part part) {
String partHeader = part.getHeader("content-disposition");
logger.info("Part Header = " + partHeader);
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
return cd.substring(cd.indexOf('=') + 1).trim()
.replace("\"", "");
}
}
return null;
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<html>
<head>
<title>File Upload with Servlet 3.0</title>
</head>
<body>
<form action="fileUpload" enctype="multipart/form-data" method="post">
<input type="file" name="uploadFile" /> <input type="submit" /></form>
</body>
</html>
|
|