C # Development Client, Java, and Tomcat development server

Source: Internet
Author: User

Hessian Introduction, hello and file upload example, C # client +java tomcat background

2.Hello Example
1) Backstage-Define Java interface:
Package Org.migle.hessian;
Public interface Hello {
public string SayHello (String smt);
public void Printhello (String SMT);
}
2) Backstage--Implement Java interface:
Package Org.migle.hessian.impl;
Import Org.migle.hessian.Hello;
public class Helloimpl implements Hello {
public string SayHello (String smt) {
return SMT! = null? "Hello" + SMT: "Hello Hessian";
}
public void Printhello (String SMT) {
System.out.println ("Hello" + SMT);
}
}
3) Backstage-configuration Tomcat/hessianserver/web-inf/web.xml, if Lib contains Hessian-4.0.7.jar:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns : web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation= "http://java.sun.com/xml/ns/ Javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 ">
<display-name>hessian</display-name>
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>org.migle.hessian.impl.HelloImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
</web-app>
4) Front--c# code, define interface:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;

Namespace WindowsFormsApplication3
{
public interface Hello
{

String SayHello (String smt);
void Printhello (String smt);
}
}
5) foreground--c# code, implement remote Call Java class, precondition is reference hessianCsharp.dll:
......
Using Hessiancsharp.client;
Using Hessiancsharp.io;

Namespace WindowsFormsApplication3
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{
String url = "Http://localhost/HessianServer/hessian";
Chessianproxyfactory factory = new Chessianproxyfactory ();

Hello test = (hello) factory. Create (typeof (Hello), URL);
MessageBox.Show (Test.sayhello ("Migle"));//print a string obtained from the server side
Test.printhello ("Hessian"); Print "Hello Hessian" on the server-side console
}
......
6) Run the C # program.

3. File Upload example, implemented on 2 basis
1) Backstage-Define Java interface:
Package Org.migle.hessian;
Import Java.io.InputStream;
Public interface UploadFile {
public boolean uploadfile (String fileName, inputstream data);
}
2) Backstage--Implement Java interface:
Package Org.migle.hessian.impl;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Org.migle.hessian.UploadFile;
public class Uploadfileimpl implements UploadFile {

@Override
public boolean uploadfile (String fileName, InputStream in) {
Try
{
OutputStream out = new FileOutputStream ("d:/temp/" +filename);
int nlength = 0;
byte[] bdata = new byte[1024];
while ( -1!= (Nlength=in.read (bdata)))
{
Out.write (bdata, 0, nlength);
}

Out.close ();
return true;
} catch (FileNotFoundException e) {
E.printstacktrace ();
return false;
} catch (IOException e) {
E.printstacktrace ();
return false;
}
Finally
{
try {
In.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
3) Backstage--Configure Tomcat/hessianserver/web-inf/web.xml, add a servlet:
......
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>org.migle.hessian.impl.UploadFileImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
......
4) Front--c# code, define interface:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;

Namespace WindowsFormsApplication3
{
public interface Hello
{

String SayHello (String smt);
void Printhello (String smt);
}

public interface UploadFile//This is a new part of the interface on the Hello basis
{
BOOL UploadFile (String fileName, Stream sroutput);
}
}
5) foreground--c# code, realize the remote Call Java class, on the basis of Hello example, add an upload file Event response processing:
......
private void Buttonupload_click (object sender, EventArgs e)
{
Stream OS = new FileStream (Textboxupload.text, FileMode.Open, FileAccess.Read);

String url = "Http://localhost/HessianServer/upload";
Chessianproxyfactory factory = new Chessianproxyfactory ();
UploadFile test = (UploadFile) factory. Create (typeof (UploadFile), URL);
Test.uploadfile ("Test.xml", OS);
MessageBox.Show ("222");
Os. Close ();
}
......

C # Development Client, Java, and Tomcat development server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.