Categories
discuss

Why can’t HttpServletResponse Headers be updated AFTER getWriter() is called?

I’ve been digging around the web after fixing an issue this afternoon @ work where Cookies added to the HttpServletResponse weren’t being properly reflected in the response headers because our Servlet had already retrieved the response’s PrintWriter (i.e. response.getWriter()) before we added the cookies. I’m now aware that best practices dictate that response header modifications (i.e. setting content type, adding/editing Cookies, etc.) must be done prior to a call to getWriter() but, what I’m looking for is: Why?

We’ve been speculating about why retrieving the PrintWriter in effect freezes the response headers but why definitively does the Servlet specification enforce that?

Answer

Section SRV.5.2 Headers of the Java™ Servlet Specification Version 2.4

To be successfully transmitted back to the client, headers must be set before
the response is committed. Headers set after the response is committed will be
ignored by the servlet container.

So the spec doesn’t explicitly mention getWriter() having an effect on setting headers.

However, your servlet container implementation may have chosen to treat the response as having been comitted once getWriter() is called. That is subtly different.

In some of the containers I’ve worked with you get a warning logged when you attempt to set a header after the response has been comitted.

It is always worth calling getWriter() as late as possible, as you may want the opportunity to set the character encoding, etc, which must be set before getWriter() is called.

Categories
discuss

How to make background image shrink proportionally to fit button size in javascript?

I am making a chessboard in javascript. The chessboard’s squares (buttons) were originally designed to be 60px by 60px, but now they are 40px by 40px.

    button 
    {
      width:40px; 
      height:40px; 
      border: 0
    }

However, some of the pieces that were designed for this earlier chessboard are still 60px by 60px. Is there any way in Javascript to make the images shrink proportionally to fit the square size? Currently, the images fit the square size, but do not shrink, so when I say,

    square.style.backgroundImage = imgLink; // square is the button, imgLink is "WhiteKing.jpg" for example.

I get pieces like this –

enter image description here

If the WhiteKing.jpg had shrunk proportionally, it would have fit nicely. Is there a way to do this in Javascript? Any help would be really appreciated.

Answer

Most modern browsers have support for CSS background-image options like background-size, so you may really use something like this:

button{
  width:40px; 
  height:40px; 
  border: 0;
  background-size: 100%; /* To fill the dimensions of container (button), or */
  background-size: 40px auto; /* to specify dimensions explicitly */
}

Of course you can use that in JavaScript too (but it’s better to add it to already existing CSS for button):

square.style.backgroundSize = '100%';
Categories
discuss

Why keydown listener doesn’t work in IE

Im trying to run this in IE 8 but it doesn’t work, any idea? It works in Firefox, Chrome, Opera…

preventBackspace();

function preventBackspace() {
    try {
        if (window.addEventListener) {
            window.addEventListener("keydown", onKeyDown, true);
        } else if (window.attachEvent) { // IE 
            alert(window);
            window.attachEvent("onkeydown", onKeyDown);
        } else {
            document.addEventListener("keydown", onKeyDown, true);
        }
        } catch (e) {
            alert(e);
    }
}

function onKeyDown(e) {
    alert("test!");
}

jsfiddle:

http://jsfiddle.net/ubfBq/

window.attachEvent is defined and the event listener added. But it never shows “test!” alert.

I read something about useCapture flag, which is possible to use in the other methods. It captures the key press on the window before the event “goes down”. Internet Explorer doesn’t seem to allow/use this. Is that the problem? If yes, how can I solve it?

Answer

Use document.attachEvent instead. :]

Categories
discuss

JAXB Bindings to schemas in a JAR

I’m using the maven jaxb2 plugin to generate Java classes, built from schemas in a jar. However, I’m not sure how to correctly locate to these schemas from a bindings file. If Iextract the schemas from the jar and drop them in the same directory as the bindings, all is well. However, this isn’t a practical long term solution.

pom.xml:

<plugin>
 <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <version>0.8.1</version>
  <executions>
    <execution>
     <goals>
      <goal>generate</goal>
     </goals>
    </execution>
   </executions>
   <configuration>
    <schemas>
     <schema>
      <dependencyResource>
       <groupId>com.test</groupId>
       <artifactId>schemas</artifactId>
       <version>1.10-SNAPSHOT</version>
       <resource>schemas/schema.xsd</resource>
      </dependencyResource>
     </schema>
    </schemas>              
    <bindingDirectory>bindings</bindingDirectory>
    <generatePackage>test.package</generatePackage>
    <bindingIncludes>
     <include>*.xml</include>
    </bindingIncludes>
    <extension>true</extension>
   </configuration>
  </plugin>

bindings.xml:

<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
 xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb ./bindingschema_2_1.xsd"
 version="2.1">

<jxb:bindings schemaLocation="classpath:/schemas/schema.xsd" node="/xs:schema">
  <jxb:bindings node="//xs:complexType[@name='AbstractChangeable']">
   <jxb:class implClass="com.test.AbstractEntity" />
  </jxb:bindings>
</jxb:bindings>

Answer

What I’d like to have working here is something like:

<jaxb:bindings schemaLocation="maven:org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-tests-po!/purchaseorder.xsd" node="/xs:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="org.jvnet.jaxb2.maven2.tests.po"/>
    </jaxb:schemaBindings>      
</jaxb:bindings>

But it does not at the moment. Please file an issue, I’ll try to fix it.

What does work now is SCD-based binding:

<jaxb:bindings scd="x-schema::po" xmlns:po="urn:po">
    <jaxb:schemaBindings>
        <jaxb:package name="org.jvnet.jaxb2.maven2.tests.po"/>
    </jaxb:schemaBindings>      
</jaxb:bindings>

So you don’t actually need to bind based on a specific schema location, you can bind based on the namespace URI, which is theoretically better.

Practically I have an experience that SCD-bindings don’t always work reliably.

UPDATE

See this link for more information SCD usage in JAXB.

Categories
discuss

Manual input not saved in Android’s DatePicker(Dialog)

Implementing the DatePicker or DatePickerDialog in Android is easy. But when it comes to data storage, I have a problem with those classes:

If you use the spinners (+ or – button) to change the date, everything works fine. The event “Date changed” or “Date set” is called and you can get the values that the user entered.

But when the year is manually entered into the input field (via keyboard) and the user then clicks “Save” in the dialog, there won’t be any event called and you won’t get that manually entered value.

It only works when the user changes something with the sliders again after manually entering the year. Because when you use the sliders, the events are fired.

Is this normal behaviour? How can I achieve the desired behaviour, namely that an event is fired when the user enteres something manually and then clicks “Save”?

Thanks in advance!

Answer

Just clear focus, and android will set the number from manual input.

eg:

DatePicker datePicker = findViewById(R.id.dp);

When saving just like onClick(), add datePicker.clearFocus();

This must be working.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..