Remove http header using java servlet api - java

We are using IBM Websphere Application Server 6.1 and browser is Internet Explorer 8.
We have a java servlet which dynamically generates PDF and MS Word documents. On the first attempt some users are saying they are getting
"Internet Explorer was unable to open this site. The requested site is either unavailable or cannot be found. Please try again later."
As per Microsoft Support article id 323308
When you try to open a Microsoft Office document or a PDF document over HTTPS (SSL) IE fails with above error message.
This issue occurs if the server sends a "Cache-control:no-store" header or sends a "Cache-control:no-cache" header.
For IE8 Microsoft suggests to add registry entry on users Windows XP desktop. This is not very practical for us to do as we don't control our users desktops. This does not happen for IE9, Firefox, Chrome, etc.
As per PK20531 WAS 6.1 is adding Cache-Control: no-cache="set-cookie, set-cookie2" and Expires
HTTP headers when there is cookie being set in the response.
Note - We are not setting the cookie in the servlet. The cookie is set by single sign-on software.
On the first attempt when the single sign-on (LTPA) cookie is being set and WAS is adding HTTP headers which IE browser does not like.
Does Java servlet api provide a way to remove http headers? Is there a technique to use Filter api to remove http headers?

If you remove the Cache-Control header from the response, then you're not sending any instructions about caching and therefore the caching behavior would be unpredictable.
It would be better to set the header to something else, rather than remove it. Presumably you want to enable caching on the browser for your pages. So you could add these lines to your servlet to enable caching in the browser:
response.setHeader("Pragma", "cache");
response.setHeader("Cache-Control", "private, must-revalidate");
You could do this in a Filter too, because filters have access to the HTTP response object. But if you've written your own servlet then it's probably more efficient — and clearer — to do it in the servlet.

It's all controllable by you. If you don't put it there, there will be nothing to remove.

Related

JMeter correct location to input a web url? - (Response time required)

JMeter correct location to input a web url?
Im new to Jmeter and need to get the reponse of a given url
I need to use the URL address and not the IP address
Is the following setup correct?
Your setup is fine, if hostname is blank it will be populated from the HTTP Request Defaults (same for the other fields). You can override the "defaults" on HTTP Request level if required.
Few more recommendations:
In the "Advanced" tab of the HTTP Request Defaults tick "Retrieve All Embedded Resources" and "Parallel Downloads" boxes, this way JMeter will request extra resources which are referenced in the HTML like images, scripts and styles (this is what real browsers do when rendering the pages)
Add HTTP Cache Manager to your Test Plan. Real browsers download aforementioned resources, but they do it only once, on subsequent requests images and so on are being returned form browser cache. HTTP Cache manager represents this cache and respects the relevant control headers
Add HTTP Cookie Manager which handles user sessions and deals with cookie-based authentication
Add HTTP Header Manager to send browser-like headers like Content-Type, User-Agent, etc.
See How To Make JMeter Behave More Like A Real Browser for above configurations explained in details.

servlets behind a proxy: getting un-proxied URL

Is there anything in the Servlet spec, Tomcat, or Wicket that will allow a webapp running behind mod_proxy to determine the non-proxied URL of the request?
We need to send out emails with links in them. I had been using the following bit of Wicket to construct URLs to specific pages in the app:
String relURL = RequestCycle.get().getRequest().getRelativePathPrefixToWicketHandler();
RequestUtils.toAbsolutePath(relURL);
Since the emails don't go back out through the proxy, of course the URLs don't get re-written, and end up looking like http://localhost/....
Right now the best I can do is to hard-code the URLs to our production server, but that's setting us up for some debugging headaches when running on dev/test machines.
Using InetAddress.getLocalHost().getHostName() isn't really a solution, since that's likely to return prod1.mydomain.com or somesuch, rather than mydomain.dom, from which the request likely originated.
As answered for the question Retain original request URL on mod_proxy redirect:
If you're running Apache >= 2.0.31 then you might try to set the
ProxyPreserveHost directive as described here .
This should pass the original Host header trough mod_proxy into your
application, and normally the request URL will be rebuild there (in
your Servlet container) using the Host header, so the schema location
should be build using the host and path infos from "before" the proxy.
Is there anything in the Servlet spec, Tomcat, or Wicket that will allow a webapp running behind mod_proxy to determine the non-proxied URL of the request?
No. If the reverse proxy doesn't put the information that you require into the message headers before passing them on, there's no way to recover it.
You need to look at the Apache Httpd documentation to figure out how to get the front-end to put the information that you need into the HTTP request headers on the way through. (It can be done. I just can't recall the details.)

Java/Wicket - How to stop browsers from caching pages?

I have a Java/Wicket page that generates a JNLP file that launches my company's software. This class will optionally take some url parameters and embed them as arguments in the JNLP. When the user launches this JNLP file the client application will perform some function based on those parameters. If the client software is already running on the machine, hitting the JNLP page will instead try to feed these parameters via a remote call to the running client instead of launching a new page.
This part is where I'm having issues. On IE, Firefox and Chrome I could open a new client, but trying to hit the same URL again would instead return a JNLP file. I found that clearing the browser cache fixes this issue on all browsers. Also, I cannot seem to hit breakpoints in the JNLP class, which enforces my hunch that this is more of an issue with the request than something strange with Wicket.
I put the following code in my page class, which extends org.apache.wicket.markup.html.WebPage:
#Override
protected void setHeaders(WebResponse response) {
getPageMap().remove(this);
HttpServletResponse httpServletResponse = response.getHttpServletResponse();
if (httpServletResponse != null) {
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0");
httpServletResponse.addHeader("Keep-Alive", "timeout=3, max=993");
}
}
This doesn't seem to work, as Firefox 3.6 still seems to cache the result. IE 7 will work but only after trying the link I create a few times. I don't know a lot about web development and Wicket and this is new to me so it's possible I'm missing something simple.
TL;DR: How do I get a Wicket page to not cache on the client browser?
A hack used in some of the Wicket internals (see for example the source for org.apache.wicket.markup.html.image.NonCachingImage) is to add random noise to the url.
Basically, if you're generating the urls that the browser calls, you can add a parameter ignored by the web application that varies randomly and fools the browser into ignoring its cache.
Please check the following page:
https://web.archive.org/web/20120104201334/http://palisade.plynt.com:80/issues/2008Jul/cache-control-attributes/
Firefox should honor the "Cache-Control" header.
I don't know Wicket very well but have you tried using WebResponse.setLastModifiedTime(Time time)? I know FF sends an If-Modified-Since header to which your server would reply with 304 Not Modified or the normal response.
It would seem natural to me that your server would check the lastModifiedTime on WebResponse to decide.
If that doesn't help I would suggest you get Firebug for Firefox and take a look at the requests and responses.
response.setHeader( "Expires", "0" );
response.setHeader( "Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, private" );
response.setHeader( "Pragma", "no-cache" );
This works with IE, Firefox and so on, the only browser with which it certainly does not work is konqueror.
Wicket 6.11.0:
Application.get().getResourceSettings().setDefaultCacheDuration(Duration.NONE);
Have you ever tried to load pages using window.location.replace?

Java: Automatic cookie handling in HttpClient?

In Java HttpClient feature list it says:
Automatic Cookie handling for reading
Set-Cookie: headers from the server
and sending them back out in a Cookie:
header when appropriate.
But I can't figure out how to use this feature. I just need to open a web page, let it set all the cookies, then refresh the same page with received cookies.
Thanks.
I kept getting errors back from the server saying my client didn't support cookies and I should turn them on. The following line stopped those errors. Hope this works for you.
httpClient.setCookieStore(new BasicCookieStore());

ajax problem - 200 OK in firebug but red message with no response body

I have small ajax problem related to cross domain as i see it.
On localmachine i created html example with some ajax:
in registration text field user types 'username',
on every keystroke ajax sends it to
local Tomcat, where servlet checks if that username is already used
and sends 'taken' reponse back.
No problem on localhost at all.
As soon as i type used 'username' servlet sends 'taken' response
and browser displays it.
But, when i put test html page with ajax
on remote machine (some free hosting on remote network)
that sends validation request on my localhost Tomcat,
connection is made,
in Tomcat console i see request comming,
and in firebug in Mozzila this is Console ouput:
GET http://89.216.182.25:8080/Dinamicki1/UsernameServlet?username=zik 200 OK
...but in response tab
there is not servlet response 'taken'
and message in firebug is in red color
So servers communicate well, no firewall problems, response is 200 OK
But response body is empty.
Any ideas what this red messages in firebugs are?
Thank you very much in advance.
And if anyone can recommend a some serious ajax tutorial for java
it will be highly appreciated :)
You need to use a domain-relative URL in your Ajax request:
/Dinamicki1/UsernameServlet?username=zik
Or a context-relative URL (assuming that the page is served from /Dinamicki1):
UsernameServlet?username=zik
With regard to "Ajax tutorial for Java", start here: How to use Servlets and Ajax?
You cannot use AJAX to read replies from other domains.
Your HTML must be on the same server (and same domain, port, and protocol) as the AJAX servlet.
The 200 status reported in Firebug does not indicate the validity of the cross-domain ajax call, be it successful or not.
You might want to try using a proxy method to perform the call.
E.g. JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
I figured out how to solve it from this site:
"To allow directory browsing via Apache Tomcat change the parameter "listings" in the file conf/web.xml from false to true."
Call your page not as C:/Documents and Settings/.../page.html but as localhost:8080/your_servlet_name (page is better named index.html).
This way, you will be able to make AJAX requests to localhost:8080/your_servlet_name/something_else.
A solution that worked for me was that I had to add "www" to the url! I was using URL Rewrite, so every URL that I had (image, js, get, load, post), I needed to use full url, but it was missing "www"!
For me, It was web api(c# .NET) request and cors was not enabled.
Added header for cors on controller and it solved the problem.
[EnableCors(origins: "*", headers: "*", methods: "*")]

Categories