Page 1 of 1

Submitting to servlet

Posted: Mon Dec 01, 2003 10:05 pm
by mdmckeehan
I am attempting to submit a post to a servlet that is running within tomcat on my own server. I want to submit a date and time parameter to the servlet post. The servlet is called fine, but I do not know how to access the parameters. They do not appear to be sent as request parameters. The following is a sample of the tag (note that date and time are field names declared in a field tag):

<submit next="http://<my ip address>/InvisentWeb/servlet/BestAvailableTimesServlet" method="post" namelist="date time" />

How can I submit this post request and get access to the date and time parameters in the servlet?

Submitting data to a servlet or JSP document

Posted: Tue Dec 02, 2003 12:28 pm
by support
Here is a simple servlet that handles 2 POST variables named "date" and "time"

Code: Select all

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

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

     String date = request.getParameter("date");
     String time = request.getParameter("time");
     response.setContentType("text/html");
     
     PrintWriter out = response.getWriter();

     //code goes here to return a valid html or vxml document
     //out.println("string containing valid vxml or html doc goes here");     
  }


  //if you wish to handle GET, uncomment this function and fill it in
/*
  public void doGet(HttpServletRequest  request,
                             HttpServletResponse response)
  throws ServletException, IOException{
     //GET variables are read in the same manner as POST variables
    //eg:  
    //String sampleGetParameter = request.getParameter("sampleGetParameter");
  }
*/
}
since this is java code it needs to be compiled. A simpler, yet slower solution is to use a Java Server Page (JSP for short):

Code: Select all

<?xml version="1.0"?> 
<vxml version="2.0"> 

  <%@ page import="java.io.*" %>

  <%  
   //setup the output object
   
   PrintWriter out = null;
   try{ 
     out = response.getWriter();
   }catch(IOException err){
      //trap errors here
   }


   //get the parameters
   String date = request.getParameter("date");
   String time = request.getParameter("time");
  %>  


  <form>
    <block> 
      you entered date
      <value expr="'<% out.println(date); %>'"/>
      and time
      <value expr="'<% out.println(time); %>'"/>
      thank you, goodbye!
    </block>
  </form> 
</vxml> 
JSP works by generating the servlet and compiling it at run time. This has trade offs in that it simplifies the document, but is slightly more inefficient since it requires compiling the bytecode on demand. Regardless, either of these 2 methods should work for handling POST and GET data

hope this helps! :)