eclipse + tomcat 编写简单的Servlet程序

本文详细介绍了如何在Eclipse环境下,配合MyEclipse插件,配置Tomcat服务器,并编写及运行一个简单的显示当前时间的Servlet程序。包括Tomcat端口设置、Eclipse的项目配置、Servlet代码编写、web.xml配置以及最终通过浏览器访问验证的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个程序是在页面上显示当前时间

1。首先确认tomcat安装完毕,我用的是tomcat5.0.28,刚开始用6.0感觉很不好用。安装的时候注意tomcat默认的端口是8080,如果安装了oracle,他有一个服务占用了8080端口,把他改了就可以了方法如下:登录到oracle,点开XML数据库——配置——Http-port,值我把他改成了8090,如图

2。右击我的电脑——管理——服务和应用程序——服务,找到tomcat那一项,将它改为手动,如图

3。安装Myeclipse,这是eclipse的一个插件,功能非常强大,可以自己从网上下载,安装好。因为太大了200多m没法传上去。

4。打开eclipse,窗口——首选项——MyEclipse——Application Servers——Tomcat5,把Tomcat Server点成Enable,Tomcat Home Directory 添上安装Tomcat的路径,我的是装在D:/Program Files/Tomcat 5.0下,如图所示:

5。在eclipse中新建一个Web Project 项目项目名称设为test,servlet类名为T整个项目结构如下图:

6。在中T.java中,输入如下代码:

package t1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class T extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public T() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.print(new Date());
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doGet(request, response);

 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

其实这里面很多代码是新建一个Servlet类自动生成的,自己要改动的只是doGet和doPost方法。

7。打开web.xml,输入如下代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>T</servlet-name>
    <servlet-class>t1.T</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>T</servlet-name>
    <url-pattern>/servlet/T</url-pattern>
  </servlet-mapping>
</web-app>

8.保存。在myeclipse中将此项目和tomcat关联,方法如下:点击eclipse中按钮,出现如下图所示

点击add,出现如下图所示:

点击完成。ok,关联完成。

9。保存项目。在myeclipse中将tomcat启动,打开ie,在地址栏中输入:http://localhost:8080/test/servlet/T,就可以看到当前时间了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值