day_2 servlet 三种开发方法

来源:互联网 发布:js post方式下载文件 编辑:程序博客网 时间:2024/04/30 16:31
在C:\tomcat\apache-tomcat-7.0.52\webapps目录下创建自己的网络站点例如“RevenWebsite”(直接创建文件夹)在RevenWebsite目录下创建“WEB-INF”在“WEB-INF”中创建classes文件夹、lib文件夹在“WEB-INF”中创建web.xml文件在该xml文件下写入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://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0"  metadata-complete="true">  <display-name>Welcome to Tomcat</display-name>  <description>     Welcome to Tomcat  </description>       </web-app>//第一个sevlet,实现servlet接口的方式来开发//在上述classes文件夹中建立该java文件 另外两种方法同样在该文件夹建立。package test1;import javax.servlet.*;import java.io.*;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import java.io.IOException;public class hallo implements Servlet{    /*     该函数用于初始化该serlvet(类似于类的构造函数)     该函数只会被调用一次     (用户第一次访问时)     */    public void init(ServletConfig parm1) throws ServletException {        // TODO: 在这添加你的代码        System.out.println("init it");//为探寻运行顺序,在此输出一下    }    /**     * Method getServletConfig     *     *     * @return     *     */    public ServletConfig getServletConfig() {        // TODO: 在这添加你的代码        return null;    }    /*     这个函数用于处理业务逻辑     程序员应该把业务代码写在这里     当用户每访问该servlet时,都会调用     req:用于获得客户端(浏览器)信息     res:用于向客户端(浏览器)返回信息     */    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {        // TODO: 在这添加你的代码        System.out.println("servie it");        //从res中得到PrintWriter        PrintWriter pw = res.getWriter();        pw.println("hello,world!");    }    /**     * Method getServletInfo     *     *     * @return     *     */    public String getServletInfo() {        // TODO: 在这添加你的代码        return "";    }    /*     *销毁servlet实例(释放内存)     *1.reload该serlvet(webApps)     *2.关闭tomcat     *3.关机     */    public void destroy() {        // TODO: 在这添加你的代码        System.out.println("destory!");    }    }然后再次修改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://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0"  metadata-complete="true">  <display-name>Welcome to Tomcat</display-name>  <description>     Welcome to Tomcat  </description>    <!--JSPC servlet mapping start-->          <servlet> <servlet-name>helloworld</servlet-name><!--servlet name 自定义--> <servlet-class>test1.hallo</servlet-class><!--package.classname-->    </servlet>    <servlet-mapping>      <servlet-name>helloworld</servlet-name>      <url-pattern>/helloworld</url-pattern><!--域名-->    </servlet-mapping>     <!--JSPC servlet mapping end-->       </web-app>多出的段落是给自己的servlet开发项目配置tomcat完成以上步骤后,打开tomcat,在浏览器中输入http://localhost:8080/RevenWebsite/helloworld出现“hello world”,至此第一个servlet app开发完毕以下方法和上述步骤基本一致。//通过继承GenericServlet来实现seevlet开发package test1;import javax.servlet.*;import javax.servlet.GenericServlet;import java.io.*;public class HelloWorldGen extends GenericServlet{    public void service (ServletRequest Req,ServletResponse Res){        /*     这个函数用于处理业务逻辑     程序员应该把业务代码写在这里     当用户每访问该servlet时,都会调用     req:用于获得客户端(浏览器)信息     res:用于向客户端(浏览器)返回信息     */        try{            PrintWriter pw = Res.getWriter();            pw.println("HelloWorld in GenericServlet!");        }        catch(Exception ex){            ex.printStackTrace();        }    }    } 在xml文件中加上相应配置     <servlet>      <servlet-name>helloworldgen</servlet-name>      <servlet-class>test1.HelloWorldGen</servlet-class>    </servlet>    <servlet-mapping>      <servlet-name>helloworldgen</servlet-name>      <url-pattern>/helloworldgen</url-pattern>    </servlet-mapping>//通过继承HttpServlet开发Servlet(最终方法)package test1;import javax.servlet.http.*;import java.io.*;public class HelloHttp extends HttpServlet{    public void doGet(HttpServletRequest req,HttpServletResponse res){        this.doPose(req,res);        //将Get和Pose请求一起处理    }    public void doPose(HttpServletRequest req,HttpServletResponse res){        /*     这个函数用于处理业务逻辑     程序员应该把业务代码写在这里     当用户每访问该servlet时,都会调用     req:用于获得客户端(浏览器)信息     res:用于向客户端(浏览器)返回信息     */        try{            PrintWriter pw = res.getWriter();            pw.println("Hello,World!in Http");    }        catch(Exception ex){            ex.printStackTrace();        }            }} 在xml文件中加上相应配置     <servlet>      <servlet-name>helloworldhttp</servlet-name>      <servlet-class>test1.HelloHttp</servlet-class>    </servlet>    <servlet-mapping>      <servlet-name>helloworldhttp</servlet-name>      <url-pattern>/helloworldhttp</url-pattern>    </servlet-mapping>注意事项:在第一次写web.xml文件时,写完要开启tomcat检查一边,如果tomcat报错,出现“严重”字样则有可能是xml文件没有写好的缘故  如果出现“at xxx javaX xxxxx”字样,则是java文件编写问题。

0 0
原创粉丝点击