在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 这个 ...
随机推荐
- View (四)视图状态及重绘流程分析
相 信大家在平时使用View的时候都会发现它是有状态的,比如说有一个按钮,普通状态下是一种效果,但是当手指按下的时候就会变成另外一种效果,这样才会给 人产生一种点击了按钮的感觉.当然了,这种效果相信几 ...
- 详解反射->Type.System
反射先了解 一:system.Type 获取基本信息: Type.Name //类名 Type.FullName //完整路径 Type.Namespace //空间名 public class ...
- C# WinForm 中Console 重定向输出到ListBox控件中显示
{ VoidAction action = { lstBox.Items. ...
- JavaScript---闭包和作用域链
作用域和作用域链: 参考文章 :http://www.cnblogs.com/malinlin/p/6028842.html http://www.cnblogs.com/lhb25/archive ...
- 用 eric6 与 PyQt5 实现python的极速GUI编程(系列01)--Hello world!
[题记] 我是一个菜鸟,这个系列是我的学习笔记. PyQt5 出来有一段时间了, PyQt5 较之 PyQt4 有一些变化,而网上流传的几乎都是 PyQt4 的教程,照搬的话大多会出错. eric6 ...
- SilverLight自定义ImageButton
SilverLight中XAML的写法和WPF一样,但是发现在自定义按钮上,没有WPF来的容易,下面说说我制作SilverLight中的ImageButton的一些思路. 在SilverLight中, ...
- MATLAB对于文本文件(txt)数据读取的技巧总结(经典中的经典)
振动论坛原版主eight的经典贴http://www.chinavib.com/thread-45622-1-1.html MATLAB对于文本文件(txt)进行数据读取的技巧总结(经典中的经典)由于 ...
- Opencv step by step - 图像融合
两个图像的融合就是像素的融合了,其实手动操作即可,用函数操作更方便了. 下面代码的作用是融合阿狸和doctor,很和谐有木有! #include <cv.h> #include <h ...
- Jenkins进阶系列之——14配置Jenkins用户和权限
今天给大家说说使用Jenkins专有用户数据库的配置,和一些常用的权限配置. 配置用户注册 在已运行的Jenkins主页中,点击左侧的系统管理—>Configure Global Securit ...
- hello,world!
这可以算是我的第一篇博客了吧?真正意义上的开始... 我于2016年6月毕业于山东英才学院信工学院计算机管理系,至今已有近半年.几经周转,总算是准备稳定下来了.于是,当初的博客想法提上了日程. 本人工 ...