我在网上搜的时候,HttpConnection这个词频繁出现,但是我发现这个好像是J2ME开发里才有的类包,不知道J2EE里应该怎么连接。
我大概解释一下我的需求:
1.写一个servlet,接收别人以GET或POST(2种方式都要支持)提交给我的数据.
2.写另外一个servlet,以GET或POST(2种方式都要支持)方式提交我的数据信息给别人的一个URL,别人在接收到我的信息后会给我一个反馈信息,我要拿到这个反馈信息并记录到日志里。
小弟是初学JAVA,只知道PHP里是怎么做的,由于时间紧,元旦还在加班,忘达人赐教,不胜感激。
我大概解释一下我的需求:
1.写一个servlet,接收别人以GET或POST(2种方式都要支持)提交给我的数据.
2.写另外一个servlet,以GET或POST(2种方式都要支持)方式提交我的数据信息给别人的一个URL,别人在接收到我的信息后会给我一个反馈信息,我要拿到这个反馈信息并记录到日志里。
小弟是初学JAVA,只知道PHP里是怎么做的,由于时间紧,元旦还在加班,忘达人赐教,不胜感激。
解决方案 »
- 我从Eclipse中导出可执行jar,在别的机器上执行不了,buildpath中的内容怎么导出?
- Tree的节点前怎样加一个‘+’ 或‘—’图标
- swt不能做企业级应用!? 在 windows下好像不能改变按钮背景颜色??
- 看看它的运行结果,说下为什么
- 急救,SOCKET通讯时DataInputStream.readInt()时偶尔程序死
- 我想问问!!我做的聊天室程序,我在本机上连接2个客户端试试都可以连通,但是我发给别的主机,为什么他就连接不上我呢?而且提示有java.
- 大家给点建议!谢谢
- 急急急,谁知道怎么用java生成excel报表?
- 一个连接数据库的简单问题,请高手多帮忙
- 请求详解这道面试题
- 求救: jdbc 的lockWaitTime属性为什么没有超时效果,一直等待到锁释放?
- 一个RS的问题
java.net.URL u=null;
java.net.URLConnection uc=u.openConnection();http://www.google.com/search?q=URLConnection
addr = localhost/127.0.0.1
socket = Socket[addr=localhost/127.0.0.1,port=8080,localport=1933]
null
null
null
null
null
null
null
null
null
null
closing...//: c15:JabberServer.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Very simple server that just
// echoes whatever the client sends.
import java.io.*;
import java.net.*;public class JabberServer {
// Choose a port outside of the range 1-1024:
public static final int PORT = 8080;
public static void main(String[] args)
throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
System.out.println(
"Connection accepted: "+ socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
// Always close the two sockets...
} finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
} ///:~//: c15:JabberClient.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Very simple client that just sends
// lines to the server and reads lines
// that the server sends.
import java.net.*;
import java.io.*;public class JabberClient {
public static void main(String[] args)
throws IOException {
// Passing null to getByName() produces the
// special "Local Loopback" IP address, for
// testing on one machine w/o a network:
InetAddress addr =
InetAddress.getByName(null);
// Alternatively, you can use
// the address or name:
// InetAddress addr =
// InetAddress.getByName("127.0.0.1");
// InetAddress addr =
// InetAddress.getByName("localhost");
System.out.println("addr = " + addr);
Socket socket =
new Socket(addr, JabberServer.PORT);
// Guard everything in a try-finally to make
// sure that the socket is closed:
try {
System.out.println("socket = " + socket);
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Output is automatically flushed
// by PrintWriter:
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
for(int i = 0; i < 10; i ++) {
out.println("howdy " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} finally {
System.out.println("closing...");
socket.close();
}
}
} ///:~
URL url=new URL("http://www.csdn.net");
URLConnection uc=(HttpConnection)url.openConnection();
====================================
老兄把j2me和j2ee区别得太开了点吧.既然两者都是被统称为java,用法都是一样的.
参考一下Socket方法
public String write(String name){
String nameInfo = "";
String result = "";
try{
URL url = new URL("http://test.com/test.jsp);
URLConnection connSpms = url.openConnection();
if (connSpms instanceof HttpURLConnection) {
HttpURLConnection httpConnection=(HttpURLConnection) connSpms;
httpConnection.connect();
String strXML="java test"
OutputStream out = connSpms.getOutputStream())),true);
out.write(strXML);
}
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(result+"\n");
return result;
}