Question enthuware.ocejws.v6.2.204: wrong answer?

Moderators: Site Manager, fjwalraven

Post Reply
socialguy
Posts: 12
Joined: Thu Nov 28, 2013 5:23 pm
Contact:

Question enthuware.ocejws.v6.2.204: wrong answer?

Post by socialguy »

Given the following JAX-RS client:

Code: Select all

public class URLConClient { 
  public static void main(String[] args) throws IOException {
      URL restURL = new URL("http://localhost:8080/SimpleRS/jax/rs/add/5/6");
      URLConnection connection = (URLConnection) restURL.openConnection();
     // 1
     readFrom(connection);
  } 
}
What are connection setup methods that can be in line //1?

connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setReadTimeout(10000);
connection.setDoOutput(true);
connection.setDoInput(false);

connection.setDoOutput(true) is not required as we're not writing out anything from the client. The explanation says the same thing as well but still it's marked as one of the correct options. Why?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: Question enthuware.ocejws.v6.2.204: wrong answer?

Post by fjwalraven »

connection.setDoOutput(true) is not required as we're not writing out anything from the client. The explanation says the same thing as well but still it's marked as one of the correct options. Why?
You are right, setDoOutput(true) is not required. It is a bit of trick question though.

Basically it doesn't harm calling this method. Note that the question states: "What are connection setup methods that can be in line //1?". It is not stating "should" or "must".

The options connection.setDoInput(false) and the connection.setRequestMethod("GET") do harm.

Regards,
Frits

socialguy
Posts: 12
Joined: Thu Nov 28, 2013 5:23 pm
Contact:

Re: Question enthuware.ocejws.v6.2.204: wrong answer?

Post by socialguy »

Thanks. You may want to update the answer explanation in the simulator to what you said here; it's a little confusing there because it just states that the method call is not needed. Leaves the reader wondering why the answer is correct then.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: Question enthuware.ocejws.v6.2.204: wrong answer?

Post by fjwalraven »

Good point!

I have changed the explanation.

Thanks,
Frits

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: Question enthuware.ocejws.v6.2.204: wrong answer?

Post by himaiMinh »

One more point, it is optional to have this

Code: Select all

connection.setDoInput(true)
According to MZ's notes, the flag for setDoInput is true by default. If the client wants to get input, this line of code can be omitted. According to MZ, "The client code will be able to read HTTP response by default."

One more interesting finding,usually most examples from books do this:

Code: Select all

//The fully qualified name is java.net.HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) restURL.openConnection();
But in this question, the connection is URLConnection:

Code: Select all

//URLConnection is an abstract class and I wonder what connection's concrete type is....
URLConnection connection = (URLConnection)restURL.openConnection();
Then, I do this

Code: Select all

 System.out.println(connection.getClass().getName());
The output is : sun.net.www.protocol.http.HttpURLConnection

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: Question enthuware.ocejws.v6.2.204: wrong answer?

Post by himaiMinh »

Another point is, I think it does not harm to put

Code: Select all

 connection.setAllowUserInteraction (true);
It compiles, but it is just not necessary.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: Question enthuware.ocejws.v6.2.204: wrong answer?

Post by fjwalraven »

himaiMinh wrote:One more interesting finding,usually most examples from books do this:

Code: Select all

//The fully qualified name is java.net.HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) restURL.openConnection();
But in this question, the connection is URLConnection:

Code: Select all

//URLConnection is an abstract class and I wonder what connection's concrete type is....
URLConnection connection = (URLConnection)restURL.openConnection();
Then, I do this

Code: Select all

 System.out.println(connection.getClass().getName());
The output is : sun.net.www.protocol.http.HttpURLConnection
The HttpURLConnection extends URLConnection and both (!) classes are abstract.

Regards,
Frits

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests