0
votes

My Apache tomcat installation directory is C:\apache-tomcat-7.0.42

In webapps folder I have created index.html and WEB-INF folder.

index.html

<html>
<form action="MyServlet" method="post">
<pre>
        Enter A: <input type="text" name="fieldA">
        Enter B: <input type="text" name="fieldA">          
        Add <input type="radio" name="operation" value="add"> 
        Sub <input type="radio" name="operation" value="sub"> 
        Add <input type="radio" name="operation" value="mul"> 
        Div <input type="radio" name="operation" value="div">           
        <input type="submit" value="submit">
        Result: <input type="text" name="result">           
<pre>
</form>
</body>
</html>

In WEB-INF folder I have created web.xml and folder named "classes"

web.xml

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

In webapps/pravesh/WEB-INF/classes folder I have created MyServlet.java

MyServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;

public class MyServlet extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    out.println("<html><body>");
    out.println("doGet");
    out.println("</body></html>");
    out.close();
}

}

I start tomcat server which is running as it shows when I type localhost:8080.

Problem:- 1)When I type localhost:8080/pravesh/index.html it does not show me html page. Instead it automatically submits the form and runs MyServlet and prints on page

doGet

Although If I delete whole WEB-INF folder, the html page shows up.

2)Instead of doGet() if I place doPost() in MyServlet.java and change method of index.html form to post, it says "HTTP method GET is not supported by this URL".

1

1 Answers

1
votes

The <url-pattern>/*</url-pattern> catch the /pravesh/index.html. So, the servlet MyServlet process the request (actually a GET request).

You need a pattern which does not trap the static resources of your application. In your form you have:

<form action="MyServlet" method="post">

The pattern should be:

<url-pattern>/MyServlet</url-pattern>