0

I have servlet with doPost method :

package web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice<br>");
String c = request.getParameter("color");
out.println("<br>Got beer color " + c);
}
}   

And I use this form.html:

<html><body>
<h1 align="center">Beer Selection Page</h1>
<form action="SelectBeer.do" method="POST">
    <table>
        <tr>
            <td>Select beer characteristics 
                Color:
                <select name="color" size="1">
                <option value="light"> light </option>
                <option value="amber"> amber </option>
                <option value="brown"> brown </option>
                <option value="dark"> dark </option>
                </select>
                <br><br>
                <center>
                <input type="SUBMIT">
                </center>
            </td>
        </tr>
    </table>    
</form>
</body></html>

With my web.xml all things are fine. A have mapped servlet fine. But when I run it, in browser is this error - HTTP method GET is not supported by this URL

I repaces POST method with GET , then i haven't got any errors but the servlet isn't work correct! Do I always need doGet in servlet? And how to use it hear, if I don't need it? I am new at programming, so this is just example what I try to figure out. I will be thanks full if somebody can explain me this.

web.xml:

<web-app 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/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>
Racoon
  • 9
  • 2
  • 5
  • 1
    The problem is with the name of Servlet. It is "BeerSelect" and you are calling like SelectBeer.do Change it to BeerSelect.do – Parkash Kumar Jan 17 '14 at 13:58
  • How could you know it ? He didn't show any xml web config about servlet-mapping. You can have a class named BeerSelect and map it to SelectBeer.do – Justin Iurman Jan 17 '14 at 14:08
  • Yes. I have mapped like Justin said. There isn't problem with that. – Racoon Jan 17 '14 at 14:10

1 Answers1

0

Code seems to be ok unless you are calling the wrong URL in the html form. Post your web.xml that might give us the solution

user2154424
  • 31
  • 1
  • 8