Java Face question-javaweb article

Source: Internet
Author: User

51. Say a servlet's life cycle?

Servlets have a good definition of lifetime, including loading and instantiation, initialization, processing requests, and end of service. This lifetime is expressed by the Init (), service (), and destroy methods of the Javax.servlet.Servlet interface.

When the servlet is instantiated by the server, the container runs its Init method, the service method is run when the request arrives, and the service method automatically dispatches the Doxxx method (Doget,dopost) corresponding to the request, etc. The Destroy method is called when the server decides to destroy the instance.

The Web container loads the servlet, beginning with the life cycle. The servlet is initialized by calling the servlet's init () method. By invoking the service () method implementation, different do*** () methods are called depending on the request. To end the service, the Web container invokes the servlet's Destroy () method.

52. What is the difference between forward () and redirect () in the Servlet API?

1. From the Address bar display
Forward is the server requests the resource, the server directly accesses the URL of the destination address, reads the response content of that URL, and then sends the content to the browser. The browser does not know where the server sent the content from, so its address bar is still the original address.
Redirect is the server based on logic, sending a status code that tells the browser to re-request that address. So the address bar shows a new URL. So redirect is equal to the client issuing two requests and receiving two response.
2. From data sharing
Forward: The forwarded page and the forwarded page can share the data within the request.
Redirect: Data cannot be shared.
Redirect can redirect not only to other resources in the current application, but also to resources in other applications at the same site, even to resources that are redirected to other sites using absolute URLs.
The forward method can only forward requests between resources within the same Web application. Forward is an operation inside the server.
Redirect is the server notification client, which lets the client re-initiate the request.
So, you can say that redirect is an indirect request, but you can't say "a request belongs to forward or redirect"
3. From the place of use
Forward: Generally used for user login, according to the role of forwarding to the corresponding module.
Redirect: Generally used for users to log off when landing to return to the main page and jump to other sites and so on.
4. From an efficiency standpoint
Forward: High.
Redirect: Low.

53. What is the difference between request.getattribute () and Request.getparameter ()?

1,request.getparameter () is obtained through the implementation of the container to obtain the data passed through similar post,get.

Request.setattribute () and getattribute () only flow inside the Web container, only the request processing phase.

2,getattribute is the return object, GetParameter returns the string

3,getattribute () has always been used with setattribute () only after the setattribute () has been set up to obtain values through GetAttribute (), which pass data of type object. and must be used in the same request object to be valid. , and GetParameter () is a GET or post-submitted parameter that receives a form

54,jsp the difference between static inclusions and dynamic inclusions

1, <% @include file= "xxx.jsp"%> is the compiler directive in JSP, its file contains is the time that the JSP transforms to servlet, and <jsp:include page= "xxx.jsp" > Is the action instruction in the JSP, whose file contains the period of compilation, that is, the time when the Java file was compiled into a class file.

2, using static inclusion will only produce a class file, while using dynamic inclusion will produce multiple class files

3, using static inclusion, the request object containing the page and the contained page is the same object, because the static inclusion simply copies the contents of the contained page to the contained page, while the dynamic inclusion contains the page and the included page is not the same page. The request object of the included page can be taken to a relatively large range of parameters, not only to the parameters that are passed to the containing page, but also to the parameters that are passed down the page containing the pages.

How do the various parts of 55,MVC have the technology to implement?

MVC is shorthand for Model-view-controller. The model represents the business logic of the application (implemented through the JAVABEAN,EJB component), the view is the presentation surface of the application (generated by the JSP page), and the controller is the process control that provides the application (typically a servlet), which uses this design model to apply logic, The process and display logic are divided into different component implementations. These components can be interacted with and reused.

What are the built-in objects for 56,jsp? What are the roles?

The JSP has the following 9 built-in objects:

1,request client request, this request will contain the parameters from the Get/post request

2,response Web page back to the client's response

The properties of the 3,pagecontext Web page are managed here

4,session session period related to the request

What the 5,application servlet is doing

6,out used to transmit the output of the response

7,config Servlet's architectural components

8,page JSP Web page itself

9,exception Exceptions for error pages, not captured

57,http, the difference between the Get and post methods

1,get is a request to send data to the server, and post is a request to submit data to the server

2,get is to obtain information, not to modify information, like database query function, the data will not be modified

The parameters of the 3,get request will be passed after the URL, the requested data will be appended to the URL, in order to split the URL and transfer data, the parameters are connected with &, the XX in%xx for the symbol in 16 binary ASCII, if the data is English letter/number, as is sent, if it is a space , convert to +, and if it is Chinese/other characters, encrypt the string directly with BASE64.
There is a size limit to the data transmitted by 4,get, because get commits data through a URL, so the amount of data that can be submitted is directly related to the length of the URL, and different browsers have different limits on the length of the URL.

5,get requested data will be cached by the browser, the user name and password will appear in plaintext on the URL, others can find historical browsing records, the data is not very safe.

On the server side, use Request.QueryString to get the data submitted by the Get method
The POST request is sent to the Web server as the actual content of the HTTP message, the data is placed within the HTML header, and the post does not restrict the submitted data. Post is safer than get, when the data is in Chinese or not sensitive data, then use GET, because using GET, parameters will be displayed in the address, for sensitive data and not Chinese characters of data, then use post.
6,post represents the possibility of modifying a request for resources on a server, and the data submitted by post can only be obtained by using Request.Form on the server side.

(For reference only, if there is a better answer, welcome to explore)

58, what is a cookie? What is the difference between a session and a cookie?

A cookie is a session technology that saves a user's information to a browser object.

Difference:

(1) The cookie data is stored on the client's browser and the session data is placed on the server
(2) Cookies are not very safe, others can analyze cookies stored locally and cookie spoofing, if the main consideration is that security should use the session
(3) The session will be stored on the server for a certain period of time. When the increase in access, will be compared to occupy your server performance, if the main consideration to mitigate server performance, you should use cookies
(4) The limit of a single cookie on the client is 3K, that is, the cookie stored by a site at the client cannot be 3 K.

Conclusion:

Store important information, such as login information, as session, and other information, if required, can be placed in a cookie.

What are the differences between 59,jsp and servlets, the common denominator, and the scope of their respective applications?

JSP is the extension of servlet technology, which is essentially a simple way of servlet. The JSP is compiled with a "class servlet".

The main difference between Servlets and JSPs is that the application logic of the servlet is in the Java file and is completely detached from the HTML in the presentation layer. The case for JSP is that Java and HTML can be combined into a file with a. jsp extension.

JSPs focus on Views, and Servlets are used primarily for control logic. In the struts framework, the JSP is in the view layer of the MVC design pattern, and the servlet is in the control layer.

How do 60,tomcat containers create instances of servlet classes? What's the principle?
When the container starts, it reads the Web. xml file in all of the WebApps directories, and then parses and reads the servlet registration information. The servlet classes that are registered in each app are then loaded and instantiated in a reflection manner. (sometimes also instantiated on first request)
When a servlet is registered, add <load-on-startup>1</load-on-startup> if it is a positive number, it is instantiated at the beginning, and if it is not written or negative, the first request is instantiated.

Java bosom friend of the public to organize some of the major companies commonly used to interview pen questions, for everyone in the daily spare time to learn a few of the topics, the cumulative, wait until the interview, all the time, the interview will naturally be at ease.

Java face question-javaweb article sevenoriginal 2017-09-02 Amu java companion

61,jdbc what are the basic steps to accessing a database?
1, load Driver
2, gets the connection object through the DriverManager object connection
3, get the session through the Connection object
4, through the session of data deletion and modification, packaging objects
5, close the resource

62, talk about the difference between PreparedStatement and statement.
1, efficiency: A precompiled session is more than a normal session object, and the database system does not compile the same SQL statement again
2, security: can effectively avoid SQL injection attack! The SQL injection attack is to enter some illegal special characters from the client, and the server side can still construct the SQL statement correctly, thus collecting the program and the server information and data.
For example: "SELECT * from t_user where userName = '" + userName + "' and password = '" + Password + "'"
If the username and password are entered as ' 1 ' or ' 1 ' = ' 1 '; The SQL statements that are produced are:
"SELECT * from t_user where userName = ' 1 ' or ' 1 ' = ' 1 ' and password = ' 1 ' or ' 1 ' = ' 1 ' The where part of this statement does not play a role in data filtering.

63, talk about the concept of transactions, the steps to handle transactions in JDBC programming.
1 transactions are a series of operations performed as a single logical unit of work.
2, a logical unit of work must have four properties, called atomicity, consistency, isolation, and persistence (ACID) properties, to be a transaction
Transaction process steps:
3,conn.setautocomit (false); Set submit by hand
4,conn.commit () COMMIT Transaction
5, there is an exception, rollback conn.rollback ();

64, the principle of database connection pooling. Why you use connection pooling.
1, a database connection is a time-consuming operation, and a connection pool enables multiple operations to share a single connection.
2, the basic idea of database connection pool is to establish a "buffer pool" for database connection. A certain number of connections are pre-placed in the buffer pool, and when a database connection needs to be established, simply take one out of the buffer pool and put it back when you are finished. We can prevent the system from endlessly connecting to the database by setting the maximum number of connections to the connection pool. More importantly, we can monitor the number and usage of the database connection through the connection pool management mechanism, and provide the basis for system development, testing and performance adjustment.
3, connection pooling is used to improve the management of database connection resources

what is the dirty reading of 65,JDBC? What kind of database isolation level prevents dirty reads?

When we use transactions, it is possible that a row of data has just been updated, while another query reads the newly updated value. This results in dirty reads, because the updated data is not persisted, and the business that updates the row of data may be rolled back so that the data is invalid. Database of transactionreadcommitted,transactionrepeatableread, and transaction_serializable The isolation level prevents dirty reads.

66, what is phantom reading and which isolation level prevents phantom reading?

Phantom reading refers to a transaction that executes a query multiple times and returns a different value. Suppose a transaction is querying a data based on a condition, and then another transaction inserts a row of data that satisfies the query criteria. The transaction then executes the query again, and the returned result set contains the new data that was just inserted. This new line of data is called Phantom-line, and this phenomenon is called Phantom reading.

Only the transaction_serializable isolation level can prevent phantom reads from being generated.

What is 67,jdbc's drivermanager for?
The JDBC DriverManager is a factory class that we use to create a database connection. When the driver class of JDBC is loaded, it registers itself in the DriverManager class.
Then we'll pass the database configuration information into the Drivermanager.getconnection () method, and DriverManager will use the driver registered to it to get the database connection and return it to the calling program.

What is the difference between 68,execute,executequery,executeupdate?
The Execute (String query) method of 1,statement is used to execute any SQL query, and if the result of the query is a resultset, this method returns True. If the result is not resultset, such as INSERT or update query, it will return false. We can get the resultset by its Getresultset method, or get the updated number of records by the Getupdatecount () method.
The 2,statement executequery (String query) interface is used to execute a select query and returns resultset. The resultset returned by the record will not be null, even if the query is not. We typically use ExecuteQuery to execute query statements, so that if an INSERT or UPDATE statement is passed in, it throws an error message "ExecuteQuery method can is not a used for update" The java.util.SQLException. ,
The 3,statement executeupdate (String query) method is used to execute an INSERT or update/delete (DML) statement, or nothing to return, for a DDL statement, the return value is of type int, if it is a DML statement , which is the updated number of bars and, if it is DDL, returns 0.
You should use the Execute () method only if you are not sure what the statement is, otherwise you should use the ExecuteQuery or Executeupdate method.

69,sql query out of the results of the page show generally how to do?

Oracle:

SELECT * FROM
(select *,rownum as tempid from student) t
where t.tempid between "+ pagesize* (pageNumber-1) +" and "+ Pagesize*pagenumber

Mysql:
SELECT * FROM students limit "+ pagesize* (pageNumber-1) +", "+ pageSize;

SQL Server:
Select Top "+ pageSize +" * from students where ID not in +
(select top "+ pageSize * (PAGENUMBER-1) + ID from the students Order by ID) +
"Order by ID;

What is the resultset of 70,JDBC?
A resultset is returned after querying the database, which is like a data table for the query result set.
The ResultSet object maintains a cursor that points to the current data row. At the beginning, this cursor is pointing to the first line. If the next () method that calls the ResultSet cursor moves down one line, the next () method returns False if there is no more data. You can use it in a for loop to traverse the dataset.
The default resultset is not updatable, and the cursor can only move down. This means that you can only iterate through the first line to the last line. However, you can also create resultset that can be rolled back or updatable

The ResultSet object is automatically closed when the statement object that generates the resultset is closed or re-executed or the next resultset is fetched.
The column data can be obtained by passing in the column name or the ordinal starting at 1 through the ResultSet getter method.

Java bosom friend of the public to organize some of the major companies commonly used to interview pen questions, for everyone in the daily spare time to learn a few of the topics, the cumulative, wait until the interview, all the time, the interview will naturally be at ease.

Java Face question-javaweb article

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.