服务器怎么写java代码,java服务器编程

如何用自己的电脑做服务器,用java语言写服务器程序

你指的服务器程序是指B/S(浏览器/服务器)

成都创新互联公司成都企业网站建设服务,提供成都做网站、网站建设、外贸营销网站建设网站开发,网站定制,建网站,网站搭建,网站设计,响应式网站,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:18982081108

还是C/S(客户端/服务器)模式啊,不过不管你想写那种服务端程序首先都是要先安装JDK的,然后才开始开发,如果是B/S可以使用tomcat作为服务器,C/S的话就要自己写了

怎样让服务器执行已经在别的电脑上写好的代码(Java写的)?具体步骤是怎样的?

如果是源码.java文件,需要先编译成.class文件,编译用服务器上面的jdk版本编译

简单的java文件,javac就可以了,如果是一个项目就比较麻烦了

一般还是用eclipse自动编译最简单了,然后上传到服务器

如果是命令行运行的代码,找到入口类的main方法,然后执行 java xxx就行了 (xxx是入口类名)

HttpPost发送字符串到服务器,服务器接收代码并显示怎么写Java代码?

服务器端接收客户端的请求的话,需要在服务器端的java文件实现HttpServlet这个接口,并且在web.xml里配置一个客户端的请求拦截。

web.xml里的代码里添加

servlet

servlet-nametestServlet/servlet-name!--这个名字可以自己定--

servlet-classcom.sun.testServlet/servlet-class!--这里是你需要接收客户端请求的那个类以及包名,也就是下面拦截到的url会转发到的那个类--

/servlet

servlet-mapping

servlet-nametestServlet/servlet-name!--和上面的name需要一样--

url-pattern/*/url-pattern!--什么类型的客户端请求会被拦截,/* 就是全拦截了--

/servlet-mapping

然后再服务器端的类文件,要实现 HttpServlet这个接口。并把doGet()方法和doPost()方法重写。

这两种方法分别对应的是客户端的get请求和post请求的处理,你的是post请求的话,就在doPost()方法内,写你的业务。

然后再用下面两句话,设置你要返回客户端的数据。

//这是设置你要返回去的数据。value才是你的数据,key是标签。

request.setAttribute("key", "value");

//这是设置你要返回去test.jsp这张页面。

request.getRequestDispatcher("test.jsp").forward(request, response);

不知道你是不是这个意思,你可以再去看看相关servlet方面的知识,

关于客户端和服务器端大概也就是有个servlet作为请求的拦截

然后经过相关判断后,选择性的传到服务器的相应类里面。

再经过类里面的业务,把得到需要的数据回传到指定的页面上。

根据要求编写java代码,急求,答案正确加100分!求高手!

服务器端:

package com.lqq.service;

import java.io.File;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.Socket;

import java.util.List;

import com.lqq.vo.QueryType;

import com.lqq.vo.Student;

public class DealClientRequest implements Runnable

{

private Socket s = null;

private ParserXML p = new ParserXML(new File("students.xml"));

public DealClientRequest(Socket s)

{

this.s = s;

}

@Override

public void run()

{

if(s != null)

{

try

{

ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

QueryType qt = (QueryType) ois.readObject();

if(qt.getQueryType() == 1)//单查

{

System.out.println("执行单查,查询的名字= " + qt.getQueryName());

Student stu = p.getStudent(qt.getQueryName());

oos.writeObject(stu);

}else if(qt.getQueryType() == 2)//全查

{

System.out.println("执行全查");

ListStudent list = p.getAllStudent();

for(int i = 0; i list.size(); i++)

{

Student stu = list.get(i);

oos.writeObject(stu);

}

}

} catch (IOException e)

{

e.printStackTrace();

} catch (ClassNotFoundException e)

{

e.printStackTrace();

}

}

}

}

*************************

package com.lqq.service;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class MainService

{

private ServerSocket ss;

public void startService() throws IOException

{

ss = new ServerSocket(10086);

while(true)

{

System.out.println("服务器在10086等待...");

Socket s = ss.accept();

new Thread(new DealClientRequest(s)).start();

System.out.println("启动处理线程成功");

}

}

}

******************

package com.lqq.service;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import org.w3c.dom.Text;

import com.lqq.vo.Student;

public class ParserXML

{

private DocumentBuilderFactory bdf;

private DocumentBuilder db;

private Document dom;

public ParserXML(File file)

{

bdf = DocumentBuilderFactory.newInstance();

try

{

db = bdf.newDocumentBuilder();

dom = db.parse(file);

} catch (Exception e)

{

e.printStackTrace();

}

}

public ListStudent getAllStudent()

{

ListStudent stu = new ArrayListStudent();

Element root = dom.getDocumentElement();

NodeList list = root.getElementsByTagName("stu");

for(int i = 0; i list.getLength(); i++)

{

Element e = (Element) list.item(i);

Student st = new Student();

NodeList names = e.getElementsByTagName("name");

if(names.getLength() == 1)

{

Element name = (Element)names.item(0);

Text text = (Text) name.getFirstChild();

st.setStuName(text.getNodeValue());

}

NodeList sexs = e.getElementsByTagName("sex");

if(sexs.getLength() == 1)

{

Element name = (Element)sexs.item(0);

Text text = (Text) name.getFirstChild();

st.setStuGender(text.getNodeValue());

}

NodeList ages = e.getElementsByTagName("age");

if(ages.getLength() == 1)

{

Element name = (Element)ages.item(0);

Text text = (Text) name.getFirstChild();

st.setStuAge(Integer.parseInt(text.getNodeValue()));

}

NodeList classs = e.getElementsByTagName("class");

if(classs.getLength() == 1)

{

Element name = (Element)classs.item(0);

Text text = (Text) name.getFirstChild();

st.setStuClassName(text.getNodeValue());

}

stu.add(st);

}

return stu;

}

public Student getStudent(String stuName)

{

ListStudent list = this.getAllStudent();

for(int i = 0; i list.size(); i++)

{

Student st = list.get(i);

if(st.getStuName().equals(stuName))

return st;

}

return null;

}

}

*************

package com.lqq.service;

import java.io.IOException;

public class Service

{

public static void main(String[] args)

{

MainService ms = new MainService();

try

{

ms.startService();

} catch (IOException e)

{

e.printStackTrace();

}

}

}

*******

package com.lqq.vo;

import java.io.Serializable;

public class QueryType implements Serializable

{

private static final long serialVersionUID = 8713870836629046060L;

/**

* 1 单查

* 2 全查

*/

private byte queryType;

private String queryName;

public byte getQueryType()

{

return queryType;

}

public void setQueryType(byte queryType)

{

this.queryType = queryType;

}

public String getQueryName()

{

return queryName;

}

public void setQueryName(String string)

{

this.queryName = string;

}

}

************

package com.lqq.vo;

import java.io.Serializable;

public class Student implements Serializable

{

private static final long serialVersionUID = -6087251613589160139L;

private String stuName;

private int stuAge;

private String stuGender;

private String stuClassName;

@Override

public String toString()

{

return "姓名: " + stuName + "\t性别: " + stuGender

+ "\t年龄: " + stuAge + "\t班级: " + stuClassName;

}

public String getStuName()

{

return stuName;

}

public void setStuName(String stuName)

{

this.stuName = stuName;

}

public int getStuAge()

{

return stuAge;

}

public void setStuAge(int stuAge)

{

this.stuAge = stuAge;

}

public String getStuGender()

{

return stuGender;

}

public void setStuGender(String stuGender)

{

this.stuGender = stuGender;

}

public String getStuClassName()

{

return stuClassName;

}

public void setStuClassName(String stuClassName)

{

this.stuClassName = stuClassName;

}

}

*************

?xml version="1.0" encoding="UTF-8"?

stus

stu

name令狐冲/name

age12/age

class华山派/class

sex男/sex

/stu

stu

name东方不败/name

age22/age

class日月神教/class

sex女/sex

/stu

stu

name岳不群/name

age23/age

class华山派/class

sex妖/sex

/stu

stu

name风清扬/name

age88/age

class华山派/class

sex男/sex

/stu

/stus

客户端:

package com.lqq.c;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

import com.lqq.vo.QueryType;

import com.lqq.vo.Student;

public class AllQuery implements Runnable

{

private Socket s;

public AllQuery(QueryType qt)

{

try

{

s = new Socket("localhost", 10086);

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

oos.writeObject(qt);

} catch (UnknownHostException e)

{

e.printStackTrace();

} catch (IOException e)

{

e.printStackTrace();

}

}

@Override

public void run()

{

if(s != null)

{

try

{

ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

Student stu = (Student) ois.readObject();

Client c = new Client();

while(stu != null)

{

c.showStudentInfo(stu);

stu = (Student) ois.readObject();

}

} catch (IOException e)

{

e.printStackTrace();

} catch (ClassNotFoundException e)

{

e.printStackTrace();

}

}

}

}

**************

package com.lqq.c;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.Socket;

import java.util.Scanner;

import com.lqq.vo.QueryType;

import com.lqq.vo.Student;

public class Client

{

public void showStudentInfo(Student stu)

{

if(stu != null)

{

System.out.println(stu);

}

}

public void showMenu()

{

System.out.println("\t\t菜  单");

System.out.println("\t1.单 查");

System.out.println("\t2.全 查");

System.out.println("\t0.退 出");

}

public String getUserInput()

{

Scanner scan = new Scanner(System.in);

System.out.println("请输入:");

return scan.next();

}

public void requestQuery(String userSelect)

{

if(userSelect != null)

{

if("1".equals(userSelect.trim()))

{

QueryType qt = new QueryType();

qt.setQueryType((byte)1);

Scanner san = new Scanner(System.in);

System.out.println("请输入查询的学生名字:");

String queryStuName = san.next();

qt.setQueryName(queryStuName);

this.executeQeury(qt);

}else if("2".equals(userSelect.trim()))

{

QueryType qt = new QueryType();

qt.setQueryType((byte)2);

this.executeQeury(qt);

}else if("0".equals(userSelect.trim()))

{

System.exit(0);

}

else

{

System.out.println("输入有误");

}

}

}

public void executeQeury(QueryType qt)

{

try

{

Socket s = new Socket("localhost", 10086);

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

oos.writeObject(qt);

System.out.println("请求发送完毕");

ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

if(qt.getQueryType() == 1)

{

Student stu = (Student) ois.readObject();

showStudentInfo(stu);

}else if(qt.getQueryType() == 2)

{

new Thread(new AllQuery(qt)).start();

}

} catch (IOException e)

{

e.printStackTrace();

} catch (ClassNotFoundException e)

{

e.printStackTrace();

}

}

public static void main(String[] args)

{

Client client = new Client();

while(true)

{

client.showMenu();

client.requestQuery(client.getUserInput());

}

}

}

**********

package com.lqq.vo;

import java.io.Serializable;

public class QueryType implements Serializable

{

private static final long serialVersionUID = 8713870836629046060L;

/**

* 1 单查

* 2 全查

*/

private byte queryType;

private String queryName;

public byte getQueryType()

{

return queryType;

}

public void setQueryType(byte queryType)

{

this.queryType = queryType;

}

public String getQueryName()

{

return queryName;

}

public void setQueryName(String string)

{

this.queryName = string;

}

}

**********

package com.lqq.vo;

import java.io.Serializable;

public class Student implements Serializable

{

private static final long serialVersionUID = -6087251613589160139L;

private String stuName;

private int stuAge;

private String stuGender;

private String stuClassName;

@Override

public String toString()

{

return "姓名: " + stuName + "\t性别: " + stuGender

+ "\t年龄: " + stuAge + "\t班级: " + stuClassName;

}

public String getStuName()

{

return stuName;

}

public void setStuName(String stuName)

{

this.stuName = stuName;

}

public int getStuAge()

{

return stuAge;

}

public void setStuAge(int stuAge)

{

this.stuAge = stuAge;

}

public String getStuGender()

{

return stuGender;

}

public void setStuGender(String stuGender)

{

this.stuGender = stuGender;

}

public String getStuClassName()

{

return stuClassName;

}

public void setStuClassName(String stuClassName)

{

this.stuClassName = stuClassName;

}

}

时间太紧,没有完善代码,基本功能实现了,代码没有优化,你自己再改改,希望可以帮到你

关于java,soap服务器端的代码怎么写

soapenv:Header/

soapenv:Body

api:getEmp

shopIdstring/shopId

/api:getEmp

/soapenv:Body

/soapenv:Envelope

这个时SOAP协议的标准报文格式,客户端只要发送这样的报文给支持SOAP协议的webservice服务器即可成功调用web service服务

服务端:部署服务只需要自己定义服务的接口和实现类,并用@webservice注解,通过endPoint类直接发布即可

做web服务器的一些简单的代码如何写?(用Java里面的面向对象的知识和sql的东西写)

用java写一个页向对像的web 服务器,得借助工具!

不然估计把你累吐血了!

想用java做一个web 服务器你得懂这样的几个方面!

xml,servlet,jsp,javabean用这些应该做一具小型的差不多了!

首先,jsp属性界面类,响应客户端的请求发送给servlet!

servlet控制类,sevlet接受客户端的请求做相应的处理!

比如,调用javabean的数据库连接的类!

做完一系列操作再由servlet转发到jsp,显示客户所需要的数据!


网页题目:服务器怎么写java代码,java服务器编程
转载注明:http://pwwzsj.com/article/hdsddj.html