0
votes

Can I run my dynamic web app in eclipse without web.xml and without @WebServlet("/Myservlet")?

I have three html files and one servlet in an example given in my book but it is not running in eclipse. When i run eclipse it gives an error: 404 source not found. I also noticed that no servlet is showing in TrainingServlets(myprojectname)/deployment descriptor/servlets area.

Code for them is below

**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");
        }
    }

}
3
Web.xml is like your public static void main(String [] args) for your java based web application. An absolute must!shinjw
You can replace web.xml with annotations with web-app version 3.0 onwards, but you can't skip over it completely.ares
i have two more questions 1-i need to restart tomcat every time when i edit servlet or html code because eclipse not updating url automatically when i am updating action in html file 2- now after includeing annotation when i am running servlet eclipse is giving errror like this "The server encountered an internal error that prevented it from fulfilling this request"Mohammad Zubair

3 Answers

3
votes

If your html files lives in the root folder (in the WebContent if you are using a standard project of eclipse), change your HTML form to:

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

In another way, if you are using Servlet 3.0 (eg. Tomcat 7), you can avoid the file web.xml if you use the annotation:

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

If the application server supports Servlet 3.0, you can use annotations. See more in 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
votes

Depends on what you mean by "run". You could put a main method in your servlet and run it that way but if you want to do a HTTPRequest on the servlet you have to specify it in your web.xml or use the servlet annotation. There will be no way for the server to map an incoming request to that servlet otherwise.

0
votes

it was not possible before servlet 3.0.Now web.xml is not required.

But if you are using version before servlet 3.0 then its required.

Then Web.xml was the deployment descriptor and it tells to the server about the servlets present.

In short it was the heart of your web application.Without this your application cannot run.