Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with posting data from servlet see code  RSS feed

 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
import HTTPClient.*;
/** Called by another servlet or jsp page to pass post parameters - both from the original request,
* and new ones added to it - on to another page, such as a CGI script.
* This is probably not of any use in a pure Servlet/JSP environment, but useful for passing
* requests along to existing CGI scripts, etc.
*
* Useage: First call the following:
* setUrlForward(url) [required]
* setPassExistingParams(boolean) [optional - default is TRUE]
* setParam(paramName, paramValue) [multiple times if desired]
* setHeader(headerName, headerValue) [multiple times if desired]
*
* Then call Go(request, response)
*
*
*
*/
public class PassPostRequestServlet extends HttpServlet {
private String urlForward = "http://rsmilgius:5000/InScope.xml";
private Vector vectParams = new Vector();
private Vector vectHeaders = new Vector();
/* by default, we pass existing params from the originating page,
but this can be suppressed if desired. */
private boolean passExistingParams = true;
/**Initialize global variables*/

private static final String CONTENT_TYPE = "text/html";
private static final int ARRAY_POS_PARAMNAME = 0;
private static final int ARRAY_POS_PARAMVALUE = 1;
private static final int ARRAY_POS_HEADERNAME = 0;
private static final int ARRAY_POS_HEADERVALUE = 1;
private static final boolean debug = true;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void setParam(String paramName, String paramValue) {
String[] arrayParamName_Value = {paramName, URLEncoder.encode(paramValue)};
vectParams.add(arrayParamName_Value);
}
public void setParam(String paramName, int paramValue) {
setParam(paramName, Integer.toString(paramValue));
}
public void setHeader(String headerName, String headerValue) {
String[] arrayHeaderName_Value = {headerName, headerValue};
vectHeaders.add(arrayHeaderName_Value);
}
////////////////////////////added myself////////////////////////////////////
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {

//setheader information

setHeader("Accept" , "*/*");
//setHeader("Referer", "http://rsmilgius:8080/examples/servlets/newpostingservlet.html");
setHeader("Accept-Language", "en-us");
setHeader("Content-Type","application/x-www-form-urlencoded");
setHeader("Accept-Encoding","gzip, deflate");
//setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
setHeader("Connection","Keep-Alive");
setHeader("Cache-Control", "no-cache");


Go(req, res);
}
///////////////////////////////////////////////////////////////////////////
public void Go(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean debug = true;
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try{
/**
* First we SEND the request to the web server
*/

//URL url = new URL("http://ssteward/TechTalk/posttome.asp");
URL url = new URL(urlForward);
//URL url = new URL(urlForward);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
if (debug) System.out.println("Opened connection to " + urlForward);


/* ADD our NEW HEADERS here */

/* We don't pass on any existing headers
It could be we may want to pick and choose a few to send on */
for (int iHeader = 0;iHeader < vectHeaders.size(); iHeader++) {
String[] arrayHeaderName_Value = (String[])vectHeaders.get(iHeader);
connection.setRequestProperty(arrayHeaderName_Value[ARRAY_POS_HEADERNAME], arrayHeaderName_Value[ARRAY_POS_HEADERVALUE]);
if (debug) System.out.println("Added header " + arrayHeaderName_Value[ARRAY_POS_HEADERNAME] +
" with value " + arrayHeaderName_Value[ARRAY_POS_HEADERVALUE]);
} //next header
PrintStream outStream = new PrintStream(connection.getOutputStream());
/* Pass on EXISTING PARAMS here (if desired)
These are parameters set in the original form from the browser.
It seems we should just be able to pass them on, but I haven't found a way,
so we've gotta recreate them here.
*/
String paramString = "";
if (passExistingParams) {
Enumeration enumPNames = request.getParameterNames();
while(enumPNames.hasMoreElements()) {
String paramName = (String)enumPNames.nextElement();
String[] arrayParam = request.getParameterValues(paramName);
if (arrayParam != null) {
for(int iParam=0; iParam < arrayParam.length; iParam++) {
paramString += paramName + "=" + arrayParam[iParam] + "&";
}
}
}
} //if (passExistingParams) {
/* ADD our NEW PARAMS here */
for (int iParam = 0;iParam < vectParams.size(); iParam++) {
String[] arrayParamName_Value = (String[])vectParams.get(iParam);
paramString += arrayParamName_Value[ARRAY_POS_PARAMNAME] + "=" + arrayParamName_Value[ARRAY_POS_PARAMVALUE] + "&";
} //next parameter
/* get rid of last '&' */
if (paramString.endsWith("&")) {
paramString = paramString.substring(0, paramString.length() - 1);
}
if (debug) System.out.println("paramString is " + paramString);
//Sending parameter to URL/CGI
outStream.println(paramString);
outStream.flush();
outStream.close();
/**
* Now we RECEIVE response from web server and pass it back to browser client
*/
String inputLine;
BufferedReader inStream = new BufferedReader(new InputStreamReader(connection.getInputStream()));
/* this was in example, but is deprecated usage */
//inStream = new DataInputStream(connection.getInputStream());
while (null != (inputLine = inStream.readLine())) {
out.println(inputLine);
}
inStream.close();
} catch (MalformedURLException me) {
System.err.println("MalformedURLException: " + me);
} catch (IOException ioe) {
System.err.println("IOException: " + ioe);
}
vectParams.clear();
}
/**Clean up resources*/
public void destroy() {
}
public void setUrlForward(String newUrlForward) {
urlForward = newUrlForward;
}
public void setPassExistingParams(boolean newPassExistingParams) {
passExistingParams = newPassExistingParams;
}
}
Thanks in advance
When I post to the server no data is getting there from the post I even tested it on a simple asp page that returns the results sent..Niether the ASP nor the echo web server is recieving the posted data. Funny enough the echo web server is getting the correct content-length of the data but not the data itself. I have been struggling on this for a week. Any help will be greatly appreciated.Also sometimes some of the data makes it there but not all of it.

I am getting a IOExvception in the servlet unexpected EOF from server
Ray Smilgius

[This message has been edited by Ray Smilgius (edited October 26, 2001).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!