1

I have a simple tomcat server running from intelliJ and I'm trying to configure a servlet that will allow the user to select different colours of beer however I always receive a 404 resource not found once the form has been submitted. Is there a probjem with my code or is it a inteliJ config issue ?

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
    <servlet>
        <servlet-name>
            beerSelect
        </servlet-name>
        <servlet-class>
            com.example.web.controller.beerSelect
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>beerSelect</servlet-name>
        <url-pattern>/beerSelect.do(</url-pattern>
    </servlet-mapping>
</web-app>

form.html

<!DOCTYPE html>
<html>
<head>
    <title>Beverages</title>
    <link rel="stylesheet" type="text/css" href="/style/main.css">
</head>
<body>
<h1 class="title">Beer selection form</h1>
<form method="post" action="beerSelect.do">
    Select your beer type:
    <select name="color" size="1">
        <option value="light">Light</option>
        <option value="amber">Amber</option>
        <option value="mild">Mild</option>
        <option value="dark">Dark</option>
    </select>
    <input type="SUBMIT" value="Submit">
</form>
</body>
</html>

beerSelect.java

package com.example.web.controller;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
 * Created with IntelliJ IDEA.
 * User: Chris
 * Date: 18/10/13
 * Time: 10:42
 * To change this template use File | Settings | File Templates.
 */

public class beerSelect extends HttpServlet{
    PrintWriter responseWriter;
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        response.setContentType("text/html");
        responseWriter = response.getWriter();
        responseWriter.print("<p>Beer Selection Advice<p>");
        responseWriter.print("<br>Beer Color:" + request.getParameter("color"));
    }
}
Chris Sewell
  • 1,155
  • 2
  • 13
  • 19

1 Answers1

3

At first look if found one mistake in web.xml

 <url-pattern>/beerSelect.do(</url-pattern>
                            ↑

Remove ( opening round bracket

Edit

You will get future error unhandled ServletException as your doPost method handles only IOException

public void doPost(HttpServletRequest request, HttpServletResponse response) 
                                                 throws IOException{  

Change to

public void doPost(HttpServletRequest request, HttpServletResponse response) 
                                                 throws IOException, ServletException{

Also add @Override annotation to doPost method check this answer

 @Override creates a compile-time check that a method is being overridden. 
 This is very useful to make sure you do not have a silly signature issue 
 when trying to override. 

See below

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) 
                                                 throws IOException, ServletException{
Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90