CSDN·开山之作:利用Editplus,手动编写第一个Servlet

开发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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值