Java - problemas con el request

 
Vista:

problemas con el request

Publicado por Hill (1 intervención) el 10/11/2006 19:18:41
tengo un problema, quiero subir unos datos al request de un servlet, pero desde una clase, y no sé como hacerlo, encontré una clase que se llama ClientHttpRequest que se supone sube los datos al request de un servlet dandole su URL y mandando los parámtros en un Object o un Map, pero no lo hace, si alguien sabe de otra forma de hacerlo, se los agradecería si me lo compartieran, de antemano gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:problemas con el request

Publicado por neossoftware (622 intervenciones) el 10/11/2006 23:37:44
Tu problema es sencillo de resolver:

Lo que te recomiendo que utilices es HttpClient es un proyecto que pertenece a Commons de Jakarta donde mas verdad???

http://jakarta.apache.org/commons/httpclient/

Lo voy a explicar con un servlet este servlet recibe un parametro que se llama nombre y lo imprime en la consola

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Boston, MA 02111.
* */
package org.neos.http.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class for Servlet: HelloWorld
*
*/
public class HelloWorld extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {

public HelloWorld() {
super();
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String nombre= request.getParameter("nombre");
System.out.println(nombre);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

Ahora desde un main mando a ejecutar el servlet pasandole los parametros necesarios en este caso nombre

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Boston, MA 02111.
* */
package org.neos.http.client;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class TestHttp {

static final String LOGON_SITE = "localhost";
static final int LOGON_PORT = 8080;

public static void main(String args[]) throws HttpException, IOException {

HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");

//localizacion del servlet
PostMethod authpost = new PostMethod("/WebProject/HelloWorld");


// Prepare login parameters
NameValuePair action = new NameValuePair("nombre", "Mario");

authpost.setRequestBody(
new NameValuePair[] {action});

client.executeMethod(authpost);

System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();
}

}

checa la salida del tomcat y efectivamente llega la peticion completa....

los jars que necesitas cson:

commons-beanutils-1.7.0.jar
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging-1.0.3.jar

Espero que te sirva el codigo fuente

No olvides visitar nuestro Sitio ;-)
http://www.geocities.com/neos_software/

Mario Hidalgo, Web Master de Neos Software Inc.

Saludos comunidad Open Source
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

thanks

Publicado por Hill (1 intervención) el 11/11/2006 03:11:45
No pues muchas gracias por tu ayuda, si me sirvió mucho lo que me pasaste, y pues no dejaré de visitar tu sitio.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:thanks

Publicado por neossoftware (622 intervenciones) el 12/11/2006 20:19:51
ME da gusto que te sirvio el codigo que desarrollo Neos Software Inc.

Mario.
http://www.geocities.com/neos_software/
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar