0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

②フロントとサーバの通信方法(RPC) テキスト 通信

Last updated at Posted at 2023-02-11

今回は、前回「①フロントとサーバの通信方法(RPC) テキスト 定義」の続きです。
実際に、サーバとクライアントで通信をしてみましょう。

クライアント側で、ボタンを用意して、ボタンを押す、「greetingService.greetServer」が実行されて
その結果、成功すると「onSuccess()」が呼び出されます。
失敗すると、「onFailure()」です。
今回は、前回「①フロントとサーバの通信方法(RPC) テキスト 定義」の続きです。
実際に、サーバとクライアントで通信をしてみましょう。

■クライアントから通信


    String p_send_txt=nameField.getText();
    //文字列「p_send_txt」をサーバに送ります。
    greetingService.greetServer(p_send_txt, new AsyncCallback<String>() {

        //失敗したら、これが呼び出される。
        public void onFailure(Throwable caught) {
            Window.alert("失敗")
        }

        //成功したら、これがが呼び出される
        //resultにサーバからも文字列が帰ってきます。
        public void onSuccess(String result) {
            dialogBox.setText("Remote Procedure Call");
            serverResponseLabel.setHTML(result);
            dialogBox.center();
            closeButton.setFocus(true);
        }
    });

サーバで受信+返信


	public String greetServer(String input) throws IllegalArgumentException {

	    	//サーバで文字を加工
	        String  p_convert_sv= "Hello, " + input + "!<br><br>I am running ";
			return  p_convert_sv;
	}

■実行画面
image.png

■結果
image.png

クライアントプログラム全部

package kut.hp_rc.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Gwt_test implements EntryPoint {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
	DialogBox dialogBox = new DialogBox();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final TextBox nameField = new TextBox();
		nameField.setText("GWT User");
		final Label errorLabel = new Label();
		final Button closeButton = new Button("Close");
		final HTML serverResponseLabel = new HTML();
		final Label textToServerLabel = new Label();


		//ダイアログ作成の準備
		createDialog(closeButton, serverResponseLabel, textToServerLabel);



		final Button sendButton = new Button("Send");
		//ボタンを押したら、通信を行う。
		sendButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {

				String p_send_txt=nameField.getText();
				//文字列「p_send_txt」をサーバに送ります。
				greetingService.greetServer(p_send_txt, new AsyncCallback<String>() {

					//失敗したら、これが呼び出される。
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setText("Remote Procedure Call - Failure");
						dialogBox.center();
						closeButton.setFocus(true);
					}

					//成功したら、これがが呼び出される
					//resultにサーバからも文字列が帰ってきます。
					public void onSuccess(String result) {
						dialogBox.setText("Remote Procedure Call");
						serverResponseLabel.setHTML(result);
						dialogBox.center();
						closeButton.setFocus(true);
					}
				});
			}});

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);


		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});
		sendButton.addStyleName("sendButton");
	}

	/**
	 * @param closeButton
	 * @param serverResponseLabel
	 * @param textToServerLabel
	 */
	public void createDialog(final Button closeButton, final HTML serverResponseLabel, final Label textToServerLabel) {
		// Create the popup dialog box
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		// We can set the id of a widget by accessing its Element
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		closeButton.getElement().setId("closeButton");
		dialogBox.setWidget(dialogVPanel);
	}
}

サーバ側プログラム全部

package kut.hp_rc.server;

import kut.hp_rc.client.GreetingService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server-side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) throws IllegalArgumentException {

	    	//サーバで文字を加工
	        String  p_convert_sv= "Hello, " + input + "!<br><br>I am running ";
			return  p_convert_sv;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 *
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(
				">", "&gt;");
	}
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?