Because Servlets and JSPs are not part of the Java Platform Javase (Standard Edition), but are part of Java EE (Enterprise Edition), you must tell the location of the compiler servlet.
To resolve the "package Javax.servlet does not exist" error method:
1. Search Servlet-api.jar
Folder: E:\TomcatSetup\lib
2. Set the value of the environment variable CLASSPATH to:
.; E:\TomcatSetup\lib\servlet-api.jar
3. In addition to setting classpath and Servlet-api.jar, there is a point!!!
Copy the Servlet-api.jar under the E:\TomcatSetup\lib to D:\JAVA\jdk1.8.0_60\jre\lib\ext.
A simple servlet instance
1 PackageStar.moon;2 ImportJava.io.*;3 Importjavax.servlet.*;4 Importjavax.servlet.http.*;5 Public classHellobeijingextendsHttpServlet6{ Public voidInit (servletconfig config)throwsservletexception7{Super. init (config);8 }9 Public voidService (httpservletrequest reqest,httpservletresponse response)Ten throwsIOException One{Response.setcontenttype ("text/html;charset=gb2312");//set the MIME type of the response APrintWriter Out=response.getwriter ();//get an output stream that sends data to a customer -Out.println ("); -Out.println ("); theOut.println ("</body>); - } -}
1<?xml version= "1.0" encoding= "Iso-8859-1"?>2<!--3 Licensed to the Apache software Foundation (ASF) under one or more4 Contributor license agreements. See the NOTICE file distributed with5 ThisWork foradditional information regarding copyright ownership.6The ASF licenses ThisFile to under the Apache License, Version 2.07(the "License"); If you don't use Thisfile except in compliance with8 The License. Obtain a copy of the License at9 Tenhttp//www.apache.org/licenses/LICENSE-2.0 One A unless required by applicable law or agreed to writing, software -Distributed under the License is distributed on a "as is"BASIS, - without warranties or CONDITIONS of any KIND, either express or implied. theSee the License forThe specific language governing permissions and - limitations under the License. -- -<web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" +Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" -Xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee +http//xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd " AVersion= "3.1" atMetadata-complete= "true" > - -<display-name>welcome to Tomcat</display-name> -<description> - Welcome to Tomcat -</description> in - to +<servlet> -<servlet-name>hello</servlet-name> the<servlet-class>star.moon.hellobeijing</servlet-class> *</servlet> $<servlet-mapping>Panax Notoginseng<servlet-name>hello</servlet-name> -<url-pattern>/lookHello</url-pattern> the</servlet-mapping> + A the</web-app>
The declaration period of the servlet:
1. Initialize the Servlet object. The first time a Servlet object is requested to load, the server initializes the Servlet object, creating a Servlet object that invokes the Init () method to do the necessary initialization work.
2. The Servlet object that was born calls the service () method to respond to the client's request.
3. When the server shuts down, call the Destroy () method to destroy the Servlet object.
Package Javax.servlet does not exist when compiling a simple servlet under cmd