由于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. hammer.js中文文档

    转自:http://www.uedsc.com/hammerjs-api.html HammerJS是一个优秀的.轻量级的触屏设备手势库,现在已经更新到2.04版本,跟1.0版本有点天壤地别了,毕竟改 ...

  2. jquery中的get和set

    jquery中通过参数的个数来判断是get方法还是set方法: css: function(name, value ) { return value !== undefined ? jQuery.st ...

  3. Spring小练习之宝宝淘项目

    数据库准备 # 表结构 CREATE TABLE `t01_user` ( `) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `) DEFAULT NULL COM ...

  4. 一段后台C#查询SQL Server数据库代码

    using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.W ...

  5. Code First开发系列之管理并发和事务(转)

    转自:http://www.cnblogs.com/farb/p/ConcurrencyAndTransctionManagement.html 返回<8天掌握EF的Code First开发&g ...

  6. Java系列:JVM指令详解(下)(zz)

    九.自增减指令    20:iconst_1    21:istore_1    22:return 指令码      助记符                                     ...

  7. Linux及安全——Linux基础实践

    Linux及安全——Linux基础实践 一.实践一:掌握软件源的维护方法,配置系统使用教育网内的软件源镜像.掌握通过软件源来查找,安装,卸载,更新软件的方法. 1.软件源的维护方法 Ubuntu的软件 ...

  8. 20135328信息安全系统设计基础第二周学习总结(vim、gcc、gdb)

    第三周学习笔记 学习计时:共8小时 读书:1 代码:5 作业:1 博客:7 一.学习目标 熟悉Linux系统下的开发环境 熟悉vi的基本操作 熟悉gcc编译器的基本原理 熟练使用gcc编译器的常用选项 ...

  9. 5332盛照宗 如何获取新技能+c语言学习调查

    如何获取新技能+c语言学习调查 你有什么技能比大多人(超过90%以上)更好? 如果问我有没有什么技能比大多数人,并且是90%的人好,我还真不敢说有,因为世界上有70亿人,要比63亿人做的好才行啊.我也 ...

  10. iOS开发--应用设置及用户默认设置——转载

    [链接]iOS开发--应用设置及用户默认设置[1.bundlehttp://www.jianshu.com/p/6f2913f6b218 在iphone里面,应用都会在“设置”里面有个专属的应用设置, ...