论坛首页 Java版 OO

求一个java(applet)向servlet写对象的实例writeObject

浏览 641 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-02-29
折磨我两天了,网上的资料都有,但是自己这里就是不工作
客户端没有抱错,服务器段连方法都没进入,求实际工作代码  ,谢谢
   
最后更新时间:2008-02-29
没有说清需求呀。
   
0 请登录后投票
最后更新时间:2008-03-01
客户端代码如下
public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("haha");
			URL url = new URL("http://127.0.0.1:8080/smweb/SaveImage.save");
			URLConnection urlcon = url.openConnection();
			//urlcon.connect();
			urlcon.setDoOutput(true);
			urlcon.setDoInput(true);
			urlcon.setUseCaches(false);
			urlcon.setRequestProperty("Content-Type", "application/x-java-serialized-object");
			ObjectOutputStream oos = new ObjectOutputStream(urlcon
					.getOutputStream());
			oos.writeObject(new String("达到"));
			oos.flush();
			oos.close();
			System.out.println("haha");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


服务器段如下
protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		System.out.println("====================hahahaha");
		ObjectInputStream oin = new ObjectInputStream(req.getInputStream());
		Object vo=null;
		try {
			vo = oin.readObject();
			oin.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("============"+vo);
	}


	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		doPost(req,res);
	}

没有什么配置上的问题,服务器servlet可以通过输入url访问得到,没有配置错误
客户端运行也没有抱错,但是实际结果服务器段方法根本没有运行
   
0 请登录后投票
最后更新时间:2008-03-03
你好象调用的就不正确呀,我感觉。所以客户端执行,服服端不执行。再看一看服务器启没启动。你还没有向服务器发送请求,在oos.close()后加urlcon.getInputStream().你在试一下。
   
0 请登录后投票
最后更新时间:2008-03-03
zingers 写道
客户端代码如下
public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("haha");
			URL url = new URL("http://127.0.0.1:8080/smweb/SaveImage.save");
			URLConnection urlcon = url.openConnection();
			//urlcon.connect();
			urlcon.setDoOutput(true);
			urlcon.setDoInput(true);
			urlcon.setUseCaches(false);
			urlcon.setRequestProperty("Content-Type", "application/x-java-serialized-object");
			ObjectOutputStream oos = new ObjectOutputStream(urlcon
					.getOutputStream());
			oos.writeObject(new String("达到"));
			oos.flush();
			oos.close();
			System.out.println("haha");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


服务器段如下
protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		System.out.println("====================hahahaha");
		ObjectInputStream oin = new ObjectInputStream(req.getInputStream());
		Object vo=null;
		try {
			vo = oin.readObject();
			oin.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("============"+vo);
	}


	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		doPost(req,res);
	}

没有什么配置上的问题,服务器servlet可以通过输入url访问得到,没有配置错误
客户端运行也没有抱错,但是实际结果服务器段方法根本没有运行


你调试一下,如果没有跳进服务器端的话,说明你的路径有问题检查一下你的路径,你可以把http://127.0.0.1:8080/smweb/SaveImage.save拷贝到ie或firefox的url地址栏中调试一下,看看有没有跳到服务器端的代码中,如果能跳进来的而出错的话,就是你的服务器端的代码有问题了;
   
0 请登录后投票
最后更新时间:2008-03-03
这个是因为没有设置Cookie所引起的
   
0 请登录后投票
最后更新时间:2008-03-03
这个和cookie有什么关系呢?
如果我的客户端是java不就是没有cookie了么?
   
0 请登录后投票
最后更新时间:2008-03-03
路径是对的,单独碾贴出来到IE是有恰当的反应的
   
0 请登录后投票
最后更新时间:2008-03-03
在看了jguru一个帖子后搞定了,applet端加一条语句
String header = urlcon.getHeaderField(0);就可以了
   
0 请登录后投票
论坛首页 Java版 OO

跳转论坛:
JavaEye推荐