【问题标题】:Servlet running without web.xml in eclipse在 Eclipse 中运行没有 web.xml 的 Servlet
【发布时间】:2014-11-19 03:36:07
【问题描述】:

我可以在没有 web.xml 和 @WebServlet("/Myservlet") 的情况下在 Eclipse 中运行我的动态 Web 应用程序吗?

在我书中给出的示例中,我有三个 html 文件和一个 servlet,但它没有在 eclipse 中运行。当我运行 Eclipse 时,它​​给出了一个错误:404 source not found。我还注意到 TrainingServlets(myprojectname)/deployment descriptor/servlets 区域中没有显示 servlet。

他们的代码如下

**login.html**
<!DOCTYPE html>
<html>
<meta charset="ISO-8859-1">
<body>
<h2>Please provide login details</h2>
<form method="post" action="http://localhost/TrainingServlets/myservlet" name="myForm">
<br>User Id:
<input type="text" name="userid"/>
<br>Password:
<input type="password" name="pwd">
<br><br>
<input type="submit" value="Submit Form"/>
</form>

</body>
</html>


**welcome.html**
<!DOCTYPE html>
<html>
<meta charset="ISO-8859-1">
<body>
<h2>You have successfully logged in</h2>
</body>
</html>

**register.html**
<!DOCTYPE html>
<html>
<meta charset="ISO-8859-1">
<body>
<h2>Your login is incorrect.Please register yourself</h2>
<form method="post" action="" name="myform">
<br>Name:
<input type="text" name="userid"/>
<br> Address:
<input type="text" name="address"/>
<br> Phone No:
<input type="text" name="phoneno"/>
<br><br>
<input type="submit" value="Register"/>
</form>
</body>
</html>

**MyServlet**
package com;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class Myservlet extends HttpServlet{


    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        processRequest(request,response);
    }
    protected void doPOST(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        processRequest(request,response);
    }

    protected void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String id = request.getParameter("userid");
        String pwd = request.getParameter("pwd");

        if (id.equals("ali")&& pwd.equals("vu")) {
            response.sendRedirect("welcome.html");
        }else{
            response.sendRedirect("register.html");
            response.sendError(response.SC_PROXY_AUTHENTICATION_REQUIRED,"Send Error Demo");
        }
    }

}

【问题讨论】:

  • Web.xml 就像您的 public static void main(String [] args) 用于基于 Java 的 Web 应用程序。绝对必须!
  • 您可以将 web.xml 替换为 web-app 3.0 以上版本的注释,但您不能完全跳过它。
  • 我还有两个问题 1-我每次编辑 servlet 或 html 代码时都需要重新启动 tomcat,因为当我在 html 文件中更新操作时 eclipse 不会自动更新 url 2-现在在包含注释之后我正在运行 servlet eclipse 出现这样的错误“服务器遇到内部错误,导致它无法完成此请求”

标签: java servlets


【解决方案1】:

如果您的 html 文件位于根文件夹中(如果您使用的是 eclipse 的标准项目,则在 WebContent 中),请将您的 HTML 表单更改为:

<form method="post" action="myservlet" name="myForm">

另外,如果你使用的是Servlet 3.0(例如Tomcat 7),如果你使用注解,你可以避免文件web.xml

@WebServlet(value="/myservlet", name="Myservlet")
public class Myservlet extends HttpServlet { ... }

如果应用服务器支持 Servlet 3.0,则可以使用注解。在Servlet 3.0 tutorial 中查看更多信息。

遵循 Java EE 1.5 概述的路径,Servlet 规范 3.0 大量使用注释来声明 Servlet、过滤器、侦听器和安全性。配置文件 web.xml 现在是可选的。

【讨论】:

  • 我还有两个问题 1-我每次编辑 servlet 或 html 代码时都需要重新启动 tomcat,因为当我在 html 文件中更新操作时 eclipse 不会自动更新 url 2-现在在包含注释之后我正在运行 servlet eclipse 出现这样的错误“服务器遇到内部错误,导致它无法完成此请求”
  • ①如果你改变了一个HTML o JSP,你不需要重新启动,否则,是的。 ② 您是否在您的网址格式中使用了“/”。在codejava.net/java-ee/servlet/webservlet-annotation-examples中查看更多示例
  • 我在我的 html 文件中给出了类似 action="localhost/TrainingServlets/com.myservlet" 的操作,这里的 "com" 是包名称是正确操作的正确方法吗?
【解决方案2】:

取决于您所说的“运行”。您可以在您的 servlet 中放置一个 main 方法并以这种方式运行它,但是如果您想在 servlet 上执行 HTTPRequest,您必须在 web.xml 中指定它或使用 servlet 注释。否则服务器将无法将传入请求映射到该 servlet。

【讨论】:

    【解决方案3】:

    在 servlet 3.0 之前是不可能的。现在不需要 web.xml。

    但是如果您使用的是 servlet 3.0 之前的版本,那么它是必需的。

    然后 Web.xml 是部署描述符,它告诉服务器存在的 servlet。

    简而言之,它是您的 Web 应用程序的核心。没有它,您的应用程序将无法运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2011-10-29
      相关资源
      最近更新 更多