我可以在没有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");
}
}
}
Web.xml与您的基于Java的Web应用程序的'public static void main(String [] args)'类似。绝对必须! – shinjw 2014-09-24 15:12:23
您可以使用web-app版本3.0开始将web.xml替换为注释,但不能完全跳过它。 – ares 2014-09-24 15:19:06
我还有两个问题1 - 我需要重新启动tomcat每次当我编辑servlet或html代码,因为eclipse不更新网址时,我自动更新HTML文件中的操作2-现在包含注释时,当我运行servlet eclipse正在给这样的错误“服务器遇到了一个内部错误,阻止它履行这个请求” – 2014-09-24 15:31:41