2014-09-24 44 views
0

我可以在没有web.xml但没有@WebServlet(“/ Myservlet”)的eclipse中运行我的动态Web应用程序吗?在eclipse中运行时没有web.xml的Servlet

我在书中给出的例子中有三个html文件和一个servlet,但它没有在eclipse中运行。当我运行eclipse时,它给出了一个错误:404 source not found。我还注意到在TrainingServlets(myprojectname)/部署描述符/ 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"); 
     } 
    } 

} 
+0

Web.xml与您的基于Java的Web应用程序的'public static void main(String [] args)'类似。绝对必须! – shinjw 2014-09-24 15:12:23

+2

您可以使用web-app版本3.0开始将web.xml替换为注释,但不能完全跳过它。 – ares 2014-09-24 15:19:06

+0

我还有两个问题1 - 我需要重新启动tomcat每次当我编辑servlet或html代码,因为eclipse不更新网址时,我自动更新HTML文件中的操作2-现在包含注释时,当我运行servlet eclipse正在给这样的错误“服务器遇到了一个内部错误,阻止它履行这个请求” – 2014-09-24 15:31:41

回答

0

之前是不需要的servlet 3.0.Now web.xml中这是不可能的。

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

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

简而言之,它是你的web应用程序的核心。没有这个你的应用程序无法运行。

0

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

3

如果你的HTML文件住在根文件夹(在WebContent,如果使用的是Eclipse的一个标准的项目),你的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

Following the path outlined by Java EE 1.5, the Servlet specification 3.0 heavily uses annotations to declare Servlet, Filters, Listeners and Security. The configuration file web.xml is now optional.

+0

我还有两个问题1 - 我需要重新启动tomcat每当我编辑servlet或html代码,因为eclipse没有更新网址时自动更新我更新在html文件中的操作2-现在在我运行servlet时包含注释之后eclipse正在给出这样的错误“服务器遇到内部错误,导致它无法完成此请求” – 2014-09-24 15:47:22

+0

①如果更改了HTML,则不需要重新启动否则是JSP,是的。 ②你在URL模式中使用了'/'。在http://www.codejava.net/java-ee/servlet/webservlet-annotation-examples中看到更多的例子 – 2014-09-24 16:06:41

+0

我在这里给出了action =“http://localhost/TrainingServlets/com.myservlet”这里的文件“com”是软件包名称是正确的行为方式吗? – 2014-09-24 17:48:58

相关问题