aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes HTTP Status 404 The requested resource is not available Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "HTTP Status 404 The requested resource is not available" Watch "HTTP Status 404 The requested resource is not available" New topic
Author

HTTP Status 404 The requested resource is not available

Abhishek Dey
Greenhorn

Joined: Oct 13, 2011
Posts: 9
Hi everyone,
I tried to run this servlet program in Eclipse Helios, and got this error. Now, I have tried all the tips in this site to overcome this problem, but am still stuck. Please help me out. I don't know whether this is because of the program or the environment which I don't understand that well. I am using Tomcat v6.0 server. While creating New Dynamic Web Project in Eclipse, I keep the Target Runtime as Apache Tomcat 6.0 and Dynamic Web Module version as 2.5. I hope they are correct.

The servlet body is as follows:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet
{
StringBuffer message = new StringBuffer();
ArrayList<String> names;
public LoginServlet()
{
super();
names = new ArrayList<String>();
names.add("Admin");
names.add("Staff");
names.add("Customer");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException
{
String username = request.getParameter("username");
if((username == null) || (username.length()==0))
{
message.append("Username required field");
}
else
{
validate(username);
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>Login Page</TITLE></HEAD>"+"<BODY BGCOLOR=\"#99CCFF\">"+"<H1> "+ message+" </H1>" +"</BODY></HTML>");
}
private void validate(String name)
{
if(message.length()!=0)
{
message.delete(0, message.length());
}
if(names.contains(name))
{
String msg = "Hello "+name+" !!!. Login successful<br>";
if(name.equalsIgnoreCase("admin"))
{
message.append(msg + "<br> ");
message.append("<a href=\"addAccount.html\">AddAccount</a><br>");
message.append("<a href=\"displayAccount.html\">Display Account</a><br>");
}
else if (name.equalsIgnoreCase("staff"))
{
message.append("<a href=\"displayAccount.html\">Display Account</a><br>");
}
else if (name.equalsIgnoreCase("customer"))
{
message.append("<a href=\"displayAccount.html\">Display Account</a><br>");
message.append("<a href=\"modifyAccount.html\">Modify Account</a> <br>");
}
}
else {
message.append("Hello guest! Login is invalid...");
}
}
}


The web.xml is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletDemo</display-name>
<welcome-file-list>
<welcome-file>loginForm.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>

And the loginForm.html is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Login Page</title></head>
<body bgcolor="#CCFFCC" >
<h2 align="center">Enter Login Details<br></h2>
<form action="Login" method="post">
<table border="4" align="center" bordercolor="brown"
bgcolor="pink">
<tr><td>UserName</td>
<td><input type="text" name="username"></td></tr>
<tr><td>Password</td>
<td><input type="password" name="pwd"></td></tr>
<tr><td><input type="submit" value="Login"/></td>
<td><input type="reset" value="Reset"/></td>
</tr>
</table>
</form>
</body></html>

Thanks in advance..
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

you action attribute value of form[<form action="Login" method="post"> ] is mismatching with you <url-pattern>/LoginServlet</url-pattern>

by the way: Dont initialize in Servlet Constructor , Use no-argument init() instead!

And Welcome to JavaRanch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Use code tag while posting your code next time. so that, it can be easy to read.
Abhishek Dey
Greenhorn

Joined: Oct 13, 2011
Posts: 9
Hi Seetharaman,

Thanks for welcoming me.
I tried that but still the error is shown as follows:

The requested resource (/ServletDemo/) is not available.

I attached a screenshot of my Eclipse window to give you a view of the file structure of the project. Here, I have seen that people always have the WEB-INF folder just within the project folder. However, in my Eclipse, it always goes three levels down. Also the servlet is not placed in WEB-INF/classes. In fact, no such folder is created by default.


[Download eclipse2.bmp] Download

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Oops! just noticed, Dont put your servlet in default packages instead put in some packages. If you put in default package, container may or may not found it. Dont do that .

and if you get any exception further, please post the stack trace.
Abhishek Dey
Greenhorn

Joined: Oct 13, 2011
Posts: 9
By stack trace, do you mean to say the console contents of Eclipse when I run the program? They don't show any exception anywhere after starting up the server. I tried to run the program after implementing the changes which you said, but the results are still the same as before.


[Download eclipse3.bmp] Download

Abhishek Dey
Greenhorn

Joined: Oct 13, 2011
Posts: 9
Please help me, somebody.. I'm stuck in this bad fix for quite a few days..and don't know the way out.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

Have you followed this suggestion:
Dont put your servlet in default packages instead put in some packages.


JBoss In Action
Abhishek Dey
Greenhorn

Joined: Oct 13, 2011
Posts: 9
Yes, I have indeed, and in the second snapshot of my Eclipse, I have shown the structure of my project.. I have done all that I could, but still, it doesn't work.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

I started with your code, made the two changes that Seetharaman suggested, and it works for me.

The two changes were:
1) Place the servlet in a package. This also required changes to the servlet-class value in web.xml.
2) Changed the post action value in the HTML file to match the servlet's url-pattern in the web.xml.
Abhishek Dey
Greenhorn

Joined: Oct 13, 2011
Posts: 9
Well, finally found the solution myself.. which is why I was always attaching the snapshots of my desktop to my replies. I was not sure whether the hierarchy was correct. And turns out that it wasn't. The loginForm.html should have been kept in the Project folder itself, not within WEB-INF. I did that, and it worked.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

I looked at the bitmaps you posted but the images were practically unreadable (the font was horrible, with letters overlapping each other), so they were really no help at all. How did you take those images? You might want to look into better screen capture tools.

Also, no files within WEB-INF are accessible from the browser.
Abhishek Dey
Greenhorn

Joined: Oct 13, 2011
Posts: 9
Well, Peter, I would have posted much better screenshots, but the site itself prevented me from uploading any picture of higher size than 1 MB. If I have to upload within that size, then this is the best I can do. But the black and white contrast itself was making it quite easy itself, I thought. So I apologize if I couldn't help you guys with clearer pics. Thanks for all the help. Maybe I'll be back soon enough with more queries.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

Never post BMP files. Convert them to JPG or PNG, that is what I do. The resulting files are a fraction of the size and just as readable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTTP Status 404 The requested resource is not available