利用Editplus,手动编写第一个Servlet
来源:互联网 发布:h3cne模拟考试软件 编辑:程序博客网 时间:2024/05/06 08:58
开发Servlet三种方法:
1、实现Servlet接口
2、继承GenericServlet
3、继承HttpServlet
其中,第一种方法为原始方法,第二种方法已经淘汰,第三种方法是主流。
讨论:第一种方法。初学者刚开始学习Servlet,应该使用第一种方法。虽然过程很繁琐,但需要把握的细节相当多。倘若你借助eclipse开发工具,细节部分必然不会把握。
作为初学者的我,深有体会。
Servlet接口中有五种方法:
<span style="font-size:24px;">1、public void init(ServletConfig config) throws ServletException2、public ServletConfig getServletConfig()3、public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException4、public java.lang.String getServletInfo()5、public void destroy()</span>
以下是MyFirstServlet.java,第一种方法的实现!
<span style="font-size:24px;"><span style="font-size:24px;">package com.hnedu;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class MyFirstServlet implements Servlet{public void init(ServletConfig config) throws ServletException{}public ServletConfig getServletConfig(){return null;}public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException{System.out.println("农历十月廿六"+"\tDate:"+new java.util.Date());System.out.println(req.getRemoteHost());res.setCharacterEncoding("utf-8");res.getWriter().println("2014年12月17日 "+"\tDate:"+new java.util.Date());}public java.lang.String getServletInfo(){return null;}public void destroy(){}}</span></span>
具体步骤如下:
1、打开Tomcat文件夹,找到webapps目录,在该目录下建立一个MyFirstWeb文件夹。
2、在MyFirstWeb目录下建立WEB-INF文件夹。从你的...\webapps\examples\WEB-INF下,右键复制web.xml。将web.xml粘贴到刚建立的WEB-INF文件夹下。
3、在WEB-INF文件夹下,创建classes文件夹和lib文件夹。
4、编写并编译MyFirstServlet.java文件。
5、部署web.xml。
6、测试。
</pre><pre name="code" class="java"><span style="font-size:18px;"><span style="font-size:32px;color:#ff0000;"><em>注意</em></span><span style="font-size:24px;">:此</span><span style="font-size:24px;">MyFirstServlet.java,</span><span style="font-size:24px;">在DOS命令行下编译命令:<span style="background-color: rgb(51, 255, 51);">javac -d . </span></span><span style="font-size:24px; background-color: rgb(51, 255, 51);">MyFirstServlet.java</span></span>
<span style="font-size:18px;"><span style="font-size:24px;"></span></span>
<span style="font-size:18px;"><span style="font-size:24px;">请看第五步部署的</span><span style="font-size:32px;"><strong style="background-color: rgb(51, 204, 255);">web.xml</strong></span><span style="font-size:24px;">文件内容:</span></span>
</pre><pre name="code" class="java"><span style="font-size:18px;"><pre name="code" class="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"><span style="color:#ff6666;"> <servlet> <servlet-name>MyFirstServlet</servlet-name> <servlet-class>com.qiuxiangyan.MyFirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyFirstServlet</servlet-name> <url-pattern>/MyFirstServlet</url-pattern> </servlet-mapping></span></web-app></span><span style="font-size: 24px;"></span>
注意:红色标记的区域才是本次部署的内容。
部署完毕之后,启动Tomcat。
在浏览器中输入:http://localhost:8080/MyFirstWeb/MyFirstServlet
0 0
- 利用Editplus,手动编写第一个Servlet
- 手动编写部署第一个servlet
- 编写第一个servlet
- 第一个 手动写Servlet
- 手工编写第一个Servlet
- Java Servlet之编写第一个Servlet
- 手动编写第一个struts2程序
- 第一个 Hibernate应用程序(手动编写)
- EditPlus的安装并且编写第一个网页Hello word
- 手动编写Servlet
- 利用IDE编写第一个J2ME程序
- 搞定JSP第一个Servlet例子并且还是手动编译
- 手工编写的第一个servlet小程序
- Servlet第一个Hello Servlet小程序(不用eclipse,手动配置路径)
- Servlet笔记——(1.3.1)第一个手动Servlet程序
- 手动编写一个Servlet程序
- 手动编写、编译一个servlet
- Servlet(一)第一个Servlet
- HDU2700(规律)
- 安装Ubuntu不能启动
- python自动化运维学习笔记3 - DNS处理模块dnspython
- 2014.12.17数据类型编程的基本概念
- Java—final,finally,finalize的区别
- 利用Editplus,手动编写第一个Servlet
- Java—java中单例模式
- 微信分享开发指南
- TMS320F28335项目开发记录11_28335之存储系统
- Java—java编程集合的比较
- Ubuntu下安装jdk,修改jdk
- SSL,TSL
- localtunnel.me 原理流程浅析
- C语言中,为什么字符串可以赋值给字符指针变量