当前位置: 技术问答>java相关
applet与servlet的通信,我想将APPLET中的参数传给SERVLET,总是通不过,详情请进
来源: 互联网 发布时间:2015-02-25
本文导语: applet源码 import..... ... public void executeQuery(){ String qryTitle = "parameter1"; String title = "Servlet/ReceiveServlet?title=" + URLEncoder.encode(qryTitle); try { //ucSend和sendURL已在前边定义 ...
applet源码
import.....
...
public void executeQuery(){
String qryTitle = "parameter1";
String title = "Servlet/ReceiveServlet?title=" + URLEncoder.encode(qryTitle);
try {
//ucSend和sendURL已在前边定义
ucSend = (new URL(sendURL,qryTitle)).openConnection();
showStatus("正在连接服务器");
ucSend.setDefaultUseCaches(false);
ucSend.setUseCaches(false);
ucSend.setDoInput(true);
ucSend.setDoOutput(false);
ucSend.connect();
showStatus("打开流");
DataInputStream in = new DataInputStream(ucSend.getInputStream());
showStatus("读数据");
title=in.readLine();
}catch(IOException e2) {
System.err.println("IOException!");
e2.printStackTrace(System.err);
showStatus("IO错误");
}
}
我的SERVLET源码如下:
import...
public class ReceiveServlet extends HttpServlet {
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
ServletOutputStream out=res.getOutputStream();
out.print("1 is the title:");
out.println(req.getParameter("title"));
// out.print("2 is the area:");
// out.println(req.getParameter("area"));
// out.print("3 is the index:");
// out.println(req.getParameter("index"));
// out.print("4 is the atime:");
// out.print(req.getParameter("atime"));
}
public String getServletInfo()
{
return "aaaaaa";
}
}
总是报io错码,在ucSend.connect()时,望高手指点。
import.....
...
public void executeQuery(){
String qryTitle = "parameter1";
String title = "Servlet/ReceiveServlet?title=" + URLEncoder.encode(qryTitle);
try {
//ucSend和sendURL已在前边定义
ucSend = (new URL(sendURL,qryTitle)).openConnection();
showStatus("正在连接服务器");
ucSend.setDefaultUseCaches(false);
ucSend.setUseCaches(false);
ucSend.setDoInput(true);
ucSend.setDoOutput(false);
ucSend.connect();
showStatus("打开流");
DataInputStream in = new DataInputStream(ucSend.getInputStream());
showStatus("读数据");
title=in.readLine();
}catch(IOException e2) {
System.err.println("IOException!");
e2.printStackTrace(System.err);
showStatus("IO错误");
}
}
我的SERVLET源码如下:
import...
public class ReceiveServlet extends HttpServlet {
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
ServletOutputStream out=res.getOutputStream();
out.print("1 is the title:");
out.println(req.getParameter("title"));
// out.print("2 is the area:");
// out.println(req.getParameter("area"));
// out.print("3 is the index:");
// out.println(req.getParameter("index"));
// out.print("4 is the atime:");
// out.print(req.getParameter("atime"));
}
public String getServletInfo()
{
return "aaaaaa";
}
}
总是报io错码,在ucSend.connect()时,望高手指点。
|
Talk to a CGI/Servlet
From the client point of view, there is no difference talking to CGI or Servlet. There is two ways to send a request to a CGI. The GET method contains encoded parameters in the URL. A typical URL talking to CGI using the GET method would be: new URL("http://www.server.com/cgi-bin/aCGI.pl?name=Real&site=JAVA+HowTo");
Here we calling a script called aCGI.pl (a PERL script) passing the parameters name and site. Parameters are encoded, spaces are changed to "+" and special character to hexadecimal using a 3-letter escape sequence. Each parameter is delimited by the character "&". Habitually the encoding is done through the static method encode of the java.net.URLencoder class. String theCGI = "http://www.server.com/cgi-bin/aCGI.pl?";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI + encoded);
Once the URL is constructed, you call the CGI using the showDocument method. getAppletContext().showDocument(CGIurl);
The CGI will process the result and produce a page to be displayed.
The POST method allows the programmer to manipulate the data received from the CGI. First a connection is made to the CGI, an OutputStream is open to send the parameters (if any). Then InputStream is created to receive the result. String theCGI = "http://www.server.com/cgi-bin/aCGI.pl";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI);
URLConnection c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream());
String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}
You can't do some output then some input and do again some output. You must do all the output and then the input. There is no "dialog" between the client and the server. The client make a request and the server send back the result and close the connection.
From the client point of view, there is no difference talking to CGI or Servlet. There is two ways to send a request to a CGI. The GET method contains encoded parameters in the URL. A typical URL talking to CGI using the GET method would be: new URL("http://www.server.com/cgi-bin/aCGI.pl?name=Real&site=JAVA+HowTo");
Here we calling a script called aCGI.pl (a PERL script) passing the parameters name and site. Parameters are encoded, spaces are changed to "+" and special character to hexadecimal using a 3-letter escape sequence. Each parameter is delimited by the character "&". Habitually the encoding is done through the static method encode of the java.net.URLencoder class. String theCGI = "http://www.server.com/cgi-bin/aCGI.pl?";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI + encoded);
Once the URL is constructed, you call the CGI using the showDocument method. getAppletContext().showDocument(CGIurl);
The CGI will process the result and produce a page to be displayed.
The POST method allows the programmer to manipulate the data received from the CGI. First a connection is made to the CGI, an OutputStream is open to send the parameters (if any). Then InputStream is created to receive the result. String theCGI = "http://www.server.com/cgi-bin/aCGI.pl";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI);
URLConnection c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream());
String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}
You can't do some output then some input and do again some output. You must do all the output and then the input. There is no "dialog" between the client and the server. The client make a request and the server send back the result and close the connection.