由于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实例

 package star.moon;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloBeijing extends HttpServlet
{ public void init(ServletConfig config) throws ServletException
{ super.init(config);
}
public void service(HttpServletRequest reqest,HttpServletResponse response)
throws IOException
{ response.setContentType("text/html;charset=GB2312");//设置响应的MIME类型
PrintWriter out=response.getWriter();//获得一个向客户发送数据的输出流
out.println("<html><body>");
out.println("<h2>北京奥运圆满成功!</h2>");
out.println("</body></html>");
}
}
 <?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the 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"
version="3.1"
metadata-complete="true"> <display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description> <servlet>
<servlet-name>hello</servlet-name>
<servlet-class>star.moon.HelloBeijing</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/lookHello</url-pattern>
</servlet-mapping> </web-app>

Servlet的声明周期:

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

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

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

doGet()方法和doPost()方法

可以在Servlet类中重写doPost()方法或doGet()方法来响应用户的请求,如果不论用户请求类型是POST还是GET,服务器的处理过程完全相同,那么我们可以只在

doPost()方法中编写处理过程,而在doGet()方法中再调用doPost()方法即可,或只在doGet()方法中编写处理过程,而在doPost()方法中再调用doGet()方法。

如果根据请求的类型进行不同的处理,就需在两个方法中编写不同的处理过程。

jsp文件如下:

 <%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY ><Font size=2>
<P>输入一个数,提交给servlet(Post方式):
<FORM action="http://localhost:8081/GetSquare" method=post>
<Input Type=text name=number>
<Input Type=submit value="提交">
</FORM>
<P>输入一个数,提交给servlet(Get方式):
<FORM action="http://localhost:8081/GetSquareOrCubic" method=get>
<Input Type=text name=number>
<Input Type=submit value="提交">
</FORM>
<P>输入一个数,提交给servlet(Post方式):
<FORM action="http://localhost:8081/GetSquareOrCubic" method=post>
<Input Type=text name=number>
<Input Type=submit value="提交">
</FORM>
</BODY></HTML>

两个Java文件分别如下所示:

 package star.moon;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetSquareOrCubic extends HttpServlet
{ public void init(ServletConfig config) throws ServletException
{super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ response.setContentType("text/html;charset=GB2312");
PrintWriter out=response.getWriter();
out.println("<html><body>");
String number=request.getParameter("number"); //获取客户提交的信息
double n=0;
try{ n=Double.parseDouble(number);
out.print("<BR>"+number+"的平方是:");
out.print("<BR>"+n*n);
}
catch(NumberFormatException e)
{ out.print("<H1>请输入数字字符! </H1>");
}
out.println("</body></html>");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ response.setContentType("text/html;charset=GB2312");
PrintWriter out=response.getWriter();
out.println("<html><body>");
String number=request.getParameter("number"); //获取客户提交的信息
double n=0;
try{ n=Double.parseDouble(number);
out.print("<BR>"+number+"的立方是:");
out.print("<BR>"+n*n*n);
}
catch(NumberFormatException e)
{ out.print("<H1>请输入数字字符! </H1>");
}
out.println("</body></html>");
}
}
 package star.moon;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetSquare extends HttpServlet
{ public void init(ServletConfig config) throws ServletException
{super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ response.setContentType("text/html;charset=GB2312");
PrintWriter out=response.getWriter();
out.println("<html><body>");
String number=request.getParameter("number"); //获取客户提交的信息
double n=0;
try{ n=Double.parseDouble(number);
out.print("<BR>"+number+"的平方是:");
out.print("<BR>"+n*n);
}
catch(NumberFormatException e)
{ out.print("<H1>请输入数字字符! </H1>");
}
out.println("</body></html>");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
}

web.xml文件如下:

 <?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the 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"
version="3.1"
metadata-complete="true"> <display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description> <servlet>
<servlet-name>getSquare</servlet-name>
<servlet-class>star.moon.GetSquare</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getSquare</servlet-name>
<url-pattern>/GetSquare</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>getSquareOrCubic</servlet-name>
<servlet-class>star.moon.GetSquareOrCubic</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getSquareOrCubic</servlet-name>
<url-pattern>/GetSquareOrCubic</url-pattern>
</servlet-mapping> </web-app>

在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在的更多相关文章

  1. 使用Maven编译项目时提示程序包javax.servlet.http不存在

    将apache-tomcat-8.0.23\lib下的servlet-api.jar拷贝到C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext下即可

  2. java编译错误 程序包javax.servlet不存在javax.servlet.*

    java编译错误 程序包javax.servlet不存在javax.servlet.* 编译:javac Servlet.java 出现 软件包 javax.servlet 不存在 软件包javax. ...

  3. 程序包javax.servlet.annotation不存在

    1.错误描写叙述 [INFO] Scanning for projects... [INFO] [INFO] --------------------------------------------- ...

  4. 程序包javax.servlet.http不存在

    在maven test项目时,出现错误: java:[7,26] 程序包javax.servlet.http不存在 原因:pom.xml中未引入javax.servlert-api相关的包 <d ...

  5. java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException or 程序包 javax.servlet 不存在

    遇到下面这个问题 程序包 javax.servlet 不存在 或者 java.util.concurrent.ExecutionException: org.apache.catalina.Lifec ...

  6. 【转】java编译错误 程序包javax.servlet不存在javax.servlet.*

    转载地址:http://blog.163.com/gis_warrior/blog/static/1936171732012811071642/ 编译:javac Servlet.java 出现 软件 ...

  7. 程序包 javax.servlet 不存在 解决办法

    其原因是java编译器没有找到软件包javax.servlet. 下载servlet.jar放到lib下没有效果,后发现需要在jdk中添加,如下: 解决办法: 从tomcat lib目录下拷贝一个se ...

  8. 解决Idea项目启动报错:程序包javax.servlet.http不存在

    报错信息 在没有使用maven的时候,web项目从远程仓库获取下以后,起一次启动往往会报错javax.servlet.http程序包找不到,随之而来的java基础包都将不能使用,报错信息如下: 解决方 ...

  9. IntelliJ IDEA 出现" java: 程序包javax.servlet不存在、 java: 程序包javax.servlet.annotation"等错误

    在IDEA中建立Servlet使用javax.servlet.http.HttpServlet等类时,出现了如下错误: 原因:IntelliJ IDEA 没有导入 servlet-api.jar 这个 ...

随机推荐

  1. Python 栅栏凯撒

    def fence_Crypto(msg,priority="row"): ''' usage: fence_Crypto(msg[, priority])->msg to ...

  2. emberjs重写补充类之reopen方法和reopenClass方法

    无需一次性将类定义完全,你可以使用reopen方法来重新打开(reopen)一个类并为其定义新的属性. Person.reopen({ isPerson: true }); Person.create ...

  3. 在collection view中加入 NavigationController问题

    在开发过程中用collectionView集合视图的时候,用navgationController跳转会出现导航栏掩盖部分内容现象, 这时候需要在viewDidLoad里面填写 self.edgesF ...

  4. 了解了这些才能开始发挥jQuery的威力(转)

    链接:http://www.cnblogs.com/dolphinX/archive/2013/10/08/3347677.html 由于当前jQuery如此的如雷贯耳,相信不用介绍什么是jQuery ...

  5. 【转】【WPF】TemplateBinding和Binding的区别

    定义 TemplateBinding是为了某个特定场景优化出来的数据绑定版本--需要把ControlTemplate里面的某个Property绑定到应用该ControlTemplate的控件的对应Pr ...

  6. Socket Programming in C#--Introduction

    This is the second part of the previous article about the socket programming. In the earlier article ...

  7. drbd初探及Heartbeat+DRBD+MySQL

    1,drbd快速入门 http://www.mingxiao.info/article/?id=39#__RefHeading___Toc1114_501652171 2.Heartbeat+DRBD ...

  8. 小程序基础09:视图层之WXML

    1.WXML WXML是框架设计的一套标签语言,结合基础组件,事件系统,可以构建出页面的结构. 用以下的例子来看看WXML有什么能力: 1.1数据绑定 <view>{{age}}</ ...

  9. Android开发探秘之三:利用jsoup解析HTML页面

    这节主要是讲解jsoup解析HTML页面.由于在android开发过程中,不可避免的涉及到web页面的抓取,解析,展示等等,所以,在这里我主要展示下利用jsoup jar包来抓取cnbeta.com网 ...

  10. 20145208 实验二 Java面向对象程序设计

    20145208 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...