Answers to JSP and Servlet questions

Source: Internet
Author: User

1. What is the servlet lifecycle? What is the difference between Servlet and CGI?
A: When a Web container loads servlet, the lifecycle begins. Call the servlet Init () method to initialize the servlet. By calling the Service () method,
Call different do *** () methods based on different requests. End the service. The Web Container calls the Servlet's destroy () method.
The difference with CGI is that the servlet is in a server process and runs its service method in multiple threads. An instance can serve multiple requests,
In addition, the instance will not be destroyed, and CGI will generate a new process for each request, and the service will be destroyed after completion, so the efficiency is lower than servlet.
2. Describe the differences, commonalities, and application scopes between JSP and servlet.
A: Although JSP is essentially a servlet, the two are created in different ways. servlet is completely composed of Java program code and is good at Process Control and transaction processing.
It is not intuitive to generate dynamic web pages. jsp consists of HTML code and JSP tags, which can be used to conveniently compile dynamic web pages. Therefore, servlet is used in actual applications to control business processes.
JSP is used to generate dynamic web pages. In the Struts framework, JSP is located at the view layer of the MVC design mode, while servlet is located at the control layer.

3. What are the similarities and differences between the user's request to access the HTML page and the servlet?

Same: both use the HTTP protocol. difference: the latter must configure the URL path in Web. xml.

Example: assume that there is a helloservlet class in the helloapp application. Its configuration in the web. xml file is as follows:
<Servlet>
<Servlet-Name> helloservlet </servlet-Name>
<Servlet-class> org. javathinker. helloservlet </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-Name> helloservlet </servlet-Name>
<URL-pattern>/Hello </url-pattern>
</Servlet-mapping>
The URL for accessing helloservlet on the browser is:
Http: // localhost: 8080/helloapp/Hello

4. What are JSP commands?
A: JSP commands are divided into two types: Page commands and action commands.
Action Commands include usebean, setproperty, getproperty, forward, plugin, and include.
How does JSP use annotations?
Answer: 1) <% -- %> 2); //; 3)/*****/; 4) <! -->;

 
Related 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.