配置tomcat接受post请求
如何配置tomcat,以便在发出post请求时将请求参数输出到jsp文件?我是否需要一个转发到 jsp 的 servlet 或者可以在 jsp 文件中处理它?
这是我的方法,它将 post 请求发送到 tomcat 服务器 -
public void sendContentUsingPost() throws IOException {
HttpConnection httpConn = null;
String url = "http://LOCALHOST:8080/services/getdata";
// InputStream is = null;
OutputStream os = null;
try {
// Open an HTTP Connection object
httpConn = (HttpConnection)Connector.open(url);
// Setup HTTP Request to POST
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-1.0");
httpConn.setRequestProperty("Accept_Language","en-US");
//Content-Type is must to pass parameters in POST Request
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// This function retrieves the information of this connection
getConnectionInformation(httpConn);
String params;
params = "?id=test&data=testdata";
System.out.println("Writing "+params);
// httpConn.setRequestProperty( "Content-Length", String.valueOf(params.length()));
os = httpConn.openOutputStream();
os.write(params.getBytes());
} finally {
if(os != null)
os.close();
if(httpConn != null)
httpConn.close();
}
}
谢谢
How can I configure tomcat so when a post request is made the request parameters are outputted to a jsp file? Do I need a servlet which forwards to a jsp or can this be handled within a jsp file ?
Here is my method which sends the post request to the tomcat server -
public void sendContentUsingPost() throws IOException {
HttpConnection httpConn = null;
String url = "http://LOCALHOST:8080/services/getdata";
// InputStream is = null;
OutputStream os = null;
try {
// Open an HTTP Connection object
httpConn = (HttpConnection)Connector.open(url);
// Setup HTTP Request to POST
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-1.0");
httpConn.setRequestProperty("Accept_Language","en-US");
//Content-Type is must to pass parameters in POST Request
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// This function retrieves the information of this connection
getConnectionInformation(httpConn);
String params;
params = "?id=test&data=testdata";
System.out.println("Writing "+params);
// httpConn.setRequestProperty( "Content-Length", String.valueOf(params.length()));
os = httpConn.openOutputStream();
os.write(params.getBytes());
} finally {
if(os != null)
os.close();
if(httpConn != null)
httpConn.close();
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的查询字符串无效。
它应该是
?
仅当您将其作为 GET 查询字符串连接到请求 URL 时才有效。当您想将其编写为 POST 请求正文时,不应使用它。也就是说,如果该服务不应该返回 HTML(例如纯文本、JSON、XML、CSV 等),则使用 servlet。这是一个发出明文的示例。
如果该服务应该返回 HTML,则使用 JSP。更改 URL 以指向 JSP 的 URL。
然后将以下内容添加到JSP模板中以打印请求参数。
无论哪种方式,您都应该能够通过读取
URLConnection#getInputStream()
来获取结果(响应正文)。另请参阅:
URLConnection
来触发和处理 HTTP 请求?与具体问题无关,您没有仔细考虑字符编码。我强烈建议这样做。另请参阅上面的链接以获取详细示例。
First of all, your query string is invalid.
It should have been
The
?
is only valid when you concatenate it to the request URL as a GET query string. You should not use it when you want to write it as POST request body.Said that, if this service is not supposed to return HTML (e.g. plaintext, JSON, XML, CSV, etc), then use a servlet. Here's an example which emits plaintext.
If this service is supposed to return HTML, then use JSP. Change the URL to point to the JSP's one.
And then add the following to the JSP template to print the request parameters.
Either way, you should be able to get the result (the response body) by reading the
URLConnection#getInputStream()
.See also:
URLConnection
to fire and handle HTTP requests?Unrelated to the concrete problem, you are not taking character encoding carefully into account. I strongly recommend to do so. See also the above link for detailed examples.
servlet 可以通过以下方式处理 get 和 post 请求:
如果您从头开始安装 tomcat,请不要忘记将以下几行添加到 web.xml 中,以便让服务器接受 GET、POST 等请求:
A servlet can handle both get and post request in following manner:
If you have a tomcat installation from scratch, don't forget to add the following lines to web.xml in order to let the server accept GET, POST, etc. request: