如何使我的第一个servlet工作? [英] How to make my first servlet work?

查看:117
本文介绍了如何使我的第一个servlet工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的第一个servlet在tomcat 7.0中的eclipse中运行j2ee,但是我无法弄清楚我做错了什么。我运行这样的整个项目:右键单击我的项目 - >运行在服务器上运行,index.html文件在浏览器中显示给我,但是当我点击继续,它显示我的消息:

I'm trying to make my first servlet to run in eclipse for j2ee with tomcat 7.0, but I can't figure it out what i am doing wrong. I run the whole project like this: Right-click on my project->Run As->Run on Server, the index.html file appear to me in the browser, but when i hit "Continue", it's showing me this message:


HTTP状态404 - / PDPJ_L5 / hello -
请求的资源(/ PDPJ_L5 / hello)是

HTTP Status 404 - /PDPJ_L5/hello - The requested resource (/PDPJ_L5/hello) is not available.

我怀疑问题出在表单标签或Web描述符文件的action属性中。请解释一下我做错了什么,如何修复我的项目。谢谢。

I suspect that the problem is at the action attribute from the form tag, or at the web descriptor file. Please explain me what i am doing wrong and how to repair my project. Thanks.

这是我的项目(PDPJ_L5)目录结构:

Here is my project (PDPJ_L5) directory structure:

PDPJ_L5


  • JAX-WS Web服务

  • 部署描述符:PDPJ_L5

  • Java资源:包含我的Hello.java servlet的servlet包的src

  • JavaScript资源

  • build

  • META-INF

  • 具有lib文件夹的WEB-INF,我的index.html和web.xml

  • JAX-WS Web Services
  • Deployment Descriptor: PDPJ_L5
  • Java Resources: src which contains the servlets package with my Hello.java servlet
  • JavaScript Resource
  • build
  • META-INF
  • WEB-INF with a lib folder, my index.html and web.xml

index.html内容:

The index.html contents:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Some title</title>
</head>
<body>
<H1 ALIGN="CENTER">Choose your option:</H1>
<form action="http://localhost:8080/PDPJ_L5/hello" method="GET">
    <center>
        <INPUT TYPE="RADIO" NAME="group" VALUE="one">ONE<BR>
        <INPUT TYPE="RADIO" NAME="group" VALUE="two">TWO<BR><BR>
        <INPUT TYPE="SUBMIT" VALUE="Continue">
    </center>
</form>
</body>
</html>

这是Hello.java servlet:

Here is the Hello.java servlet:

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class Hello
 */
public class Hello extends HttpServlet
{
    private static final long   serialVersionUID    = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Hello()
    {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println("<HEAD><TITLE>Success</HEAD><BODY>");
        out.println("<h1> It works </h1>");
        out.println("</BODY>");
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }

}

而web.xml文件:

And the web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-application_2_3.dtd">

<web-app>
   <servlet>
      <servlet-name>Hello</servlet-name>
      <servlet-class>servlets.Hello</servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>Hello</servlet-name>
      <url-pattern>/hello</url-pattern>
   </servlet-mapping>
</web-app>    


推荐答案

我发现我的问题的解决方案。

I found the solution for my problem.

我使用jar命令构建了一个战争存档,并将其放在tomcat的webapps文件夹中,并以这种方式工作。

I built a war archive with the jar command, and put it in the webapps folder of tomcat, and it's working in this way.

唯一的问题是,它不能在eclipse下运行在服务器上,但这样才能正常工作。

The only problem left is that it's not working under eclipse with run on server, but in this way it's working.

感谢您的帮助。

这篇关于如何使我的第一个servlet工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆