Java Side question ssh

Source: Internet
Author: User
Tags html form

1. Write out your familiar open source framework and their respective roles (why SSH is used in the project)

Answer: Frame: hibernate,spring,struts1/struts2. Hibernate is primarily used for data persistence, encapsulates JDBC operations, and provides an easy-to-use, efficient object-relational mapping framework; Spring's inversion of control can act as a decoupling function; Struts is primarily used for process control of request processing; Struts is based on the MVC pattern, It is very good to layered the application, so that the developers focus on the implementation of the business logic, struts has a wealth of taglib, such as flexible use can greatly improve the development efficiency.

Struts (presentation layer) +spring (business layer) +hibernate (persistence layer)

Struts related

2, briefly describe the struts structure (the work flow of struts, the working principle of struts)

Briefly:

Struts is an open-source framework that uses Java Servlet/javaserver pages technology to develop Web applications. Using struts can develop an application architecture based on the MVC (Model-view-controller) design pattern. Struts has the following main functions: one. Contains a controller servlet that can send a user's request to the appropriate action object. Two. JSP free tag library, and provide associated support in the controller servlet to help developers create interactive form applications. Three. Provides a series of practical objects: XML processing, automatic processing of JavaBeans attributes via Java Reflection APIs, internationalization hints and messages.

Process:

When the web app starts, it loads the initialization actionservlet,actionservlet reads the configuration information from the Struts-config.xml file and stores them in various configuration objects when Actionservlet receives a customer request, The following process will be performed. (1) The retrieval and the user request matches the Actionmapping instance, if does not exist, returns the request path invalid information; (2) If the Actionform instance does not exist, create a Actionform object and save the form data submitted by the customer to the Actionform object; (3) Determine if form validation is required based on configuration information. If validation is required, call Actionform's validate () method; (4) If Actionform's Validate () method returns null or returns a Actuiberrors object that does not contain actionmessage, the form validation is successful; (5) Actionservlet determines which action to forward the request to, based on the mapping information contained in the actionmapping, and if the corresponding action instance does not exist, create the instance first and then invoke the Execute () method of the action; (6) The Execute () method of the action returns a Actionforward object that actionservlet the JSP component to which the client request is forwarded to the Actionforward object; (7) The Actionforward object points to the JSP component to generate a Dynamic Web page, which is returned to the customer;

3, struts on the embodiment of MVC M: In struts, the model consists of JavaBean and EJB components to implement the business logic portion of the program. C:actionservlet,requestprocessor and Struts helper classes to implement the controller. Actionservlet is the core controller in Struts Actionservlet transfers control to the appropriate action class based on the configuration in the struts configuration file. The action class is the proxy for the business, and you can invoke the model component in the action class or write other business logic code to accomplish a specific business. The views in the V:struts framework are primarily made up of JSP files, where struts tags and custom tags can be used to perform simple processing of data in a model component. The Actionform Bean is actually a javabean that follows a special convention, in struts Actionform beans can be seen as an intermediate memory between the view and the controller for data transfer.

4, the difference between struts1.2 and struts2.0? A, Action class: struts1.2 requires that the action class inherit a base class. struts2.0 action can be a simple Jopo object or (will) inherit Actionsupport base class B, thread mode struts1.2 action is singleton mode and must be thread safe, Because there is only one instance of the action to handle all requests. The singleton strategy limits what Struts1.2 action can do and is especially careful when developing. The action resource must be thread-safe or synchronous. struts2.0 action generates an instance for each request, so there is no thread safety issue. C, the servlet relies on the struts1.2 action to rely on the servlet API, because when an action is invoked HttpServletRequest and HttpServletResponse are passed to the Execut method. The struts2.0 action does not depend on the container, allowing the action to be tested separately from the container. If required, the STRUTS2 action can still access the initial request and response. However, other elements reduce or eliminate the need for direct access to HttpServletRequest and HttpServletResponse. D, testability test one of the main problems with the struts1.2 action is that the Execute method exposes the servlet API (which makes the test dependent on the container). A third-party extension: Struts TestCase provides a set of struts1.2 mock-up objects for testing. Struts2.0 action can be tested by initializing, setting properties, calling methods, and "Dependency injection", which makes testing easier.

5, how to achieve the internationalization of struts

The following are examples of the two languages (Chinese, English): 1. Add struts support to the project 2. Edit the Applicationresource.properties file to include information that you want to use internationalization, for example: Lable.welcome.china=welcome!!! 3. Create the English resource file Applicationresource_en.properites 4. Create temporary Chinese resource file Applicationresource_temp.properites For example: lable.welcom.china= China welcomes you! 5. Encode and convert the temporary Chinese resource file. You can use MyEclipse plug-ins, or DOS execution: native2ascii-encoding gb2312 applicationresource_temp.properties Applicationresource_ Zh_cn.properties 6. Adding struts's bean tag library to the JSP

6, the data validation of struts framework can be divided into several types?

Form validation (handled by Actionform Bean): If the user submits the form without entering a name in the form, the form validation error will be generated

Business logic validation (handled by action): If the user enters the form with the name "Monster", the business rules for this app do not allow "Monster" to be greeted, so a business logic error will be generated.

7. Briefly describe the form validation process of form Bean.

1. When the user submits an HTML form, the Struts framework automatically assembles the form data into the Actionform bean.

2. The struts framework then calls the Actionform Bean's Validate () method for form validation.

3. If the Actionerrors object returned by the Validate () method is null, or does not contain any Actionmessage objects, it means there is no error and data validation is passed.

4, if the actionerrors contains Actionmessage objects, it means that a validation error occurred, the Struts framework will save the Actionerrors object to the request scope, and then forward the request to the appropriate view component, the view component through < The html:errors> tag displays the error message contained in the Actionerrors object within the request scope, prompting the user to modify the error.

8. Brief description of the role of Actionform Bean

1, Actionform Bean is also a kind of javabean, in addition to having some javabean general methods, but also contains some special methods, used to validate the HTML form data and to reset its properties to the default values.

2. The struts framework uses actionform beans to transfer form data between view components and controller components.

3. The struts framework saves the user-entered form data received by the view component in the Actionform bean, passes it to the controller component, and the controller component can Actionform The data in the bean is modified the JSP file uses the struts tag to read the modified Actionform bean information and reset the HTML form.

9. Struts Advantages and disadvantages Advantages:

1. Implement the MVC pattern with a clear structure that allows developers to focus only on the implementation of the business logic. 2. There is a rich tag can be used, struts of the tag library (Taglib), if flexible to use, it can greatly improve the development efficiency of 3. Page navigation makes the system's context clearer. Through a configuration file, you can grasp the connection between the parts of the whole system, which is of great benefit to the later maintenance. This is especially true when another group of developers took over the project. 4. Provide exception processing mechanism. 5. Database link Pool Management 6. Support i18nDisadvantagesFirst, to go to the presentation layer, you need to configure the forward, if there are 10 display layer of the JSP, need to configure 10 struts, but also does not include sometimes directories, file changes, need to re-modify the forward, note that each time the configuration is modified, the request to redeploy the entire project, Servers such as Tomcate must also be restarted server two or two, Struts action is required to be thread-safe mode, it only allows one instance to handle all requests. So all the resources that the action uses must be synchronized uniformly, which raises the thread-safety issue. Third, the test is not convenient. Each of struts's actions is coupled to the web layer so that its tests depend on the Web container and unit tests are difficult to implement. However, there is a junit extension tool that struts testcase can implement for its unit testing. Iv. types of conversions. The Formbean of struts takes all the data as a string type, which can be converted using the tool commons-beanutils. However, it is converted at the class level, and the type of conversion is not configurable. It is also very difficult to return the error message to the user when the type is converted. V. The dependency on the servlet is too strong. Struts must rely on servletrequest and servletresponse when handling action, all of which are not able to get rid of the servlet container. Vi. front-end expression language aspects. Struts integrates Jstl, so it uses the JSTL expression language primarily to get data. But Jstl's expression language is weak in terms of collection and indexed properties. Vii. control of action execution is difficult. Struts creates an action, which can be very difficult if you want to control the order in which it is executed. You'll even have to write the servlet again to implement your functional requirements. Eight, the action to perform before and after the processing. Struts handles action as a class-based hierarchies, which makes it difficult to operate before and after action processing. Nine, the incident support is not enough. In struts, it is actually a form form that corresponds to an action class (or Dispatchaction), in other words: in struts it is actually a form that only corresponds to one event, and struts is called the application event, Application events is a coarse-grained event compared with component event

Hibernate related

1. Understanding of Hibernate framework (working principle)

Hibernate is a lightweight, persistent, open-source framework that is the middleware that connects Java applications and relational databases, and is responsible for mapping between Java objects and relational data. Hibernate internally encapsulates the JDBC API and is responsible for the persistence of Java objects. Because it encapsulates all the data access details, the business logic layer can focus on implementing the business logic. It is an excellent ORM mapping tool that provides a perfect object-relational mapping service, the development process is not dependent on the container, the flexibility is very large, can be seamlessly integrated into any Java system

2, why to use Hibernate

1. Encapsulates JDBC, simplifying many of the repetitive code. 2. The DAO layer coding has been simplified to make development more object-enabled. 3. Good portability, support a variety of databases, if you change a database as long as the configuration file to transform the configuration can be, do not change hibernate code. 4. Support transparent persistence, because Hibernate operates purely (Pojo) Java classes, without implementing any interfaces, without intrusion. So it's a lightweight framework.

3. Cache:

Session cache: The first-level cache known as Hibernate. It stores the objects that were loaded by the work unit before the sheet. Sessionfactory Cache: A second-level cache called Hibernate. An instance corresponds to a data storage source. It is thread-safe and heavyweight. It requires a large cache to hold pre-defined SQL statements, as well as mapping meta data.

4.3 states of an object

Instantaneous: After an entity is created with the new operator, no relationship is established with Hibernate's session, nor is it manually assigned the persistent identity of the entity (the persistent identity can be considered the primary key of the mapping table). Updates to any of the attributes in the entity are not reflected in the database tables at this time.
Persistence: When an entity and Hibernate session create a relationship and obtain a persistent identity, it exists during the session life cycle of hibernate. Changes to any property of the entity directly affect the update of the corresponding field in a record in the database table, which is synchronized with the corresponding database table.
Off-tube: When an entity and Hibernate session create a relationship and obtain a persistent identity, the lifetime of the session at Hibernate ends, and the persistent identity of the entity is not altered. Modifications to any properties of the entity are not reflected in the database tables in a timely manner.

5, performance problems in the batch operation performance and the cache there is irreconcilable contradiction, you have to be familiar with Hibernate level and level two cache mechanism to write reasonable batch operation code, otherwise not only performance is low, may also lead out memory. The best practise of the Hibernate reference documentation also mentions the use of hand-coded jdbc in bottlenecks, meaning that hard-coded JDBC is considered where some performance bottlenecks are concerned.

6. What is the difference between JDBC,HIBERNATE,EJB and three?

EJB: We must abide by the complex Java EE specification, and Hibernate does not force the need to meet certain specifications. EJBs can only run in EJB containers, and hibernate may run in any Java environment. Currently, the EJB container provides limited object-relational mapping capabilities for complex domain models. In contrast, Hibernate provides a perfect object-relational mapping service. Although EJB is a portable component, it is actually very limited, because the different producers produce CMP engine differences, they use the object-relational mapping metadata are different, Makes it impossible for EJBs to migrate from one EJB container to another. Hibernate can be seamlessly integrated into any Java system.

JDBC: Code that implements business logic and code that accesses the database is mixed together to make the program structure unclear and the readability poor

7. The difference between get and load

1>get returns the entity class directly, does not exist, returns NULL, and does not error.

2>load returns a proxy object (Javassist.jar generates a binary code), and a SELECT statement is issued when a proxy object is called. Throws an exception if it does not exist (the cause of lazy loading can be configured)

3> will first look for a primary cache (session cache), and then check the level two cache (need to configure), and finally go to the database to find. Call the Clear () method to force the purge

8. How does hibernate delay loading?

1. Hibernate2 Deferred load implementation: a) Entity object B) Collection (Collection) 2. Hibernate3 provides lazy loading of properties when hibernate is querying the data, the data does not exist in memory, and when the program actually operates on the data, the object exists in memory, and the delay is loaded, which saves the server's memory overhead and improves the server's performance.

9. How to optimize hibernate?

1. Use bidirectional one-to-many associations, do not use unidirectional pair-to-more 2. Flexible use of unidirectional one-to-many associations 3. Instead of one-to-many, replace 4 with a multi-pair. Configure the object cache without using the collection cache 5. A one-to-many collection uses bag, which uses set 6 for many-to-many collections. An inherited class uses explicit polymorphism 7. Fewer table fields, no more fear of tables, two levels of cache

10. What are the core classes of hibernate, and what are their interrelationships? What are the important ways?

Configuration Interface: Configure Hibernate to create a Sessionfactory object according to its startup hibernate; Sessionfactory interface: Initialize hibernate, act as the proxy for data storage source, create session object, Sessionfactory is thread-safe, meaning that its same instance can be shared by multiple threads of application, it is heavyweight, level two cache; Session Interface: Responsible for saving, updating, deleting, loading and querying objects, is thread insecure, to avoid multiple threads sharing the same session, is lightweight, first-level cache; The session is as follows: Save,load,update,delete, Query q=createquery ("from Customer where Customername=:customername") BeginTransaction, close, transaction, COMMIT TRANSACTION interface: Management transactions; Query and Criteria interfaces: Queries that execute the database.

Spring related

1, Spring's understanding

Spring is a lightweight container, non-intrusive framework. The most important core concept is the IOC, and provides an approach to implementing the AOP concept, providing support for persistent layers, transactions, and some of the current popular frameworks (STRUTS,HIBERNATE,MVC), Sping also offers a solution that integrates with them. With spring, we can reduce the dependency between classes and the degree of coupling between programs, maximizing loose coupling, making programs more flexible and more scalable.

IOC, Chinese translation for "inversion control,". di->; " Dependency Injection "means that we do not have to maintain the dependency of the object in the program code, but rather to inject the properties and instances of the class into the class silently through an XML configuration file. The dynamic loading of the class makes it easy to switch between classes and classes (via interfaces).

The idea of this design approach is that the high-level modules should not rely on the lower modules, but the modules must rely on abstraction. The program should not be dependent on implementation, but rather on an abstract interface. The application does not look for containers, but containers give us all the objects we want.

Another important aspect of spring is support for AOP implementations: The Chinese translation of AOP is: aspect-oriented programming, also known as problem-oriented programming. Aspect-oriented programming (AOP) complements object-oriented programming (OOP), and object-oriented programming decomposes programs into objects of all levels. Plane-oriented programming breaks down the program's running process into facets. AOP considers the structure of the program from the point of view of the program, extracts the facets of the business process, and OOP is a static abstraction, and AOP is a dynamic abstraction that abstracts the steps in the application execution process, thus obtaining the logical division between the steps.

The AOP framework has two characteristics: 1. Good isolation between steps 2. Source Code Independence

2. Spring annotations

In addition to providing @Component annotations in Spring 2.5, several annotations with special semantics are defined, namely, @Repository, @Service, and @Controller. In the current Spring version, these 3 comments and @Component are equivalent, but from the name of the annotation class it is easy to see that the 3 annotations correspond to the persistence layer, the business layer, and the control layer (the WEB layer), respectively. Although there is nothing new about these 3 annotations and @Component, Spring will add special features to them in future releases. Therefore, if a WEB application uses a classic three-tier hierarchy, it is best to annotate the classes in the hierarchy by using @Repository, @Service, and @Controller, respectively, in the persistence layer, the business layer, and the control layer @Component Comment on those classes that are more neutral.

In a slightly larger project, there are usually hundreds of components, and if these components are configured with an XML bean definition, it will obviously increase the volume of the configuration file, and it may not be convenient to find and maintain them. Spring2.5 introduced the component Auto-scan mechanism, where he could look under the classpath to annotate @Component, @Service, @Controller, @Repository annotated classes, and incorporate these classes into the spring container for management. Its role is the same as when you configure a component with a bean node in an XML file.

@Service is used to label business layer components, @Controller for labeling control layer components (such as action in struts), @Repository for labeling data Access Components, DAO components, @Component generic components, when components are poorly categorized , we can use this annotation for labeling.

3. What are the advantages of spring?

1. The coupling between the components is reduced, and the decoupling between the various layers of the software is realized 2. You can use many services that are easy to provide, such as transaction management, messaging services, etc. 3. Container provides singleton mode support 4. The container provides AOP technology, which makes it easy to implement functions such as permission interception, runtime monitoring, etc. 5. The container provides many auxiliary classes, which can accelerate the development of the application 6.spring for the mainstream application framework provides integrated support, such as Hibernate,jpa,struts 7.spring is a low-intrusive design, code pollution is very low 8. Independent of various application Server 9. Spring's di mechanism reduces the complexity of business object substitution by 10.Spring, and does not force applications to rely entirely on spring, and developers are free to select part or all of spring

4. How to configure database driver in spring?

Use the "Org.springframework.jdbc.datasource.DriverManagerDataSource" data source to configure the database driver.

5, Spring inside Applicationcontext.xml file can be changed to other file name?

Contextloaderlistener is a servletcontextlistener that is initialized when your web app starts. By default, it will find spring's configuration in the Web-inf/applicationcontext.xml file. You can change the location of the spring configuration file by defining an element named "Contextconfiglocation". Examples are as follows:

Org.springframework.web.context.ContextLoaderListener Contextconfiglocation/web-inf/xyz.xml

6. Some important concepts of terminology are explained in AOP

Facets (Aspect): A focus of modularity, which may be crosscutting across multiple objects. Transaction management is a good example of a crosscutting concern in the Java EE application. In spring AOP, facets can be implemented using generic classes (pattern-based styles) or @Aspect annotations (@AspectJ styles) in ordinary classes.

Connection point (Joinpoint): a particular point in the execution of a program, such as when a method is called or when an exception is handled. In spring AOP, a connection point always represents the execution of a method. By declaring a parameter of type Org.aspectj.lang.JoinPoint, you get the connection point information for the body part of the notification (Advice).

Notification (Advice): An action performed on a particular connection point (joinpoint) of a slice. Notifications are available in various types, including notifications such as "Around", "before", and "after". The type of notification is discussed later in this section. Many AOP frameworks, including spring, are notification models with interceptors, and maintain a chain of interceptors centered on the connection point.

Pointcut (Pointcut): an assertion that matches a connection point (Joinpoint). The notification is associated with a pointcut expression and runs on the connection point that satisfies the pointcut (for example, when a method for a particular name is executed). How Pointcut expressions match connection points is the core of AOP: Spring defaults to using the ASPECTJ pointcut syntax.

Introduced (Introduction): (also known as an internal type declaration (Inter-type declaration)). Declare additional methods or fields of a certain type. Spring allows the introduction of new interfaces (and a corresponding implementation) to any Proxied object. For example, you can use an introduction to make the bean implement the IsModified interface to simplify the caching mechanism.

Target object: An object that is notified (advise) by one or more facets (aspect). It is also called the notified (advised) object. Since spring AOP is implemented by the runtime proxy, this object is always a proxy (proxied) object.

AOP Proxy: An object created by the AOP framework to implement a facet contract (aspect contract), including functions such as notification method execution. In spring, an AOP proxy can be either a JDK dynamic agent or a cglib proxy. Note: The latest introduction to Spring 2.0 is the pattern-based (schema-based) style and @aspectj annotation style facet declarations, which are transparent to the users who use these styles.

Weaving (Weaving): Connects a facet (aspect) to another application type or object and creates an object that is notified (advised). These can be done at compile time (for example, with the AspectJ compiler), at class load time, and at run time. Spring, like other pure Java AOP frameworks, completes weaving at run time.

Type of notification:

Pre-notification (before advice): A notification that is executed before a connection point, but this notification does not prevent execution before the connection point (unless it throws an exception).

Notification after return (after returning advice): A notification that is executed after a connection point is completed normally: for example, a method does not throw any exceptions and returns normally.

Notification after an exception is thrown (after throwing advice): the notification that is executed when the method throws an exception exits.

Post notification (after (finally) advice): A notification that is executed when a connection point exits (whether it is a normal return or an unexpected exit).

Surround notification (Around Advice): A notification that encloses a connection point, such as a method call. This is the most powerful type of notification. Surround notifications can accomplish custom behavior before and after a method call. It also chooses whether to continue executing the connection point or to return directly to their own return value or throw an exception to end the execution.

Surround notification is one of the most common types of notifications. Most interception-based AOP frameworks, such as Nanning and JBOSS4, provide only surround notifications.

The concept of pointcut (pointcut) and connection point matching is the key to AOP, which makes AOP different from other old technologies that simply provide interception functionality. The pointcut enables the location notification (advice) to be independent of the OO hierarchy. For example, a around notification that provides declarative transaction management can be applied to a set of methods that span multiple objects, such as all business operations at the service layer.

Java Side question ssh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.