由于servlet和JSP不是Java平台JavaSE(标准版)的一部分,而是Java EE(企业版)的一部分,因此,必须告知编译器servlet的位置。
解决“软件包 javax.servlet不存在”错误的方法:
1. 搜索servlet-api.jar
所在文件夹:E:TomcatSetuplib
2. 将环境变量CLASSPATH的值设置为:
.;E:TomcatSetuplibservlet-api.jar
3. 除了设置classpath以及servlet-api.jar外,还有一点!!!
把E:TomcatSetuplib下的SERVLET-API.JAR 拷贝到D:JAVAjdk1.8.0_60jrelibext下
一个简单的Servlet实例
1 package star.moon; 2 import java.io.*; 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 public class HelloBeijing extends HttpServlet 6 { public void init(ServletConfig config) throws ServletException 7 { super.init(config); 8 } 9 public void service(HttpServletRequest reqest,HttpServletResponse response) 10 throws IOException 11 { response.setContentType("text/html;charset=GB2312");//设置响应的MIME类型 12 PrintWriter out=response.getWriter();//获得一个向客户发送数据的输出流 13 out.println("<html><body>"); 14 out.println("<h2>北京奥运圆满成功!</h2>"); 15 out.println("</body></html>"); 16 } 17 }
1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <!-- 3 Licensed to the Apache Software Foundation (ASF) under one or more 4 contributor license agreements. See the NOTICE file distributed with 5 this work for additional information regarding copyright ownership. 6 The ASF licenses this file to You under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 --> 18 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 19 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 20 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 21 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 22 version="3.1" 23 metadata-complete="true"> 24 25 <display-name>Welcome to Tomcat</display-name> 26 <description> 27 Welcome to Tomcat 28 </description> 29 30 31 32 <servlet> 33 <servlet-name>hello</servlet-name> 34 <servlet-class>star.moon.HelloBeijing</servlet-class> 35 </servlet> 36 <servlet-mapping> 37 <servlet-name>hello</servlet-name> 38 <url-pattern>/lookHello</url-pattern> 39 </servlet-mapping> 40 41 42 </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文件如下:
1 <%@ page contentType="text/html;charset=GB2312" %> 2 <HTML><BODY ><Font size=2> 3 <P>输入一个数,提交给servlet(Post方式): 4 <FORM action="http://localhost:8081/GetSquare" method=post> 5 <Input Type=text name=number> 6 <Input Type=submit value="提交"> 7 </FORM> 8 <P>输入一个数,提交给servlet(Get方式): 9 <FORM action="http://localhost:8081/GetSquareOrCubic" method=get> 10 <Input Type=text name=number> 11 <Input Type=submit value="提交"> 12 </FORM> 13 <P>输入一个数,提交给servlet(Post方式): 14 <FORM action="http://localhost:8081/GetSquareOrCubic" method=post> 15 <Input Type=text name=number> 16 <Input Type=submit value="提交"> 17 </FORM> 18 </BODY></HTML>
两个Java文件分别如下所示:
1 package star.moon; 2 import java.io.*; 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 public class GetSquareOrCubic extends HttpServlet 6 { public void init(ServletConfig config) throws ServletException 7 {super.init(config); 8 } 9 public void doPost(HttpServletRequest request,HttpServletResponse response) 10 throws ServletException,IOException 11 { response.setContentType("text/html;charset=GB2312"); 12 PrintWriter out=response.getWriter(); 13 out.println("<html><body>"); 14 String number=request.getParameter("number"); //获取客户提交的信息 15 double n=0; 16 try{ n=Double.parseDouble(number); 17 out.print("<BR>"+number+"的平方是:"); 18 out.print("<BR>"+n*n); 19 } 20 catch(NumberFormatException e) 21 { out.print("<H1>请输入数字字符! </H1>"); 22 } 23 out.println("</body></html>"); 24 } 25 public void doGet(HttpServletRequest request,HttpServletResponse response) 26 throws ServletException,IOException 27 { response.setContentType("text/html;charset=GB2312"); 28 PrintWriter out=response.getWriter(); 29 out.println("<html><body>"); 30 String number=request.getParameter("number"); //获取客户提交的信息 31 double n=0; 32 try{ n=Double.parseDouble(number); 33 out.print("<BR>"+number+"的立方是:"); 34 out.print("<BR>"+n*n*n); 35 } 36 catch(NumberFormatException e) 37 { out.print("<H1>请输入数字字符! </H1>"); 38 } 39 out.println("</body></html>"); 40 } 41 }
1 package star.moon; 2 import java.io.*; 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 public class GetSquare extends HttpServlet 6 { public void init(ServletConfig config) throws ServletException 7 {super.init(config); 8 } 9 public void doPost(HttpServletRequest request,HttpServletResponse response) 10 throws ServletException,IOException 11 { response.setContentType("text/html;charset=GB2312"); 12 PrintWriter out=response.getWriter(); 13 out.println("<html><body>"); 14 String number=request.getParameter("number"); //获取客户提交的信息 15 double n=0; 16 try{ n=Double.parseDouble(number); 17 out.print("<BR>"+number+"的平方是:"); 18 out.print("<BR>"+n*n); 19 } 20 catch(NumberFormatException e) 21 { out.print("<H1>请输入数字字符! </H1>"); 22 } 23 out.println("</body></html>"); 24 } 25 public void doGet(HttpServletRequest request,HttpServletResponse response) 26 throws ServletException,IOException 27 { 28 doPost(request,response); 29 } 30 }
web.xml文件如下:
1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <!-- 3 Licensed to the Apache Software Foundation (ASF) under one or more 4 contributor license agreements. See the NOTICE file distributed with 5 this work for additional information regarding copyright ownership. 6 The ASF licenses this file to You under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 --> 18 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 19 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 20 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 21 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 22 version="3.1" 23 metadata-complete="true"> 24 25 <display-name>Welcome to Tomcat</display-name> 26 <description> 27 Welcome to Tomcat 28 </description> 29 30 31 32 33 <servlet> 34 <servlet-name>getSquare</servlet-name> 35 <servlet-class>star.moon.GetSquare</servlet-class> 36 </servlet> 37 <servlet-mapping> 38 <servlet-name>getSquare</servlet-name> 39 <url-pattern>/GetSquare</url-pattern> 40 </servlet-mapping> 41 42 43 <servlet> 44 <servlet-name>getSquareOrCubic</servlet-name> 45 <servlet-class>star.moon.GetSquareOrCubic</servlet-class> 46 </servlet> 47 <servlet-mapping> 48 <servlet-name>getSquareOrCubic</servlet-name> 49 <url-pattern>/GetSquareOrCubic</url-pattern> 50 </servlet-mapping> 51 52 </web-app>