Могу ли я запустить динамическое веб-приложение в eclipse без web.xml и без @WebServlet ("/ Myservlet")?Сервлет работает без web.xml в eclipse
У меня есть три html-файла и один сервлет в примере, приведенном в моей книге, но он не работает в eclipse. Когда я запускаю eclipse, он дает ошибку: 404 source not found
. Я также заметил, что сервлет не отображается в области TrainingServlets (myprojectname)/дескриптора дескриптора/сервлетов.
Код для них ниже
**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. Абсолютно необходимо! – shinjw
Вы можете заменить web.xml аннотациями с помощью веб-приложения версии 3.0, но вы не можете полностью пропустить его. – ares
У меня еще два вопроса: 1-мне нужно перезапустить tomcat каждый раз, когда я редактирую сервлет или код html, потому что eclipse не обновляет url автоматически, когда я обновляю действие в файле html 2- теперь после включения аннотации, когда я запускаю сервлет, eclipse дает как это происходит »Сервер обнаружил внутреннюю ошибку, которая помешала ему выполнить этот запрос» –