views:

76

answers:

4

My web.xml is like

    <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"&gt;
    <display-name>sample</display-name>
    <servlet>
        <servlet-name>Sampleclass</servlet-name>
        <servlet-class>sample.SampleClass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Sampleclass</servlet-name>
        <url-pattern>/SampleClass</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/page/form.jsp</welcome-file>
    </welcome-file-list>
</web-app>

and form.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>A simple web application</h1>
        <form method="POST" name="Sampleclass" action="SampleClass">
            <label for="name">Enter your name </label>
            <input type="text" id="name" name="name"/><br><br>
            <input type="submit" value="Submit Form"/>
            <input type="reset" value="Reset Form"/>
        </form>
</body>
</html>

and SampleClass.java is

public class SampleClass extends HttpServlet {
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        PrintWriter out = response.getWriter();
        out.write("<html>Hello Your name is "+name +", and your age is "+age+"</html>");
    }
    public void destroy() {

    }


}

but I am getting error when I entered to submit button of form.jsp
and error is

type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).

I am not understanding that what is the problem exactly ? Please help..

+2  A: 

With the form called at path /page/form.jsp and with form action SampleClass , the browser will submit to /page/SampleClass. Change the form action to /SampleClass, same as the servlet is mapped to in web.xml (note the slash).

If you've deployed the webapp anywhere but the root path (/), the form action should probably even be <%= request.getContextPath() %>/SampleClass.

Edit: Also, web.xml says the class is named sample.SampleClass, but there is no package sample; in the class?

Christoffer Hammarström
I put <url-pattern>/page/SampleClass</url-pattern> but still I am getting same errror.
saloni
Also, web.xml says the class is named sample.SampleClass, but there is no "`package sample;`" in the class?
Christoffer Hammarström
If the package was actually missing, that would have yielded a completely different result (error).
BalusC
There is a sample package inside the src package.
saloni
+1  A: 

I think the problem is that you ar using a relative URL for the action of the form. Since the jsp path is page/form.jsp you are calling /page/SampleClass instead of /SampleClass.

Use:

<form method="POST" name="Sampleclass" action="/SampleClass">
Andrea Polci
this will be the tomcat root, and not the context root
Bozho
+2  A: 

Your web.xml and <form> are all perfectly fine. The servlet must be mapped on /servleturl and the form action must point to servleturl.

The error message also proves that the servlet is perfectly found:

message HTTP method POST is not supported by this URL

You would otherwise have gotten a 404 Page Not Found (servlet not found) or maybe worse a 500 Internal Server Error (servlet failed to execute).

The error you got basically means that there's no doPost() method. However, your code example do contain it. This can have only one cause: you are not running the servlet class version you think you are running. The one currently deployed does not have the doPost() method. Clean up everything, recompile/rebuild everything, redeploy the webapp, restart the server and try again.

BalusC
Actually I am getting an error in Problem window "Target runtime Apache Tomcat v5.0 is not defined." Is this related to my error page.
saloni
I agree with this. It's likely that you're not running the code you think you're running. +1
Christoffer Hammarström
I put my classes folder outside the WebContent and in build folder so now it is working fine . But still error in Problem window is showing same..
saloni
So you should mark this answer accepted then.
Christoffer Hammarström
A: 

Actually I am getting an error in Problem window "Target runtime Apache Tomcat v5.0 is not defined." Is this related to my error page.

saloni
That sounds like an error from your IDE, and therefore unrelated.
Christoffer Hammarström