19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

import java.util.List;<br />

import java.util.ArrayList;<br />

import <strong>com</strong>.sun.xml.ws.developer.servlet.HttpSessionScope;<br />

@HttpSessionScope<br />

@WebService(name = "QuizService", serviceName = "QuizWebService")<br />

public class QuizService {<br />

private ArrayList quiz = new ArrayList();<br />

public QuizService() {<br />

// Initialize questions and answers<br />

quiz.add(new Object[]{<br />

"Is Atlanta the capital of Georgia?", true});<br />

quiz.add(new Object[]{<br />

"Is Columbia the capital of South Carolina?", true});<br />

quiz.add(new Object[]{<br />

"Is Fort Wayne the capital of Indiana?", false});<br />

quiz.add(new Object[]{<br />

"Is New Orleans the capital of Louisiana?", false});<br />

quiz.add(new Object[]{<br />

"Is Chicago the capital of Illinois?", false});<br />

}<br />

// Shuffle <strong>to</strong> generate a random quiz for a client<br />

java.util.Collections.shuffle(quiz);<br />

@WebMethod(operationName = "getQuestions")<br />

public java.util.List getQuestions() {<br />

// Extract questions from quiz<br />

List questions = new ArrayList();<br />

for (int i = 0; i < quiz.size(); i++) {<br />

questions.add((String)(quiz.get(i)[0]));<br />

}<br />

}<br />

return questions; // Return questions in the quiz<br />

@WebMethod(operationName = "gradeQuiz")<br />

public List gradeQuiz(List answers) {<br />

List result = new ArrayList();<br />

for (int i = 0; i < quiz.size(); i++)<br />

result.add(quiz.get(i)[1] == answers.get(i));<br />

}<br />

}<br />

return result;<br />

The Web service class named QuizService contains two methods<br />

getQuestions and gradeQuiz. The new Web service is named<br />

QuizWebService (line 10).<br />

The annotation @HttpSessionScope (line 9) is new in JAX-WS 2.2,<br />

which enables the Web service au<strong>to</strong>matically maintains a separate<br />

instance for each client session. To use this annotation, you have<br />

add JAX-WS 2.2 in<strong>to</strong> your project’s library. This can be done by<br />

clicking the Library node in the project and select Add Library.<br />

<br />

20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!