在cmd下编译一个简单的servlet时出现程序包javax.servlet不存在
由于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不存在的更多相关文章
- 使用Maven编译项目时提示程序包javax.servlet.http不存在
将apache-tomcat-8.0.23\lib下的servlet-api.jar拷贝到C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext下即可
- java编译错误 程序包javax.servlet不存在javax.servlet.*
java编译错误 程序包javax.servlet不存在javax.servlet.* 编译:javac Servlet.java 出现 软件包 javax.servlet 不存在 软件包javax. ...
- 程序包javax.servlet.annotation不存在
1.错误描写叙述 [INFO] Scanning for projects... [INFO] [INFO] --------------------------------------------- ...
- 程序包javax.servlet.http不存在
在maven test项目时,出现错误: java:[7,26] 程序包javax.servlet.http不存在 原因:pom.xml中未引入javax.servlert-api相关的包 <d ...
- java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException or 程序包 javax.servlet 不存在
遇到下面这个问题 程序包 javax.servlet 不存在 或者 java.util.concurrent.ExecutionException: org.apache.catalina.Lifec ...
- 【转】java编译错误 程序包javax.servlet不存在javax.servlet.*
转载地址:http://blog.163.com/gis_warrior/blog/static/1936171732012811071642/ 编译:javac Servlet.java 出现 软件 ...
- 程序包 javax.servlet 不存在 解决办法
其原因是java编译器没有找到软件包javax.servlet. 下载servlet.jar放到lib下没有效果,后发现需要在jdk中添加,如下: 解决办法: 从tomcat lib目录下拷贝一个se ...
- 解决Idea项目启动报错:程序包javax.servlet.http不存在
报错信息 在没有使用maven的时候,web项目从远程仓库获取下以后,起一次启动往往会报错javax.servlet.http程序包找不到,随之而来的java基础包都将不能使用,报错信息如下: 解决方 ...
- IntelliJ IDEA 出现" java: 程序包javax.servlet不存在、 java: 程序包javax.servlet.annotation"等错误
在IDEA中建立Servlet使用javax.servlet.http.HttpServlet等类时,出现了如下错误: 原因:IntelliJ IDEA 没有导入 servlet-api.jar 这个 ...
随机推荐
- c# 二进制或算法实现枚举的HasFlag函数
from:http://www.cnblogs.com/icyJ/archive/2013/02/20/HasFlag.html 在权限的管理中,常常会出现一个权限包含的现象.例如,有三种基本权限:职 ...
- Git之分支创建策略
分支策略:git上始终保持两个分支,master分支与develop分支.master分支主要用于发布时使用,而develop分支主要用于开发使用. 创建master的分支developgit che ...
- ASP.NET错误处理的方式(总结)
转载至: http://www.cnblogs.com/chinhr/archive/2007/06/26/795947.html ASP.NET错误处理的方式(整理&总结)英文文章研究:ht ...
- C# 结构体
1,结构体不能出现在继承关系中,除了继承接口. 结构体不能继承类或结构,也不能被类或结构继承,只可以继承接口. 2,struct不能定义默认构造函数(无参构造函数),也不能定义析构函数.class对这 ...
- 完成一个MVC+Nhibernate+Jquery-EasyUI信息发布系统
一.最近学习了Jquery-EasyUI框架,结合之前用过的MVC3+Nhibernate做一个信息发布系统,对工作一年半的自己做一个总结吧!(也正好 供初学者学习!) 二.先上截图(系统简介),让大 ...
- NET中MSMQ的使用----附例子
目录 一:MSMQ的一些理论上的知识 二:队列类型(Queue Type) 三:安装消息队列 四:在C#中Messagequeue class 五:MSMQ-发送消息到远程专用队列 六:例子 一. ...
- 【转】使用sklearn优雅地进行数据挖掘
这里是原文 目录 使用sklearn进行数据挖掘 1.1 数据挖掘的步骤 1.2 数据初貌 1.3 关键技术并行处理 并行处理 2.1 整体并行处理 2.2 部分并行处理流水线处理自动化调参持久化回顾 ...
- 1从零开始学习Xamarin.iOS安装篇
安装和配置xamarin.ios 最近.net 开源新闻很火呀,于是想学习xamarin,早1年前就了解过这个东西,但是一直没有时间来学习,我这里装的是MAC上面的版本,废话不多说开始第一步安装. 概 ...
- 流媒体技术之RTSP
流媒体技术之RTSP 标签: RTSP技术移动流媒体 2016-06-19 18:48 38人阅读 评论(0) 收藏 举报 分类: 流媒体相关技术 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
- [CareerCup] 10.7 Simplified Search Engine 简单的搜索引擎
10.7 Imagine a web server for a simplified search engine. This system has 100 machines to respond to ...