HTML List Box processing using Servlets

For: Intermediate Users (Those who know Java, JDBC)

This example uses two servlets DishServlet and DishSubmitServlet which tells about Dishes with preparation time, method to cook and ingredients needed.

DishServlet just populates the data from the Database table “dishes” and all the dishes in the table will be in a List Box (Select Box).

Once any dish is selected and submitted, another servlet will show the information about dish preparation, time needed and ingredients. Both the Servlets are going to use the Same Table

Here is the Sample Database Table: Name is “dishes

dish_name time_to_prepare method ingredients
Pongal 20 using cooker and prepare the dishes rice, water, salt
Idly 10 using cooker and make 10 pieces in a strech flour,
Poori 15 make dough using wheat, water and salt wheat, salt, water, oil
Pizza 30 Take the pizza base and make the topping using sauze, cheese and spread the onions Oven, Pizza Base, Mozarilla Cheese, Sauze, tomato, capsicum, onion, olive

There are four dishes in the above menu and all of them are populated to a Servlet using the following program

Filename: DishServlet.java

import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class DishServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {
        res.setContentType("text/html");
        ServletOutputStream out = res.getOutputStream();
        out.println("<html><body>");
        out.println("<form method=post action=DishSubmitServlet>");
        out.println("Please select a Dish to be prepared");
        out.println("<select name=dish>");       
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (Exception e)
        {
            out.println("Database Driver Problem" +e.getMessage());
        }
        try
        {
            Connection con=DriverManager.getConnection("jdbc:odbc:pradeep");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("Select * from dishes");
            while(rs.next())
            {
                String s1=rs.getString(2);
                out.println("<option>"+s1+"</option>");
            }
            out.println("</select>");
            out.println("<input type=submit>");
            out.println("</form></body></html>");
        }
        catch (SQLException ee)
        {

           out.println("DSN Issues "+ee.getMessage());
        }
    }
}

FileName: DishSubmitServlet.java

import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class DishSubmitServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {
        res.setContentType("text/html");
        ServletOutputStream out = res.getOutputStream();
        String dish=req.getParameter("dish");
        out.println("<html><body>");
        out.println("<table border=1><tr>");
        out.println("<th>Dish Name</th>");
        out.println("<th>Preparation Time</th>");
        out.println("<th>Method to Prepare </th>");
        out.println("<th>Ingredients Needed </th></tr><tr>");

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (Exception e)
        {
            out.println("Database Driver Problem" +e.getMessage());
        }
        try
        {
            Connection con=DriverManager.getConnection("jdbc:odbc:pradeep");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("Select * from dishes where dish_name='"+dish+"'");
            while(rs.next())
            {
                String s2=rs.getString(2);
                String s3=rs.getString(3);
                String s4=rs.getString(4);
                String s5=rs.getString(5);
                out.println("<td>"+s2+"</td>");
                out.println("<td>"+s3+"</td>");
                out.println("<td>"+s4+"</td>");
                out.println("<td>"+s5+"</td>");
            }
            out.println("</tr></table>");
            out.println("</body></html>");
        }
        catch (SQLException ee)
        {
            out.println("DSN Issues "+ee.getMessage());
        }
    }
}

Compile the file and Make an entry in the web.xml file and run the HTML file using the link http://localhost:8080/examples/DishServlet

Select a Dish from the given Menu and click Submit, you can see a table in the next page that shows the dish and its preparation along with ingredients. See the Images

dish1

DISH2

Comments

Post a Comment

Popular Posts