zoukankan      html  css  js  c++  java
  • servlet 学习笔记(二)

    ---------------------第二讲---------------------------------

    开发servlet有三种方法:

      1.实现servlet接口(最原始的)

      实现接口的5个方法,具体在下面的代码中体现

      2.继承GenericServlet(较少使用)

           通过GenericServlet去开发servlet,只需要重写service方法,相对来说要简单一些。

      3.继承HttpServlet(普遍使用的)

          通过HttpServlet去开发servlet,需要重写doGet、doPost方法,这是目前用的最多的一种方法。

    表单提交数据get请求和post请求的区别:

    1.从安全性看get<post。get提交的数据会在浏览器的地址栏显示

    2.从提交的内容大小看get<post。get提交的数据不能大于2K,而post提交的数据理论上不受限制,但是实际编程中建议不要大于64K

    3.从请求响应速度看get>post。get请求服务器立即处理请求,而post请求可能形成一个队列请求

    -------------------------------------------------------------------------------------------------------------

    servlet的生命周期

    servlet部署在容器里(我们使用的是Tomcat,也可是别的,比如jboss,weblogic。。。),它的生命周期由容器来管理。

    servlet的生命周期分为以下几个阶段:

      1.装载servlet,由相应的容器来完成

      2.创建一个servlet实例

      3.调用servlet的init()方法,该方法只会在第一次访问servlet时被调用一次

      4.服务:调用servlet的service()方法,一般业务逻辑在这里处理,该方法在访问该servlet时,会被调用

      5.销毁:调用servlet的destroy()方法,销毁该servlet实例,该方法在以下情况被调用:

      a)tomcat重新启动

      b)reload该webapps

      c)重新启动电脑

    -------------------------------------------------------------------------------------------------------------

    servlet开发流程:

    Step1. 在tomcat的webapps目录下新建myWebSite文件夹,作为网站的根目录;myWebSite文件夹下新建WEB-INF文件夹;WEB-INF文件夹下新建classes和lib文件夹以及web.xml文件;

    Step2. 开发servlet(引入Tomcat文件夹下lib文件夹里的servlet-api.jar),具体过程如下:

    在JCreatorb编辑器中新建文件名为:Hello的java file,然后选择Cogfigure—Options—JDKProfiles—New(在左侧)—选择jdk的路径后确定—编辑—Classes—Add—AddArchive—选择安装后tomcat目录下lib文件夹里面的servlet-api.jar后确定即可。

    Step4. 实现servlet的接口,具体过程如下:

    选择tools—implement interfaces—javax—servlet—Servlet确定即可。

    Step5. 在实现的接口里面填写相应的语句,具体代码如下:

    Step6. 部署你的servlet(servlet开发流程)

      在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 
    19 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
    20    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    21    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    22    version="2.5">
    23 
    24   <display-name>Welcome to Tomcat</display-name>
    25   <description>
    26      Welcome to Tomcat
    27   </description>
    28 
    29 <!—给servlet取名,可以随意取名-->
    30 <servlet-name>hello</servlet-name>
    31 <!—指明servlet的路径,就是servlet的包+类名-->
    32 <servlet-class>com.tsinghua.Hello</servlet-class>
    33 </servlet>
    34 <servlet-mapping>
    35 <!—给servlet取名,可以随意取名-->
    36 <servlet-name>hello</servlet-name>
    37 <!—浏览器中输入的url,可以随意取名-->
    38 <url-pattern>/sp<url-pattern>
    39 </servlet-mapping>
    40 
    41 
    42 </web-app>
    web.xml

    Step4.启动Tomcat,访问你的servlet,在浏览器的地址栏中输入:http://127.0.0.1:8080/myWebSite/hello回车就可以看到servlet的输出

    注意:127.0.0.1是服务器所在的IP,8080是端口号,要根据实际情况定。

    另外,继承GenericServlet和继承HttpServlet在源代码中具体体现。源代码下载点我

  • 相关阅读:
    [洛谷P4513][题解]小白逛公园
    [洛谷P2564][题解][SCOI2009]生日礼物
    [洛谷P3384][题解]轻重链剖分
    [洛谷P2607][题解][ZJOI2008]骑士
    第一次个人编程作业
    第一次博客作业
    第一次个人编程作业
    第一次博客作业
    1.初识数据库系统
    1.计算机发展历程
  • 原文地址:https://www.cnblogs.com/bkygg/p/3358258.html
Copyright © 2011-2022 走看看