I’ve just started working on servlets and i’m a newbie.
I have developed a html page with a drop-down menu from which the user can select the color of beer and there is a submit button which invokes the servlet.Here is the html code for it.
<html>
<head>
<title>Beer Selection</title>
</head>
<body>
<h2>Beer Selection Page</h2>
<p>Select beer charecteristics:</p>
<hr>
<form method="POST" action="SelectBeer.do">
<b>Color :</b><!--Keeping it outside the form unnecessarily creates a line break between color and Drop-down menu-->
<select name="color">
<option>Light</option>
<option>Brown</option>
<option>Amber</option>
<option>Dark</option>
</select><br>
<input type="SUBMIT" value="SUBMIT">
</form>
</body>
</html>
I have created the following deployment descriptor
<?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/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>MyBeer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyBeer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>
I have also successfully compiled the BeerSelect.java servlet whose code is given below
package com.example.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);
}
}
I am using tomcat 7.0.21 and i have created the directories as follows:
tomcat/webapps/learning/Beer-v1/form.html and
tomcat/webapps/learning/Beer-v1/WEB-INF/web.xml and
tomcat/webapps/learning/Beer-v1/WEB-INF/classes/com/example/web/BeerSelect.class
(learning is just a folder in which i keep all my small servlet and jsp projects in webapps
Inside learning i’ve my different project such as Beer-v1,DateDisplay,HotelMgmt,etc.)
Now when i run the form.html inside tomcat it is properly displayed but when i click on submit it shows
HTTP Status 404 - /learning/Beer-v1/SelectBeer.do
type Status report
message /learning/Beer-v1/SelectBeer.do
description The requested resource (/learning/Beer-v1/SelectBeer.do) is not available.
Apache Tomcat/7.0.21
what am i doing wrong .Please help.
It should work if you have
Beer-v1as a direct subfolder of the Tomcat’swebappfolder.Then try to access [tomcaturl]/Beer-v1/[yourhtmlform].html.
Because you mapped your servlet to the context root (which is
/), there is no mapping for a/learning/Beer-v1/SelectBeer.do.IMHO it is not a very good idea to have your workspace folder structure inside Tomcat’s webapp folder. This is a special folder for deployed webapps, and Tomcat is expecting a kind of standard structure for this folder (WEB-INF inside root directory, for example).
I think it is better to use Eclipse or something that deploys your application (from your filesystem) to Tomcat automatically. This way you can have your workspace managed however you want.
Eclipse maintains a so called ‘workspace’ where it stores all projects that you create with it. The first time you start Eclipse it will ask you which folder to use as workspace. You could then name your ‘learning’ folder as you use it at the moment.
When you add a server execution environment to Eclipse (e.g. a Tomcat installation), you can configure how Eclipse deploys. The default configuration is to copy the webapp to a Eclipse specific workspace subfolder.