Acceso a variables post usando Servlets Java
¿Cuál es el equivalente Java de PHP $_POST
? Después de buscar en la web durante una hora, todavía no estoy más cerca.
5 answers
Su objeto HttpServletRequest
tiene un método getParameter(String paramName)
que se puede usar para obtener valores de parámetros. http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-21 17:54:52
Aquí hay un ejemplo simple. No me puse elegante con el html o el servlet, pero usted debe conseguir la idea.
Espero que esto te ayude.
<html>
<body>
<form method="post" action="/myServlet">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" />
</form>
</body>
</html>
Ahora para el Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("username");
String password = request.getParameter("password");
....
....
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-17 13:47:45
Las variables POST deben ser accesibles a través del objeto request: HttpRequest.getParameterMap () . La excepción es si el formulario está enviando datos MIME de varias partes (el FORMULARIO tiene enctype="multipart/form-data"). En ese caso, necesita analizar la secuencia de bytes con un analizador MIME. Puede escribir el suyo propio o usar uno existente como la API Apache Commons File Upload.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-07 19:47:30
Las respuestas anteriores son correctas, pero recuerde usar el atributo name en los campos de entrada (formulario html) o no obtendrá nada. Ejemplo:
<input type="text" id="username" /> <!-- won't work -->
<input type="text" name="username" /> <!-- will work -->
<input type="text" name="username" id="username" /> <!-- will work too -->
Todo este código es HTML válido, pero usando getParameter(java.lang.String) necesitará que el atributo name se haya establecido en todos los parámetros que desee recibir.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-24 11:12:07
Para obtener todos los parámetros post hay Map que contiene el nombre del parámetro de solicitud como clave y el valor del parámetro como clave.
Map params = servReq.getParameterMap();
Y para obtener parámetros con nombre conocido normal
String userId=servReq.getParameter("user_id");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-19 13:12:43