Hi all,

I have created a simple servlet with one one JSP.

The files are,

index.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Login Page</title>
    <link rel="stylesheet" type="text/css" href="css/style1.css" />
</head>

<body>
<div style="background: LightBlue  ">
    <h1 align="center">Sample Application</h1>
</div>

<FORM action="/myServlet" method="GET">
    What's your name?
<INPUT TYPE=TEXT NAME="name"><P>
<INPUT TYPE=SUBMIT>
</FORM>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>classes.servlet.myServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/myServlet</url-pattern>
    </servlet-mapping>
    
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>
            index.jsp
        </welcome-file>
    </welcome-file-list>
</web-app>

myServlet.java

package classes.servlet;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class myServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        String name = req.getParameter("name");
        out.println(name);
        out.println("<HTML>");
        out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>" );
        out.println("<BODY>");
        out.println("Hello, " + name);
        out.println("</BODY></HTML>");
    }
}

When im trying to give name & press Submit, error is coming as,

HTTP Status 404 - /myServlet
type : Status report
message : /myServlet
description : The requested resource (/myServlet) is not available.
Apache Tomcat/5.5.17

Im using NetBeans, My folder Structure is,

C:\Documents and Settings\mani\test\web\index.jsp
C:\Documents and Settings\mani\test\web\WEB-INF\web.xml
C:\Documents and Settings\mani\test\src\java\classes\servlet\myServlet.java

Please help me in this regard.....

Recommended Answers

All 7 Replies

Give us the complete URL on how you are calling it,

Is it like this : http://localhost:8080/<project-name>/myServlet
Also can you access "index.jsp" ?

Thanks for your reply...
Im accessing the index page as "http://localhost:8084/test/index.jsp"
while redirecting to the servlet, the URL is displaying as "http://localhost:8084/myServlet?name=rani"

It is displaying without the project name, When i manually used to edit the URL, i mean, i have edit the URL by including the project name as "http://localhost:8084/test/myServlet?name=rani",
Then i got the perfect result. So, what may the problem? Please Explain. Where i have to change the code?

And then i wish to store in the database(MySQL).
So where i have to include DB Connection code? whether in java servlet or somewhere else?
Please explain this also...

I have tried myself the DB connection and storage, But i failed, got upset...

Please me and explain....
Awaiting for your reply with DB explaination...
For your note, i have reffered many books but i didnt get the proper knowledge... Help me please.....

> Please Explain. Where i have to change the code?

Try changing your markup; your HTML page doesn't understand your *servlet project* and hence you need to specify the extra "/test" before your servlet name.

> So where i have to include DB Connection code?

In your DAO classes.

> i have reffered many books but i didnt get the proper
> knowledge... Help me please.....

Refer the sticky thread at the top of this forum along with the JEE 5 documentation for some concrete examples.

commented: Ah... So is it finally the return of the king ??? :P +7

Hi SOS,

thank uoi very much for your guidence...
Now its working....
i have tried to change my servlet name as /test/myServlet... Still it was not working... But i tried to give the full url in my form action.... Its now working....

Now i tried to connect DB(Insertion & Selection) by writing the code in servlet itself and DAO class..... Got the result... very happy.....

Now learning beans in progress.....
Thank you for your help....

Had the same problem
The action form acts just like a web browser that runs the page on the server so...
Pasting the full path of the page works fine.
Thanks everyone.

commented: I will select a less offensive username for you -2

please remove classes form your web.xml file then project will work fine......

wonderthegreat: this thread is years old. if they haven't got it to work yet, I doubt they ever will.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.