Acquaintance Java Web

Acquaintance Java Web

Learning Java Web system also got a week, and sort out the new concept of exposure to this week and based on what they have learned this week completed based on JSP + Servlet + Java Bean + MySql message board Demo

Several concepts

tomcat

tomcat is a "Web App Server", the underlying Socket is a program that is a container JSP / Servlet, in short, when we write HTML in our local computer, css, we can only write a page you visited, others we can not write a page for remote access, but allow others to access a tomcat is to write a program pages

Servlet/JSP

Commonly referred Servlet server applet, runs on the server for processing and responding to client requests, it is a Java class Servlet development follows, Servlet life cycle can be divided into five steps:

  • Load Servlet: When tomcat first visit Servlet, tomcat will be responsible for creating an instance of the Servlet
  • Initialization: When the Servlet is instantiated, tomcat will call the init () method to initialize the object
  • Processing services: When a browser to access Servlet, Servlet calls the service () method to handle user requests, in addition to the use of service () unified process user requests, Servlet also provides four different methods for responding to user requests
    • doPost: in response to client requests POST
    • doGet: GET request to respond to client
    • doDelete: in response to client requests DELETE
    • doPut: PUT request the client responsive
  • Destruction: When turned off or tomcat tomcat from Servlet detected when deleted automatically calls destory () method, so that the share of resources freed examples
  • Uninstall: When calling Servlet End destory () method, wait for garbage collection, if necessary, to use the Servlet again, it will re-invoke init () to initialize operation

Full name JSP Java Server Pages, is a text-based program, which is characterized by the presence of HTML and Java code at the same time, is the essence of jsp Servlet, jsp after being deployed to the Web container, Web container will automatically compiled into a corresponding JSP Servlet. Since the direct use of Servlet output page is very troublesome, JSP Servlet is output instead of the HTML page.

After the emergence of MVC specification, Servlet output page no longer need to label or play other roles view layer, just as a controller

JavaBean

JavaBean is a special Java class that must provide a no-argument constructor, member attributes is best defined as private, and offers a range setter, getter methods to access and manipulate these properties.

reflection

Reflection is the Java language itself has an important dynamic mechanism to explain the sentence reflected the definition: self-control, self-describing. Classes can dynamically obtain reflection attribute information, the method, the object can be constructed and the properties and behavior of an object.

for example:

Apple 类

public class Apple {
    private String name;
    public Apple() {

    }
    public Apple(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
}

自描述(通过反射获取类,属性,方法的信息)

import java.lang.reflect.*;

public class Main {
    public static void main(String[] args) {
        Class clazz = Apple.class;
        // 获取构造器信息
        for (Constructor constructor : clazz.getConstructors()) {
            System.out.println(constructor.getName() + " " + constructor.getParameterCount());
        }
        // 获取属性信息
        for (Field field : clazz.getDeclaredFields()) {
            System.out.println(field);
        }
        // 获取方法信息
        for (Method method: clazz.getDeclaredMethods()) {
            System.out.println(method);
        }
    }
}

输出

Apple 0
Apple 1
private java.lang.String Apple.name
public java.lang.String Apple.getName()
public void Apple.setName(java.lang.String)

自控制(构造对象并控制对象属性及行为)

import java.lang.reflect.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("Apple");
        final Constructor constructor = clazz.getConstructor(String.class);
        final Object appleInstance = constructor.newInstance("China Apple");
        System.out.println("Is appleInstance type equals Apple: " + (appleInstance instanceof Apple));
        Apple chinaApple = (Apple)appleInstance;
        System.out.println("Apple name: " + chinaApple.getName());
        final Method setNameMethod = clazz.getMethod("setName", String.class);
        setNameMethod.invoke(appleInstance, "Japan Apple");
        System.out.println("Apple name: " + chinaApple.getName());
        final Field nameField = clazz.getDeclaredField("name");
        nameField.setAccessible(true);
        nameField.set(appleInstance, "USA Apple");
        System.out.println("Apple name: " + chinaApple.getName());
    }
}

输出

Is appleInstance type equals Apple: true
Apple name: China Apple
Apple name: Japan Apple
Apple name: USA Apple

A substantially non-reflective of Java classes is running processes: preparation of the source file Apple.java, the compiler compiles it into bytecode file Apple.class, and finally loaded into the jvm running, in reflection mode, the compiler start ignorant of its type, only after running the compiler know its true type.

There are two main advantages reflection

  • In some scenarios, this "unknown" type greatly increased runtime flexibility
  • For determining the type of the object can be run, in fact, such characteristics is greatly enhanced polymorphic, further concrete implementation of the abstract and the lower upper decouple

annotation

The concept is very similar annotations and labels, annotation information is often resolve to achieve a "reflection"

Wall Demo

Using JSP + Servlet + JavaBean achieved, JSP layer implementation view, Servlet achieve control layer, MySQL database connection for data storage

https://github.com/bodhisatan/MessageBoard/

Guess you like

Origin www.cnblogs.com/yaoomoon/p/10994999.html