Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

WAP builds Java applications

Take advantage of your existing EJBs to power a mobile workforce


Return to article



<%@ page contentType="text/vnd.wap.wml"  session="true" import="java.util.Vector"%>

<?xml version="1.0"?> 
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> 
 

<%
    /*
      The place order obtains the Item code from the index passed to it. It then places this in 
      the session.
    */

    int index = Integer.parseInt(request.getParameter("ind"));
    Vector itemInfo[] = (Vector[])session.getAttribute("ItemList");
    session.setAttribute("ItemId",itemInfo[index].get(1));
%>
<wml> 
  <card id="quantity" title="Quantity">
     <p align="center">
           
             <br/>
             Quantity: <input name="Quantity" type="text" emptyok="false"  value=""/>   
             
   </p>
<do type="prev" label="Next">
    <go href="/PlaceOrderServlet" method="get">
            <postfield name="quant" value="Quantity"/>
    </go>        
</do>
</card>
</wml>



PlaceOrder.jsp accepts from the user via an input field the number of items that the salesperson wants to order. It then places an order by calling PlaceOrderServlet.


PlaceOrderServlet.java
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.rmi.*;
import javax.naming.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;


public class PlaceOrderServlet extends HttpServlet
{
    Context ic;
    OrderHome home;
    javax.sql.DataSource ds;
    /*
      The PlaceOrder servlet first contacts the data source directly and obtains the highest  
      primary key value(i.e. the order id). It then increments this value to obtain the next primary key value.
      It then creates a new Order Entity EJB, which corresponds to the new order placed. The order 
      consists of the order id, salesman id, client id, item id and the quantity of items ordered.
    */
      

    
    public void init() throws ServletException
    {
        try
        {
            System.out.println("trying to get initial context");
            ic = (InitialContext) getInitialContext();
            System.out.println("Got InitContext");
            /*
              Lookup the datasource of this application. The name 'wapDB' is recognized by the 
              application server from the configuration files.
            */
                   
		ds = (javax.sql.DataSource)ic.lookup("wapDB");
            System.out.println("lookup for ds succeeded");
            
            Object objRef = ic.lookup("Order");
            home = (OrderHome)PortableRemoteObject.narrow(objRef,OrderHome.class);
            System.out.println("Got Home");
            
        }catch(Exception e){
            System.out.println("Error in init of PlaceOrderServlet: " + e.getMessage());
            e.printStackTrace();
        }
    
    
    }

    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
    {
    
        try
        {
            HttpSession session = req.getSession(false);
            String salesman_id = (String)session.getAttribute("SalesmanId");
                       
            String client_id = (String)session.getAttribute("ClientId");
            
		String quantity = (String)req.getParameter("quant");
            
            String item_id = (String)session.getAttribute("ItemId");
            
            String maxOrderId = null;
            Connection conn = ds.getConnection();
            //Get the max order id 
            PreparedStatement s = conn.prepareStatement("SELECT MAX(ID) FROM ORDER_INFO");
            s.executeQuery();
            ResultSet rs = s.getResultSet();
            while(rs.next())
            {
                maxOrderId = rs.getString(1);
            }
            maxOrderId = maxOrderId.trim();
            long orderId = Long.parseLong(maxOrderId);
		//increment the old max id to obtain the new one.
            orderId++;
            String newOrderId = "" + orderId;
            //create a new Order EJB corresponding to the order just placed.
            Order order = home.create(newOrderId,quantity,item_id,salesman_id,client_id);
            
            String time = order.getTime();
            session.setAttribute("OrderId",order.getPrimaryKey());
            session.setAttribute("OrderTime",time);
            res.sendRedirect(res.encodeURL("/Confirm.jsp"));
            
            
            
        }catch(Exception e)
        {
            
            System.out.println("Error in doGet of PlaceOrderServlet: " + e.getMessage());
            e.printStackTrace();
        }
                
    }
    
    private Context getInitialContext() throws NamingException
   {
        Context initial = null;
        try {
             /*
               There are other methods of obtaining the initial context too. Check the J2EE docs and
               your application server docs for these.
             */		
             initial = new InitialContext();
             
         
        }catch(Exception ne) {
          System.out.println("Unable to get an initial context");
        }
        return initial;

   }
}

Return to article