java 如何使用参数调用 URL 并在 servlet 中返回响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10410704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a URL with params and get back response in servlet?
提问by AabinGunz
I have a situation where a intermediate servlet needs to be introduced which will handle requests from existing project and redirect the manipulated response to either existing project or the new one. This servlet will act as an interface to logininto the new project from some other application.
我有一种情况,需要引入一个中间 servlet,它将处理来自现有项目的请求,并将操纵的响应重定向到现有项目或新项目。该 servlet 将充当从其他应用程序登录到新项目的接口。
So currently I use the following code to get back response in jsp as an xml.
所以目前我使用以下代码以 xml 形式获取 jsp 中的响应。
var jqxhr =$.post("http://abhishek:15070/abc/login.action",
{ emailaddress: "[email protected]",
projectid: "123" },
function(xml)
{
if($(xml).find('isSuccess').text()=="true")
{
sessiontoken=$(xml).find('sessiontoken').text();
setCookie("abcsessionid", sessiontoken , 1);
setCookie("abcusername",e_add,1);
}
}
)
.error(function() {
if(jqxhr.responseText == 'INVALID_SESSION') {
alert("Your Session has been timed out");
window.location.replace("http://abhishek:15070/abc/index.html");
}else {
alert( jqxhr.responseText);
}
});
xml content
xml 内容
<Response>
<sessiontoken>334465683124</sessiontoken>
<isSuccess>true</isSuccess>
</Response>
but now I want the same thing to be done using servlet, is it possible?
但现在我想用 servlet 做同样的事情,这可能吗?
String emailid=(String) request.getParameter("emailaddress");
String projectid=(String) request.getParameter("projectid");
Update
更新
I just came up with something.
我只是想出了点什么。
Is it possible to return back a html page with form (from servlet), whose on body load
it will submit a form and on submission of this form it will receive the response xmlwhich will get processed.
是否有可能返回一个带有表单的 html 页面(来自 servlet),on body load
它会提交一个表单,并且在提交这个表单时它会收到将被处理的响应xml。
回答by BalusC
Use java.net.URLConnection
or Apache HttpComponents Client. Then, parse the returned HTTP response with a XML tool like as JAXBor something.
使用java.net.URLConnection
或Apache HttpComponents 客户端。然后,使用JAXB 之类的 XML 工具解析返回的 HTTP 响应。
Kickoff example:
开场示例:
String emailaddress = request.getParameter("emailaddress");
String projectid = request.getParameter("projectid");
String charset = "UTF-8";
String query = String.format("emailaddress=%s&projectid=%s",
URLEncoder.encode(emailaddress, charset),
URLEncoder.encode(projectid, charset));
URLConnection connection = new URL("http://abhishek:15070/abc/login.action").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
try {
connection.getOutputStream().write(query.getBytes(charset));
}
finally {
connection.getOutputStream().close();
}
InputStream response = connection.getInputStream();
// ...
See also:
也可以看看:
回答by John Munsch
Actually, what you probably want is not an intermediate servlet at all. What you probably want is called a servlet filter and writing one is not particularly hard. I've written one in the past and I just started on a new one yesterday.
实际上,您可能想要的根本不是中间 servlet。您可能想要的是 servlet 过滤器,编写一个并不是特别困难。我过去写过一篇,昨天刚开始写一篇新的。
An article like this oneor this onelays out pretty simply how you can use a servlet filter to intercept calls to specific URLs and then redirect or reject from there. If the incoming URL matches the pattern for the filter, it will get a shot at the request and response and it can then make a choice whether or not to pass it on to the next filter in line.
像这样或这样的一篇文章非常简单地阐述了如何使用 servlet 过滤器拦截对特定 URL 的调用,然后从那里重定向或拒绝。如果传入的 URL 与过滤器的模式匹配,它将获得请求和响应的快照,然后可以选择是否将其传递给下一个过滤器。
I don't know if all third party security solutions do it like this, but at least CAS seemed to be implemented that way.
我不知道是否所有第三方安全解决方案都是这样做的,但至少 CAS 似乎是这样实现的。