Li xucheng http://blog.csdn.net/javaeeteacher
1. What is the difference between Servlet and JSP? The Servlet and JSP functions are the same. They can both receive user requests, respond to users, and call business methods. The difference is that JSP is embedded with Java code or JSP tags in HTML or XML, and has advantages in page creation. You can use the webpage editing tool to create a webpage, then embed the Java or JSP tag. Servlet is pure Java code, which is usually used for control and not for output. In MVC mode, JSP usually acts as a view, while servlet usually acts as a controller. In addition, JSP also needs to be converted into servlet-like Java code during running. 2. What are the common methods of the servelt init method to complete service initialization, including doget and dopost, used to receive user requests, call the background JavaBean or EJB, and select the interface to respond to the user. The destroy method is used to release resources. 3. Let's talk about the servlet lifecycle. When a request is received, the container checks whether the corresponding servlet object exists. If not, load servetl and instantiate servlet, call the init Method for initialization. If the two objects already exist, create the request and response objects according to the user's request, and use these two objects as parameters to call the service method of the servlet object. Through this method, the servlet interacts with the user, after the method is executed, the request is processed. The servelt object continues waiting for the next request. When the application is uninstalled, call the destroy method to release the resource. Multiple requests share the servelt object. 4. Two main servlet API packages: javax. servlet. *; javax. servlet. http. *; 5. What is the class to be inherited by Servlet compiling? Httpservlet6. What are the two methods required to compile Servlet? What are the two parameters in doget and dopost Methods 7, doget and dopost? Httpservletrequest and httpservletresponse. The former encapsulates request-related information, and the latter encapsulates response-related information. To obtain the request information, obtain it from the first parameter. To respond to the user, use the second parameter. 8. To obtain user information, obtain the getparameter method and getparametervalues method of the request. The former is used to obtain the value of a single-value form element, or to obtain a multi-value, a typical check box. The former returns a string, and the latter returns a string array. If the specified form element does not exist, null is returned. 9. How do I complete the response to the user? Set the response content type: response. setcontenttype ("text/html; charset = gb2312"); get the output stream object: printwriter out = response. getwriter (); output information: Use the println Method 10 of the out statement. In servlet, select the method to respond to the user. Use the sendredirect method of requestdispatcher and response. If you use requestdispatcher: requestdispatcher RD = request, getrequestdispatcher ("target file"); Rd. forward (request, response); if you use the sendredirect method response. sendredirect ("target file"); 11. What are the differences between the above two methods? The forward method of requestdispatcher is equivalent to <JSP: Forward>. Similar to method call, when this line of code is executed, the forward method is used to switch to the execution target file, and the request and response are passed as parameters to the next page, so that the current page (servlet or JSP) the request is shared with the target page and can be passed through the request object. The sendredirect method of response is equivalent to sending a message to the client browser, asking the browser to request the target file again. From the user's perspective, it is equivalent to sending two requests, each request has an independent request and response object and cannot pass values between two pages through the request. From the address bar, the first file path is displayed in the address bar in the previous way, and the second file path is displayed in the latter way. 12. How to get the session object in servlet, how to get the cookie, use the getsession method of the request object to get the session, get cookie13 through getcookies, and in which file the servlet is configured? Configure in Web. xml under the WEB-INF of the Web application. 14. configuring servlet involves two steps: servlet declaration and Servlet access method declaration. Servlet Declaration: <servlet> <servlet-Name> servlet name (defined by yourself) </servlet-Name> <servlet-class> complete servlet class name </servlet-class> </servlet> servlet access method declaration <servlet-mapping> <servlet-name> servlet name (the name should be consistent with that declared) </servlet> <URL-pattern> access path </url-pattern> </servlet-mapping> 15. Where can the compiled servlet class be placed? Put it in the classes folder under the web application WEB-INF. 16. Suppose the IP address of the website is 1.2.3.4, port 8878, application name hello, Servlet access method/hello. How can I access this servlet now? Http: // 1.2.3.4: 8878/Hello/hello17. What is the role of servlet filter? The servlet filter is a special servlet that can filter specific request paths. before accessing this path, the servlet filter first performs preprocessing and determines whether to continue executing subsequent files. For typical applications, you can write user verification code in a filter, and then prepare the filtered path as the path of the file to be verified. 18. Let's talk about the role of the servlet listener. The servlet listener listens to specific events. When these events are generated, the code of the listener will be executed. You can load and uninstall applications, initialize and destroy sessions, and listen on events such as the change in the session value.
Li xucheng csdn blog: http://blog.csdn.net/javaeeteacher
Csdn student base camp: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5