码迷,mamicode.com
首页 > 编程语言 > 详细

在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在

时间:2016-07-30 19:52:58      阅读:1770      评论:0      收藏:0      [点我收藏+]

标签:

由于servlet和JSP不是Java平台JavaSE(标准版)的一部分,而是Java EE(企业版)的一部分,因此,必须告知编译器servlet的位置。

解决“软件包 javax.servlet不存在”错误的方法:

1. 搜索servlet-api.jar

所在文件夹:E:\TomcatSetup\lib

2. 将环境变量CLASSPATH的值设置为:

.;E:\TomcatSetup\lib\servlet-api.jar

 

3. 除了设置classpath以及servlet-api.jar外,还有一点!!!

把E:\TomcatSetup\lib下的SERVLET-API.JAR 拷贝到D:\JAVA\jdk1.8.0_60\jre\lib\ext下

 

 

一个简单的Servlet实例

 1 package star.moon;
 2 import java.io.*;
 3 import javax.servlet.*;
 4 import javax.servlet.http.*;
 5 public class HelloBeijing extends HttpServlet
 6 {  public void init(ServletConfig config) throws ServletException
 7     { super.init(config);
 8     }
 9    public void service(HttpServletRequest reqest,HttpServletResponse response)
10 throws IOException
11     {  response.setContentType("text/html;charset=GB2312");//设置响应的MIME类型
12        PrintWriter out=response.getWriter();//获得一个向客户发送数据的输出流
13        out.println("<html><body>");
14        out.println("<h2>北京奥运圆满成功!</h2>");
15        out.println("</body></html>");
16     } 
17 }
 1 <?xml version="1.0" encoding="ISO-8859-1"?>
 2 <!--
 3  Licensed to the Apache Software Foundation (ASF) under one or more
 4   contributor license agreements.  See the NOTICE file distributed with
 5   this work for additional information regarding copyright ownership.
 6   The ASF licenses this file to You under the Apache License, Version 2.0
 7   (the "License"); you may not use this file except in compliance with
 8   the License.  You may obtain a copy of the License at
 9 
10       http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 -->
18 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
19   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
21                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
22   version="3.1"
23   metadata-complete="true">
24 
25   <display-name>Welcome to Tomcat</display-name>
26   <description>
27      Welcome to Tomcat
28   </description>
29   
30   
31   
32  <servlet>
33         <servlet-name>hello</servlet-name>
34         <servlet-class>star.moon.HelloBeijing</servlet-class>
35     </servlet>
36     <servlet-mapping>
37         <servlet-name>hello</servlet-name>
38         <url-pattern>/lookHello</url-pattern>
39     </servlet-mapping>
40 
41 
42 </web-app>

 

Servlet的声明周期:

1、初始化Servlet对象。Servlet对象第一次被请求加载时,服务器初始化这个Servlet对象,即创建一个Servlet对象,对象调用init()方法完成必要的初始化工作。

2、诞生的Servlet对象再调用service()方法响应客户的请求。

3、当服务器关闭时,调用destroy()方法,消灭Servlet对象。

在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在

标签:

原文地址:http://www.cnblogs.com/xh0102/p/5721560.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!