How to get started with Servlet

Source: Internet
Author: User

1. Start with "P" of JSP, which page can be executed? Impossible! It will certainly be an object, that is, jsp is an object, but who will make it new; what is its class?

The first problem: tomcat is a new client. Yes, jsp must comply with certain writing specifications. This is to make tomcat better, or tomcat is a jsp Container.

The second question: <% = this instanceof HttpServlet %> is the answer.

2. "True" is the answer to the second question, indicating that JSP is an object of the HttpServlet subclass, which is a simplified version. Now we need to customize a class to do the same work as JSP:

Class MyServlet extends HttpServlet {

Public void deGet (HttpServletRequest req, HttpServletResponse resp ){

// Put the java code in jsp here: Where are the objects ?)

}

}

3. How to run it?

If we run it ourselves, we must do this:

HttpServlet MS = new Myservlet ();

Ms .. doGet (req, resp );

But how are two object arguments passed?

We hope that it runs like jsp, that is, it should meet the following four things for short ):

1) Automatic execution of each access

2) Automatic instantiation

3) The method is automatically called, and the real parameters passed by the method are also instantiated.

4) There should be a unique url to ensure it can be accessed.

All of this should be done by tomcat.

But what is the current situation ?...... We only have one MyServlet class. We need to do more to let Tomcat know about this class and do the "four things ".

4. How can we solve the "four things?

The WEB-INF/web. xml under the project is a magic file that can be addressed here together:

<! -Define a servlet here. tomcat can know it->

<Sevlet>

<Sevlet-name> name </servlet-name>

<Sevlet-class> package name. class Name </servlet-class>

</Sevlet>

<! -Specify the url here->

<Servlet-mapping>

<Sevlet-name> name </servlet-name>

<Url-parten>/url </url-parttern>

</Sevlet-mapping>

In this way, we can use http: // ip: port/project/url in the browser to access and execute the program in doGet.

 

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.