Jsp & amp; Servelet Study Notes (1)

Source: Internet
Author: User

1.1 write a servelet Program
1.2 write a jsp (preferred for SUN Enterprise Applications) Program
1.3 compile a Servlet Program
1.4 Package A Servlets and jsp (preferred for SUN Enterprise Applications) s program
1.5 build a deployment descriptor.
 
 
1.1 write a servelet Program
 
Problem
You want to write a servlet as a web application
Solution
Write a java class that inherits from javax. servlet. http. HttpServlet. First, you must import a class package servlet. jar. You will need the class library to compile this servlet program.
Discussion
A servlet program is a java class used to respond to the dynamic content of client requests over the network. If you are familiar with CGI programs, java-based servlets can completely replace CGI programs. It is usually called a web component. When a servlet program is executed in a runtime environment, it provides a servles container such as tomcat (a very useful JSP running platform) and bea weblogic.
Note: A web Container can be
Servlets is installed in a web container as part of a web application. This application will contain WEB resources. Configuration files such as HTML pages, images, multimedia clips, servlets, jsp (preferred for SUN Enterprise Applications) s and xml (more and more standardized, java runtime Class and Class Library when a web application is deployed to a web Container, this container will generate and load the java servlet class instance to the JVM to process servlet requests.
All servlets inherit the javax. servlet. servlet interface. Programmers who develop Web applications write a servlet
Inherited from javax. servlet. http. HttpServlet, an abstract class implements the Servlet interface and is specially designed to process HTTP requests.
When a web Container generates a servlet instance, the basic sequence is as follows:
1. The servlet container first calls the servlet's init () method, which will initialize a resource for the servlet to use. For example, a logger. The init () method is called only once throughout the servlet lifecycle.
2. The init () method initializes an object that inherits the java. servlet. ServletConfig interface. This object enables the servlet to initialize parameters declared in the deployment descriptor. ServletConfig also gives servlet the right to use a javax. servlet. servletContext object. With this object, servlet can record information, dispatch requests to other web components, and be able to use other web resources on the same application.
3. The servlet container calls the servlet service () method to respond to some servlet requests. According to HttpServlets, service () automatically calls the appropriate HTTP method to process the request by calling the servlet doGet () or doPost () method. In a few examples, the user sends a Post request. At this time, the servlet responds to this request by executing the doPost () method.
4. When two main HttpServlet doPost () and doGet () methods are called, the servlet container will generate javax .. servlet. http. httpServletRequest and HttpServletResponse objects and pass them as parameters to these request processing methods.
Http://www.knowsky.com/
5. Manage the lifecycle of a servlet, or determine the time when the servlet instance processes the request on the Java VM. When a servlet container starts to remove a servlet, it will call the servlet's destroy () method to release all resources, such as a database connection.
Example A typical HttpServlet
 
Package com. mydev;
Import java. io. IOException; import java. io. PrintWriter;
Import java. util. Enumeration; import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
// The HttpServlet interface must be inherited.
Public class FirstServlet extends HttpServlet ...{
Public void doGet (HttpServletRequest request, HttpServletRe

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.