利用Editplus,手动编写第一个Servlet
2014-12-17 20:10
323 查看
开发Servlet三种方法:
1、实现Servlet接口2、继承GenericServlet
3、继承HttpServlet
其中,第一种方法为原始方法,第二种方法已经淘汰,第三种方法是主流。
讨论:第一种方法。初学者刚开始学习Servlet,应该使用第一种方法。虽然过程很繁琐,但需要把握的细节相当多。倘若你借助eclipse开发工具,细节部分必然不会把握。
作为初学者的我,深有体会。
Servlet接口中有五种方法:
<span style="font-size:24px;">1、public void init(ServletConfig config) throws ServletException 2、public ServletConfig getServletConfig() 3、public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException 4、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
相关文章推荐
- 手动编写部署第一个servlet
- cookie小试刀—编写servlet利用Cookie保存上次访问该servlet的时间
- 第一个 手动写Servlet
- 利用urllib+beadutifulsoup编写自己的第一个小爬虫,获取美女图片
- 手动编写Servlet
- Servlet笔记——(1.3.1)第一个手动Servlet程序
- 利用IDE编写第一个J2ME程序
- 【Java框架型项目从入门到装逼】第四节 - 编写第一个Servlet程序
- Java Servlet之编写第一个Servlet
- 第一个 Hibernate应用程序(手动编写)
- 「后台之路第1篇」Ubuntu上利用Eclipse搭建第一个Servlet工程
- 第一个 手动写Servlet
- 利用普通工具编译的第一个Servlet
- ModernUI教程:第一个ModernUI应用(手动编写)
- Servlet第一个Hello Servlet小程序(不用eclipse,手动配置路径)
- 手动编写一个Servlet程序
- 第一个 手动写Servlet
- qtday01 ubuntu 下手动编写第一个qt程序
- Servlet基础-手工编写第一个servlet
- 手动编写一个servlet部署在tomcat上