2

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 login into the new project from some other application.

So currently I use the following code to get back response in jsp as an xml.

var jqxhr =$.post("http://abhishek:15070/abc/login.action",
                  { emailaddress:   "ars@gmail.com",
                    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

<Response>
  <sessiontoken>334465683124</sessiontoken>
  <isSuccess>true</isSuccess>
</Response>

but now I want the same thing to be done using servlet, is it possible?

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 xml which will get processed.

AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • If I understood your question right, you want to call another URL through the servlet? You could use [Apache HttpClient](http://hc.apache.org/httpcomponents-client-ga/index.html) or just the [URLConnection](http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html). – Moritz Petersen May 02 '12 at 09:31
  • @Moritz Petersen: Yes something like that, please see my update – AabinGunz May 02 '12 at 09:41
  • Now I'm confused. Is this a Java (Servlet) or jQuery related question? – Moritz Petersen May 02 '12 at 10:52
  • @Moritz: It's a java and servlet question, I used jquery so i included the tag. i'll remove it :) – AabinGunz May 02 '12 at 10:53
  • So you want the servlet to display an html page that contains a form and has a body onload function that will submit the form and then the same servlet will receive the response from the form submission? IS there any point in displaying an html page that will just submit itself when it loads? – ChadNC May 02 '12 at 11:48
  • @ChadNC: I'll make form inputs hidden. But I guess using HttpClient would be suit better here – AabinGunz May 02 '12 at 11:58

2 Answers2

4

Use java.net.URLConnection or Apache HttpComponents Client. Then, parse the returned HTTP response with a XML tool like as JAXB or something.

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, i'll try your code also I am using Groovy, so I can parse the xml using XmlParser – AabinGunz May 03 '12 at 06:25
  • Thanks for your code. It worked. Also if you could please take a look at another [question here](http://stackoverflow.com/questions/10447266/how-to-call-a-url-with-multipart-information-on-server-side) – AabinGunz May 04 '12 at 10:42
0

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.

An article like this one or this one lays 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.

I don't know if all third party security solutions do it like this, but at least CAS seemed to be implemented that way.

John Munsch
  • 19,530
  • 8
  • 42
  • 72