Implement a servlet's HelloWorld applet (not applicable to eclipse)

Source: Internet
Author: User
Tags tomcat server

Implement a servlet's HelloWorld applet (not applicable to eclipse)

1. Build an application under WebApps in Tomcat Firstservlet (C:\tomcat\webapps\FirstServletweb)

2. Create a directory under the application

C:\tomcat\webapps\FirstServletweb\WEB-INF\classes

3. Create a file in the class directory Firstservlet.java

4. Implementing content

Package Cn.school;  Import java.io.*;  Import javax.servlet.*;  Extendsthrows Servletexception, java.io.IOException {outputstream out = res.getoutputstream (); Out.write ("Hello". GetBytes ());}}         

5. Compiling the servlet program

The problem occurs because the imported import javax.servlet.*; This package cannot be found.

The reason for this is that this is the time to start Javac, can only find the J2SE under the API, and can not find the API under the Java EE

6. Add the jar containing the servlet to the Classpath

The Tomcat server can run Tomcat, so it must have this jar package inside.

Directory C:\tomcat\lib There is a tomcat-api.jar below

So

The jar package is successfully imported

7. Recompile, success

A folder with more than one package in the classes directory

8. Configure an external access path for the servlet

configuring in the Web. xml file

Create a new Web. xml file under Web-inf

<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"Version= "3.0"><Servlet><Servlet-name>firstservlet</Servlet-name><Servlet-class>cn.school.firstservlet</Servlet-class></Servlet> <> < servlet-name>firstservlet </servlet-name > <url-pattern</url-pattern> </servlet-mapping ></web-app< Span style= "color: #0000ff;" >>                 

Configuring Servlet and Servlet mappings

9. Start the tomcat server and have access to the

Http://localhost:8080/FirstServletWeb/FirstServlet

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.