user2837260
user2837260

Reputation: 181

tomcat resource missing, servlet not running

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

public class MyServlet implements Servlet
{

    public void init(ServletConfig con) {}

    public void service(ServletRequest req, ServletResponse res) throws IOException,ServletException
    {

    res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    String s="blah";
    String s1="blah";
    out.println("<html><body>");

    if((s.equals(req.getParameter("firstname")))&&(s1.equals(req.getParameter("pwd"))))

    out.println("passwords match");

    else
    out.println("password and name combo does not match");

    out.println("</body></html>");
    }

    public void destroy() {}

    public ServletConfig getServletConfig() { return null;}
    public String getServletInfo() { return null;}
}

this is my java file with the servlet class.its saved with the name MyServlet.java and so is the class file.

and here is the xml file:

<web-app>
    <servlet>
        <servlet-name>demoo</servlet-name>
        <servlet-class>MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>demoo</servlet-name>
        <url-pattern>/demo</url-pattern>
    </servlet-mapping>
</web-app>

i have made the folder as WEB-INF and then classes... WEB-INF also contains the .xml file

but when i try to run the servlet , it says resource not found ps- i am already looking for the servlet with the name :demo

localhost:8081/s1/demo* s1 is the war file *

a html file in the war file seems to run fine on the server though.

*

Upvotes: 0

Views: 560

Answers (3)

Timofey
Timofey

Reputation: 2518

If you are using maven for your project then local application start may be done like that:

mvn clean tomcat:run-war

Don't forget to mention in your pom.xml

<packaging>war</packaging>

Upvotes: 0

Shamse Alam
Shamse Alam

Reputation: 1315

If you are using Java 6, just add following annotation in your servlet class and remove the web.xml file from the WEB-INF directory. I hope it will work

@WebServlet(name = "MyServlet", urlPatterns = {"/demo"}) 

Upvotes: 1

Shamse Alam
Shamse Alam

Reputation: 1315

Use following code, delete your web.xml, recompile and deploy your web app

  import javax.servlet.*;
  import java.io.*;
  import javax.servlet.annotation.WebServlet;

   @WebServlet(name = "MyServlet", urlPatterns = {"/demo"})
   public class MyServlet implements Servlet {

   public void init(ServletConfig con) {
   }

    public void service(ServletRequest req, ServletResponse res) throws IOException,    ServletException {

       res.setContentType("text/html");
       PrintWriter out = res.getWriter();
       String s = "blah";
       String s1 = "blah";
       out.println("<html><body>");

       if ((s.equals(req.getParameter("firstname"))) && (s1.equals(req.getParameter("lastname")))) {
        out.println("passwords match");
    } else {
        out.println("password and name combo does not match");
    }

    out.println("</body></html>");
}

public void destroy() {
}

public ServletConfig getServletConfig() {
    return null;
}

public String getServletInfo() {
    return null;
}
}

Upvotes: 1

Related Questions