applet调用servlet的问题
我写了一个applet,调用servlet但是servlet总是没有相应?
applet的代码:
package testservlet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.*;
import java.io.DataOutputStream;
public class Applet1 extends Applet {
private boolean isStandalone = false;
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
jButton1.setBounds(new Rectangle(143, 70, 73, 25));
jButton1.setText("jButton1");
jButton1.addActionListener(new Applet1_jButton1_actionAdapter(this));
this.setLayout(null);
jTextField1.setSelectionStart(11);
jTextField1.setBounds(new Rectangle(107, 137, 177, 21));
jTextField2.setBounds(new Rectangle(110, 175, 179, 24));
this.add(jButton1, null);
this.add(jTextField1, null);
this.add(jTextField2, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
void jButton1_actionPerformed(ActionEvent e) {
try {
String port = jTextField1.getText();
String sl = "http://localhost:"+port+"/index.jsp";
URL url = new URL(sl);
URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
String strContent = "where are you going?";
int fileLength = strContent.getBytes().length;
con.setRequestProperty("fileLength",Integer.toString(fileLength));
DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
dataOut.write(strContent.getBytes());
dataOut.flush();
dataOut.close();
}
catch (Exception ex) {
}
//URLConnection con = url.openConnection();
}
}
class Applet1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Applet1 adaptee;
Applet1_jButton1_actionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
servlet的代码如下:
package testservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int fileLength = 0;
String fileCotent = "";
byte[] fcn = new byte[fileLength];
DataInputStream ins = new DataInputStream(request.getInputStream());
fileLength = Integer.parseInt(request.getAttribute("Content-length").toString());
ins.read(fcn);
ins.close();
fileCotent = fileCotent+fcn.toString();
if(fileCotent.length()==0) fileCotent = "tedtedted";
WriteFile myWritor = new WriteFile();
myWritor.setPath("C:\\ted.txt");
myWritor.setSomething(fileCotent);
myWritor.writeSomething();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
//Clean up resources
public void destroy() {
}
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>testservlet.Servlet1</servlet-class>
</servlet><servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
</web-app>
我调用applet上的按钮但是总是servlet没有相应,服务器为tomcat5。0,请大家赐教,谢谢
问题点数:0、回复次数:1Top
1 楼ocean617(海洋)回复于 2005-04-02 18:34:00 得分 0
http://www.cn-java.com/target/news.php?news_id=721
建议看看这个..
好像要建立socket 连接..Top




