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