Talk about the life cycle of Servlet?
Servlet has a good definition of life cycle, including loading and instantiation, initialization, and processing The request and service end. This lifetime is expressed by the init(), service() and destroy methods of the javax.servlet.Servlet interface. (Recommended study: java interview questions)
After the Servlet is instantiated by the server, the container runs its init method, runs its service method when the request arrives, and the service method automatically dispatches the execution corresponding to the request. doXXX methods (doGet, doPost), etc., call its destroy method when the server decides to destroy the instance.
The web container loads the servlet and the life cycle begins. Initialize the servlet by calling the servlet's init() method. This is achieved by calling the service() method, and different do***() methods are called according to different requests. To end the service, the web container calls the servlet's destroy() method.
What is the difference between forward() and redirect() in Servlet API?
1. From the address bar display,
forward means that the server requests resources. The server directly accesses the URL of the target address and puts the The response content is read and then sent to the browser. The browser does not know where the content sent by the server comes from, so its address bar is still the original address.
redirect is the server side According to the logic, a status code is sent to tell the browser to request that address again. So the address bar displays the new URL. So redirect means that the client sends two requests to the server and also accepts two responses.
2. From the perspective of data sharing
forward: the forwarded page and the forwarded page can share the data in the request.
redirect: cannot Shared data.
redirect can not only redirect to other resources of the current application, but also redirect to resources in other applications on the same site, and even redirect to other sites using absolute URLs Resources.
The forward method can only forward requests between resources within the same Web application. Forward is an operation within the server.
redirect is when the server notifies the client and allows the client to The client re-initiates the request.
So, you can say that redirect is an indirect request, but you cannot say "whether a request belongs to forward or redirect"
3. From the application Locally speaking
forward: Generally used when a user logs in, forwarded to the corresponding module according to the role.
redirect: Generally used to return to the main page and jump when the user logs out. Go to other websites, etc.
4. In terms of efficiency
forward: high.
redirect: low.
What is the difference between request.getAttribute() and request.getParameter()??
1, request.getParameter() is obtained through the implementation of the container Data passed in through methods like post, get, etc.
request.setAttribute() and getAttribute() only flow within the web container and are only in the request processing stage.
2, getAttribute returns an object, getParameter returns a string
3, getAttribute() is always used together with setAttribute(), only after setting it with setAttribute() first, The value can be obtained through getAttribute(), which passes Object type data. And it must be used in the same request object to be valid.
And getParameter() is to receive the parameters submitted by the get or post of the form
The difference between static inclusion and dynamic inclusion of jsp
1, < ;%@include file="xxx.jsp"%> is a compilation instruction in jsp. The inclusion of its file occurs during the conversion of jsp to servlet, and
2. Using static inclusion will only generate one class file, while using dynamic inclusion Multiple class files will be generated
3. Use static inclusion. The request object of the containing page and the included page is the same object, because static inclusion only copies the content of the included page to the included page;
Dynamic inclusion of the containing page and the included page are not the same page. The request object of the included page can obtain a relatively larger range of parameters. Not only can the parameters passed to the containing page be obtained, but also the parameters passed to the containing page can be obtained. You can also get the parameters passed down in the included page
What technologies are used to implement each part of MVC? How to implement it?
MVC is Model-View-Controller abbreviation. Model represents the business logic of the application (implemented through JavaBeans and EJB components), View is the presentation surface of the application (generated by JSP pages), and Controller provides process control of the application (usually a Servlet). Through this design model Divide application logic, processing and display logic into different component implementations. These components can be interacted with and reused.
What are the built-in objects in jsp? What are their functions?
JSP has the following 9 built-in objects:
1, request client request, this request will contain parameters from GET/POST request
2, response web page returns the response from the client
3, pageContext web page attribute is in Here management
4, session session related to the request
5, application servlet is executing content
6, out is used to transmit the output of the response
7, config servlet architecture components
8, page JSP web page itself
9, exception for error web pages, uncaught exceptions
Http, The difference between get and post methods
1. Get is a request to the server for data, while Post is a request to submit data to the server
2. Get is Obtain information instead of modifying information, similar to the database query function, the data will not be modified
3. The parameters of the Get request will be passed after the URL, and the requested data will be appended to the URL to? Split the URL and transmit the data. The parameters are connected with &. The XX in %XX is ASCII expressed in hexadecimal notation. If the data is English letters/numbers, send it as it is. If it is a space, convert it to, if it is Chinese. / For other characters, directly encrypt the string with BASE64.
4. There is a size limit on the data transmitted by Get. Because GET submits data through the URL, the amount of data that can be submitted by GET is directly related to the length of the URL. Different browsers have different requirements on the length of the URL. The restrictions are different.
5. The data requested by GET will be cached by the browser, and the username and password will appear in clear text on the URL. Others can check the historical browsing records, and the data is not safe.
On the server side, use Request.QueryString to obtain the data submitted by the Get method.
The Post request is sent to the web server as the actual content of the http message, and the data is placed in the HTML Header for submission. Post does not limit the data submitted. Post is safer than Get. When the data is in Chinese or insensitive data, use get because with get, the parameters will be displayed in the address. For sensitive data and data that are not Chinese characters, use post.
6, POST represents a request that may modify resources on the server. On the server side, data submitted in the Post method can only be obtained using Request.Form.
What are cookies? What is the difference between Session and cookie?
Cookie is a session technology that saves user information to the browser object.
Difference:
(1) Cookie data Stored on the client's browser, session data is placed on the server
(2) Cookies are not very safe. Others can analyze the COOKIE stored locally and conduct COOKIE deception. If security is the main consideration, session
(3) The session will be saved on the server within a certain period of time. When visits increase, it will take up more of your server's performance. If you mainly consider reducing server performance, you should use COOKIE
(4) The limit of a single cookie on the client is 3K, which means that a website is stored on the client The cookie cannot be 3K.
Conclusion:
Store important information such as login information as SESSION; if other information needs to be retained, it can be placed in COOKIE.
What are the differences, common points, and scope of application between jsp and servlet?
JSP is an extension of Servlet technology, which is essentially a simple way of Servlet. After JSP is compiled, it is a "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 the case of JSP, Java and HTML can be combined into a file with a .jsp extension.
JSP focuses on views, and Servlet is mainly used for control logic. In the struts framework, JSP is located in the view layer of the MVC design pattern, and Servlet is located in the control layer.
How does the tomcat container create a servlet class instance? What principles are used?
When the container starts, it will read the web.xml files in all web applications in the webapps directory, then parse the xml files, and read the servlet registration information. Then, the servlet classes registered in each application are loaded and instantiated through reflection. (Sometimes it is also instantiated on the first request)
Add
The above is the detailed content of JavaWeb interview questions (1). For more information, please follow other related articles on the PHP Chinese website!