1
votes

I am using IDEA 13 . I am trying to write a simple JAVA application where there is a form with some options and its action is a servlet.Here is my file structure

webapps
 |___ROOT
     |___Beer
           |___form.html
           |___index.jsp
           |___WEB-INF
                 |___web.xml
                 |___classes
                       |____com
                             |___example
                                   |___model
                                   |     |___BeerExpert.class
                                   |
                                   |___web
                                        |___BeerSelect.class



Beer is the name of my app.The form is displayed correctly but when i click the submit button the tomcat is unable to locate the servlet again it shows blank page ( the form is not displayed )"HTTP Status 404 - /Beer/SelectBeer.do"


My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
<servlet>
    <servlet-name>Ch3 Beer</servlet-name>
    <servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Ch3 Beer</servlet-name>
    <url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>


My servlet

package com.example.web;
import com.example.model.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

public class BeerSelect extends HttpServlet {
    public void  doPost(HttpServletRequest request,HttpServletResponse response)
    throws IOException , ServletException {

        String c=request.getParameter("color");
        BeerExpert be=new BeerExpert();
        List result=be.getBrands(c);
        request.setAttribute("styles",result);
        RequestDispatcher view=request.getRequestDispatcher("result.jsp");
        view.forward(request,response);


    }
}


My form

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <h1 align="center">Beer Selection</h1>
    <form method="POST" action="SelectBeer.do">

     Select Beer characteristics<br>
    Color:
    <select name="color" size="1">
    <option value="light">light</option>
    <option value="dark">dark</option>
    <option value="amber">amber</option>
    <option value="brown">brown</option>
    </select>
    <br><br>
    <center>
     <input type="submit">
   </center>
   </form>
   </body>
   </html>


My jsp file(the result is displayed by it)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
    <title></title>
</head>
<body>
<h1 align="center">Beer Recommendation JSP</h1>
<%
    List styles=(List)request.getAttribute("styles");
    Iterator it=styles.iterator();
    while(it.hasNext()){
    out.print("<br>try: " +it.next());
    }
%>
</body>
</html>


My model class

     /**
 * Created by sasha.s on 5/28/2014.
 */
import java.util.*;
public class BeerExpert {
    public List getBrands(String color){
        List brands=new ArrayList();
        if(color.equals("amber")){
            brands.add("Jack Amber");
            brands.add("Red Moose");
        }
        else{
            brands.add("Jail Pale Ale");
            brands.add("Gout Stout");
        }
        return (brands);
    }
}


Why is the tomcat unable to find my servlet ?

2
Where is your result.jsp file?Rohan
why does your servlet and model class looks the same?ravikumar
@R.S My jsp file is the result.jsp i have included it in the postsashas
override doGet method also in your servlet class.ravikumar
@ravikumar sorry it was a mistake corrected it nowsashas

2 Answers

3
votes

Move your application from the webapps/ROOT directory into the webapps directory.

webapps/
   ROOT/
       ...
   Beer/
       index.jsp
       ...
   app2/
       ...
0
votes

Override doGet method also,

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

        doPost(request,response)
  }