0

I'm new to servlet to I'm testing my first servlet program but it is giving HTTP STATUS 404 error. The things which I have done are:

1> Installed java-> C:\Program Files\Java\jdk1.6.0

2> Installed Tomcat-> C:\apache-tomcat-7.0

3> When I type -> localhost:8080 in the browser it works fine.

4> Directory for the folder which I created-> C:\apache-tomcat-7.0.23\webapps\examples.

It contains a web.xml file and WEB-INF folder.

Inside WEB-INF, there is a folder named classes, which contains validate.class

The Souce code for my java file 'validate.java' is given below-

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.lang.*;
public class validate extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
    String name=req.getParameter("tb");
    String pass=req.getParameter("pass");
    String name1=getInitParameter("usrnme1");
    String pass1=getInitParameter("pswrd1");
    Cookie c1=new Cookie(name1,pass1);
    res.addCookie(c1);
    String name2=getInitParameter("usrnme2");
    String pass2=getInitParameter("pswrd2");
    Cookie c2=new Cookie(name2,pass2);
    res.addCookie(c2);
    String name3=getInitParameter("usrnme3");
    String pass3=getInitParameter("pswrd3");
    Cookie c3=new Cookie(name3,pass3);
    res.addCookie(c3);
    String name4=getInitParameter("usrnme4");
    String pass4=getInitParameter("pswrd4");
    Cookie c4=new Cookie(name4,pass4);
    res.addCookie(c4);
    res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    Cookie[] c=req.getCookies();
    if(c!=null)
    {
        for(int i=0;i<c.length;i++)
        {
            if(c[i].getName()==name && c[i].getValue()==pass)
            {
                out.println("<html><body><center>");
                out.println("Hello"+"<b>c[i].getName()</b>");
                out.println("</center></body></html>");
                break;
            }
            else
            {
                out.println("<html><body><center>");
                out.println("Not a Valid USER");
                out.println("</center></body></html>");
                break;
            }
        }
    }
}
}

web.xml file code-

<?xml version="1.0"?>
<web-app>
<servlet>
    <servlet-name>validate</servlet-name>
    <servlet-class>validate</servlet-class>
</servlet>
<init-param>
    <param-name>usrnme1</param-name>
    <param-value>user1</param-value>
</init-param>
<init-param>
    <param-name>pswrd1</param-name>
    <param-value>pwd1</param-value>
</init-param>
<init-param>
    <param-name>usrnme2</param-name>
    <param-value>user2</param-value>
</init-param>
<init-param>
    <param-name>pswrd2</param-name>
    <param-value>pwd2</param-value>
</init-param>
<init-param>
    <param-name>usrnme3</param-name>
    <param-value>user3</param-value>
</init-param>
<init-param>
    <param-name>pswrd3</param-name>
    <param-value>pwd3</param-value>
</init-param>
<init-param>
    <param-name>usrnme4</param-name>
    <param-value>user4</param-value>
</init-param>
<init-param>
    <param-name>pswrd4</param-name>
    <param-value>pwds4</param-value>
</init-param>
<servlet-mapping>
    <servlet-name>validate></servlet-name>
    <url-pattern>/validate</url-pattern>
</servlet-mapping>
</web-app>

After reading few suggestions I tried by changing url to /validate.htm (or .html) but still I get the same error message.


HTTP Status 404 - /examples/validate.html

type Status report

message /examples/validate.html

description The requested resource (/examples/validate.html) is not available.

Apache Tomcat/7.0.27
******************************************************
Log file catalina.2015-04-22 is- 

Apr 22, 2015 9:08:18 PM org.apache.catalina.core.AprLifecycleListener init

INFO: The APR based Apache Tomcat Native library which allows optimal

performance in production environments was not found on the java.library.path: 

C:\Program Files (x86)\Apache Software Foundation\Tomcat 

7.0\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\

Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\

Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files 

(x86)\Java\jdk1.8.0_31\bin;C:\Program Files 

(x86)\Java\jre1.8.0_31\bin;C:\TurboC++\Disk\TurboC3\BIN;C:\Program Files 

(x86)\Skype\Phone\;C:\Program Files (x86)\Java\jdk1.6.0_25\bin;C:\Program Files 

(x86)\Java\jre6\bin;C:\Program Files (x86)\Apache Software Foundation\Tomcat 

7.0\lib;

Apr 22, 2015 9:08:18 PM org.apache.coyote.AbstractProtocol init

INFO: Initializing ProtocolHandler ["http-bio-8080"]

Apr 22, 2015 9:08:18 PM org.apache.coyote.AbstractProtocol init

INFO: Initializing ProtocolHandler ["ajp-bio-8009"]

Apr 22, 2015 9:08:18 PM org.apache.catalina.startup.Catalina load

INFO: Initialization processed in 495 ms

Apr 22, 2015 9:08:19 PM org.apache.catalina.core.StandardService startInternal

INFO: Starting service Catalina

Apr 22, 2015 9:08:19 PM org.apache.catalina.core.StandardEngine startInternal

INFO: Starting Servlet Engine: Apache Tomcat/7.0.27

Apr 22, 2015 9:08:19 PM org.apache.catalina.startup.HostConfig deployDirectory

INFO: Deploying web application directory C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\docs

Apr 22, 2015 9:08:19 PM org.apache.catalina.startup.HostConfig deployDirectory

INFO: Deploying web application directory C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\examples

Apr 22, 2015 9:08:19 PM org.apache.catalina.startup.HostConfig deployDirectory

INFO: Deploying web application directory C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\host-manager

Apr 22, 2015 9:08:19 PM org.apache.catalina.startup.HostConfig deployDirectory

INFO: Deploying web application directory C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\manager

Apr 22, 2015 9:08:19 PM org.apache.catalina.startup.HostConfig deployDirectory

INFO: Deploying web application directory C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\ROOT

Apr 22, 2015 9:08:19 PM org.apache.coyote.AbstractProtocol start

INFO: Starting ProtocolHandler ["http-bio-8080"]

Apr 22, 2015 9:08:19 PM org.apache.coyote.AbstractProtocol start

INFO: Starting ProtocolHandler ["ajp-bio-8009"]

Apr 22, 2015 9:08:19 PM org.apache.catalina.startup.Catalina start

INFO: Server startup in 785 ms

Apr 22, 2015 9:12:20 PM org.apache.catalina.startup.HostConfig checkResources

INFO: Reloading context [/examples]

Apr 22, 2015 9:12:24 PM org.apache.catalina.startup.HostConfig checkResources

WARNING: Error during context [/examples] restart

org.apache.catalina.LifecycleException: Failed to start component 

[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/examples]]

    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)

    at 

org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1366)

    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1454)

    at 

org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:295)

    at 

org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)

    at 

org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)

    at 

org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1379)

    at 

org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild

ren(ContainerBase.java:1537)

    at 

org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild

ren(ContainerBase.java:1547)

    at 

org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Containe

rBase.java:1526)

    at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an 

unknown servlet name hai>

    at 

org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:

3204)

    at 

org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:

3183)

    at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1302)

    at 

org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1294)

    at 

org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:855)

    at 

org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)

    at 

org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja

va:119)

    at 

org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)

    at 

org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161

)

    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

    ... 10 more

Apr 22, 2015 9:37:00 PM org.apache.catalina.startup.HostConfig checkResources

INFO: Reloading context [/examples]
Apr 22, 2015 9:37:04 PM org.apache.catalina.startup.HostConfig checkResources

WARNING: Error during context [/examples] restart
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/examples]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1366)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1454)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:295)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1379)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1537)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1547)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1526)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name validate>
    at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3204)
    at     org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:    3183)
    at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1302)
    at     org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1294)
    at     org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:855)
    at     org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)

    at     org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)

    at     org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)

    at    org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)

    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 10 more

Please help me out as soon as possible. I'll be very much thankful to you.

singhakash
  • 7,891
  • 6
  • 31
  • 65
  • 1
    whoah that is too much to read. 404 means it can't find your page. – jgr208 Apr 22 '15 at 16:52
  • try `localhost:8080/validate` – singhakash Apr 22 '15 at 16:56
  • 5
    In `web.xml` your `` `validate>` contains extra `>` after `validate`. Is it error in your question or in your real code? – Pshemo Apr 22 '15 at 16:56
  • What is the URL you are trying to contact? – devnull69 Apr 22 '15 at 16:56
  • localhost:8080/examples/validate.html – Sindhu Garimella Apr 22 '15 at 17:08
  • 2
    Remove the `>` in your `servlet-mapping`, as suggested by @Pshemo, and as it is noted by the exception, then restart your Tomcat and then try to open `http://localhost:8080/examples/validate`. – Matthias Herlitzius Apr 22 '15 at 17:32
  • Does changing `validate>` into `validate` help? – Pshemo Apr 22 '15 at 18:43
  • Yeah..! But still i'm having a problem.. It has been showing the error type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception java.lang.IllegalArgumentException: Cookie name may not be null or zero length javax.servlet.http.Cookie.(Cookie.java:113) validate.service(validate.java:14) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs. – Sindhu Garimella Apr 24 '15 at 05:02
  • This problem doesn't seem to be actually related with the one described in your question (same code != same problem). Consider creating separate question where you will show properly formatted error message and [minimal code which produces it](http://sscce.org/). Also you should probably delete this question since typographic errors are off-topic on Stack Overflow. – Pshemo Apr 26 '15 at 00:45
  • @MatthiasHerlitzius Can you please take a look https://stackoverflow.com/questions/70117462/http-status-500-internal-server-not-responding – Encipher Nov 26 '21 at 07:56

0 Answers0