Java Web Review
JAVA Basics
1. JAVA keywords: final, finally, finalize, and static
Resolution: simple difference
Final is used to declare attributes, methods, and classes, indicating that attributes are unchangeable, methods cannot be overwritten, and classes cannot be inherited.
Finally is a part of the structure of the exception handling statement, indicating that it is always executed.
Finalize is a method of the Object class. This method is called when the garbage collector is executed. It can overwrite this method to collect other resources during garbage collection, for example, close a file.
Static: If a member is declared as static, it can be accessed before any object of its class is created without referencing any object.
Detailed analysis: Final keywords (1) The final modifier class indicates that the class cannot be inherited, and a class cannot be declared as abstract class or final class; (2) If final modifies a variable, the variable must be assigned an initial value, and its value will not change throughout the process; (3) The final modifier is called the final method. It cannot be redefined or overloaded by the quilt class; Static keywords: (1) static data or methods can be called directly without the new class instance; (2) static code block. The static keyword also plays a key role in forming static code blocks to optimize program performance. Finally:Java exception handling mechanism. Finally is the best supplement to the Java Exception Handling Model. The finally structure enables code to always be executed, regardless of exceptions. Using finally, you can maintain the internal status of an object and clear non-memory resources. Especially in the aspect of closing the database connection, if the programmer puts the close () method of the database connection in finally, it will greatly reduce the chance of program errors. Finalize: A method name in the Object class in Java. Ava technology uses the finalize () method to clear objects from the memory before the garbage collector. this method is called by the garbage collector when it determines that this object is not referenced. It is defined in the Object class, so all classes inherit it. Subclass overwrites the finalize () method to sort system resources or perform other cleanup tasks. The finalize () method is called before the Garbage Collector deletes an object. |
2. constructor/JAVA features/Inheritance/encapsulation/Abstraction/interface/polymorphism concept/IO stream file copy operation/JDBC database operation
2.1 Constructor: Class initialization;
2.2 JAVA features: Platform independence, security, object-oriented, distributed, robustness, and excellent performance.
2.3 three major features of JAVA: Encapsulation, inheritance, and polymorphism;
2.4 inheritance: Through inheritance, you can use the member methods and member variables of the previously defined class to construct a powerful new class after simple program encoding. UseExtendsThe Declaration format is as follows:
[Modifier] class name extends parent class name
class Son extends Father{……}
2.5 Encapsulation: Hides the attributes of an object inside the object and does not allow direct external access or modification. This is also known as the object class.
2.6 Abstraction: Abstract class and abstract method. Abstract classes are classes that cannot be instantiated using the new method, that is, classes without specific instance objects. Abstract classes are similar to "templates" to create and modify new classes based on their formats. Objects cannot be directly created by abstract classes. They can only be derived from abstract classes to create objects. When a class is declared as an abstract class, the modifier abstract must be added before the class. The member methods in the abstract class can include general methods and abstract methods. Abstract methods are modified with abstract. This method only declares the returned data type, method name, and required parameters. There is no method body. That is to say, abstract methods only need to be declared without implementation. When a method is an abstract method, it means that the method must be overwritten by the method of the quilt class. Otherwise, the method of its subclass is still abstract, and The subclass must be abstract, the statement is abstract. Abstract classes do not necessarily contain abstract methods, but classes that contain abstract methods must be declared as abstract classes. Abstract classes do not have actual functions and can only be used to derive their subclasses. Abstract classes can contain constructor methods, but they cannot be declared as abstract. Call methods in abstract classes (abstract and non-Abstract METHODS). If the methods are static, they are directly abstract classes. method. If it is not static, an inherited non-abstract class must be required, and then the non-abstract class instance is used to call the method ..
2.7 Interface: Use interface to define an interface. Similar definitions of interface definitions are similar. They are also divided into interface declarations and interface bodies. The interface body consists of constant definition and method definition. The basic format of the interface definition is as follows:
[Modifier] interface name [extends parent interface name list] {[public] [static] [final] constant; [public] [abstract] method ;}
In class inheritance, only single-inheritance can be performed. When an interface is implemented, multiple interfaces can be implemented at a time. Each interface is separated by commas. In this case, a constant or method name conflict may occur. To solve this problem, if a constant conflict exists, you must specify the constant interface, which can be implemented through "interface name. Constant. If a method conflict occurs, you only need to implement one method.
Polymorphism: polymorphism is one of the three major features of Java object-oriented language. polymorphism means that the same operation in a program has different semantic interpretations in different environments.
2.8 differences between an abstract class and an interface:
(1) In an interface, all methods are abstract. abstract classes can contain non-abstract methods.
(2) the interface is public, and there cannot be private methods or variables in it. They are used by others. abstract classes can have private methods or private variables;
(3) to implement an interface, you must implement all the methods defined in the interface. To implement an abstract class, you can selectively rewrite the methods to be used. In a general application, the top-level interface is the interface, then the abstract class implements the interface, and the class is finally implemented;
(4) An interface can implement multi-inheritance, while a class can only inherit one super class, but it can implement multi-inheritance by inheriting multiple interfaces. The interface also has an identifier (there is no method in it, such as Remote interface) and Data Sharing (the variables in it are all constants.
2.9 IO stream:IO includes input and output streams. An input stream refers to reading data from external media such as files and databases to the memory in character or byte form. Therefore, it is also divided into character input streams and byte input streams. The output stream refers to writing data in the memory to an external media, which is also divided into character output streams and byte output streams.
2.10 file copy operations
// Copy a file in the primary stream mode (do not read or write) FileReader fr = new FileReader (sourcePath); // read the stream BufferedReader from the source file character br = new BufferedReader (fr ); // The source file character reads the buffer stream FileWriter fw = new FileWriter (destPath, true); // the target file character writes to the stream BufferedWriter bw = new BufferedWriter (fw ); // buffer stream String str = null for writing characters in the target file; while (null! = (Str = br. readLine () {bw. write (mes); bw. newLine ();} bw. close (); br. close (); fw. close (); fr. close ();
// File inputFile = new File (inputPath); File outputFile = new File (outputPath); InputStream inputStream = new FileInputStream (inputFile ); outputStream outputStream = new FileOutputStream (outputFile); // read all the data to the memory at a time, and then write the data to public void copy1 () at a time () throws IOException {byte B [] = new byte [(int) inputFile. length ()]; inputStream. read (B); // read outputStream at a time. write (B); // One-time write into inputStream. close (); outp UtStream. close () ;}// Write public void copy2 () throws IOException {int temp = 0; while (temp = inputStream. read ())! =-1) {outputStream. write (temp);} inputStream. close (); outputStream. close ();}
2.11JDBC database operations:
Class. forName ("com. mysql. jdbc. driver "); // load the database Driver String url =" jdbc: mysql: // localhost: 3306/sqlname "; // specify the URLString username =" root "to connect to the database "; // specify the database Connection username String password = "123456"; // specify the database Connection password Connection conn = DriverManager. getConnection (url, username, password );
3. Definition and features of JavaBean
Java Beans is a reusable software component.
Features of JavaBean:
(1) It is a public class;
(2) No input parameters for the constructor;
(3) attributes must be declared as private and methods must be declared as public;
(4) use a set method to set internal properties;
(5) use a set of get methods to obtain internal attributes;
(6). It is a class without a primary method. The general Java class inherits from the Object class by default, and Bean does not need this inheritance.
4. JAVA expressions
An expression is composed of identifiers, constants, variables, and operators. It is a basic component of a program.
5. JSP knowledge points
5.1 Servlet Lifecycle
Step 1: The user requests the server through the client browser, the server loads the Servlet, and creates a Servlet instance;
Step 2: The container calls the Servlet init () method;
Step 3: The container calls the service () method, passes the HttpServletRequest and HttpServletResponse objects to the method, and processes user requests in the service () method;
Step 4: return the result to the container after the Servlet request processing is complete;
Step 5: The container returns the result to the client for display;
Step 6: when the Web server is disabled, call the destroy () method to destroy the Servlet instance.
5.2 JSP Lifecycle
The following JSP lifecycle stages:
(1) parsing phase:The Servlet container parses the JSP file code. If a syntax error occurs, the error message is returned to the client; (2) translation stage:The Servelet container translates the JSP file into the Servelet source file; (3) compilation phase:The Servelet container compiles the Servelet source file and generates the servelet class; (4) initialization phase:Load the Servelet class corresponding to JSP, create its instance, and call its initialization init () method; (5) execution phase:Call the service method of the Servelet instance corresponding to JSP; (6) Destruction stage:Call the destruction method of the Servelet instance corresponding to JSP, and then destroy the Servelet instance; |
5.3 commands
JSP commands (directive) are designed for the JSP Engine. They do not directly generate any visible output, but only tell the engine how to process the rest of the JSP page. In the JSP2.0 specification, three commands are defined:
(1) page command
(2) include command
(3) taglib command
Basic syntax format of JSP commands: <% @ command attribute name = "value" %>.
The page command defines various attributes of a JSP page. No matter where the page command appears on a JSP page, it serves the entire JSP page, to keep the program readable and follow good programming habits, the page instruction should be placed at the beginning of the entire JSP page.
The include command is used to introduce other JSP pages. If the include command is used to introduce other JSP pages, the JSP Engine translates the Two JSPs into a servlet. So the introduction of include commands is also called static introduction.
The Taglib command is used to import the tag library on the JSP page.
5.4jsp seven actions
(1) create a bean object:
(2) set attribute values for objects:
(3) retrieving attribute values from an object:
(4) page orientation:
Note: the redirection of the Action tag is the same request, which is equivalent to: request. getRequestDispatcher ("/url"). forward (request, response)
(5) : Used to specify the plug-in running on the client
(6) Pass Parameter Tag:
(7) The page contains:
5.5 scope object (nine implicit objects and their scopes)
Scope ranges from small to large: (pageContext, response, out, page, config, exception) ---- request ---- session ---- application
PageContext:The scope is limited to the current page object. It can be similar to this object in java. If you leave the current JSP page (whether redirect or forward), all attribute values in pageContext will be lost. Scope page
Request:The scope is within the same request. During page Jump, if you use the forward method to redirect, the forward target page can still get the attribute value in the request. If a page is redirected through redirect, because redirect is equivalent to sending a new request, the attribute values in the request will be lost in this scenario.
Session:The session scope is within the lifecycle of a session. If the session fails, data in the session will be lost.
Application:The scope is the largest. As long as the server does not stop, the application object will always exist and be shared by all sessions.
Response: indicates the response result of the client, scope page
Out: The standard output of the data stream. It is responsible for displaying the data results to the browser of the client and scope page.
Page: jsp itself. More accurately, it represents the Servlet after jsp is translated. Therefore, it can call the method defined by the Servlet class and scope page.
Config: it is related to servlet and stores some data structures and scope pages of the servlet's initial test.
Exception: exception Handling, scope page.
5.6 jump: And response. sendRedirect ("…") The former is an action tag called forward, which occurs on the server side and is highly efficient. The latter is called a reset item that occurs on the client;
5.7Servlet-JSP relationship:
Parsing: JSP must be converted to Servlet during the process. JSP is essentially a Servlet. JSP is an extension of Servlet technology. It is essentially a simple Servlet method, with more emphasis on the external expression of the application. After JSP compilation, it is "servlet-like ". The main difference between Servlet and JSP is that the application logic of Servlet is in the Java file and is completely separated from the HTML in the presentation layer. In JSP, Java and HTML can be combined into a file with the extension. jsp. JSP focuses on views. Servlet is mainly used for control logic.
6. Others
6.1 HTML form elements/Table
6.2 Tomcat port Modification
There is a server. xml file in the conf folder of Tomcat. Modify
In this Code, the port value is the port number!
6.3MVC Mode
MVC is a program development and design model that separates the display module from the function module. It improves the maintainability, portability, scalability and reusability of the program and reduces the difficulty of program development. It consists of three layers: model, view, and controller.
Model ):The main body of the application, mainly including the business logic module and data module. The model has nothing to do with the data format. Such a model can provide data for multiple views. Because the Code applied to the model can be reused by multiple views only once, code duplication is reduced;
View ):Interface: the interface for user interaction and the web View generally consist of jsp and html, and data should not be processed;
Controller ):Receives requests from the interface and submits them to the model for processing. This controls the interaction process between the model and the view.
Advantages of MVC:
1. developers can only pay attention to one of the layers in the entire structure;
2. It is easy to replace the original implementation with the new implementation;
3. The dependency between layers can be reduced;
4. conducive to standardization;
5. facilitate the reuse of logic at each layer.
In summary, the hierarchical design can achieve the following goals: decentralized attention, loose coupling, logic reuse, and standard definition.
Disadvantages of MVC:
Similar to the design pattern
1. added complexity;
2. The development efficiency of small projects is reduced while the development efficiency of large projects is improved;
3. the complicated process of processing the stream results in a reduction in performance;
4. Rich Client is provided to avoid javaScript, and JavaScript has never been taken into account in traditional MVC frameworks.