当前位置: 技术问答>java相关
第一次写servlet,有问题请教
来源: 互联网 发布时间:2015-05-26
本文导语: 准备用Applet向Servlet提交对象,用对象流 请问这么些为什么会有问题? Applet: import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.net.*; import java.io.*; public class Applet1 extends JApplet { ...
准备用Applet向Servlet提交对象,用对象流
请问这么些为什么会有问题?
Applet:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Applet1 extends JApplet {
//Initialize the applet
public void init() {
try {
URL url = new URL("http://localhost:8080/servlet1");
URLConnection con = url.openConnection();
con.setDoOutput(true);
test tt = new test();
tt.a1="dddddddddd";
tt.a2="dddddfasd";
OutputStream out = con.getOutputStream();
ObjectOutputStream objStream;
objStream = new ObjectOutputStream(out);
objStream.writeObject(tt);
}
catch(Execption e){}
}
}
Servlet1:
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 {
response.setContentType(CONTENT_TYPE);
InputStream in = request.getInputStream();
ObjectInputStream objStream;
objStream = new ObjectInputStream(in);
test tt = new test();
try{
tt = (test)objStream.readObject();
}
catch(ClassNotFoundException e)
{
}
System.out.println(tt.a1+tt.a2);
}
//Clean up resources
public void destroy() {
}
}
test:
public class test {
String a1="aaaa";
String a2="bbbb";
}
请问这么些为什么会有问题?
Applet:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Applet1 extends JApplet {
//Initialize the applet
public void init() {
try {
URL url = new URL("http://localhost:8080/servlet1");
URLConnection con = url.openConnection();
con.setDoOutput(true);
test tt = new test();
tt.a1="dddddddddd";
tt.a2="dddddfasd";
OutputStream out = con.getOutputStream();
ObjectOutputStream objStream;
objStream = new ObjectOutputStream(out);
objStream.writeObject(tt);
}
catch(Execption e){}
}
}
Servlet1:
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 {
response.setContentType(CONTENT_TYPE);
InputStream in = request.getInputStream();
ObjectInputStream objStream;
objStream = new ObjectInputStream(in);
test tt = new test();
try{
tt = (test)objStream.readObject();
}
catch(ClassNotFoundException e)
{
}
System.out.println(tt.a1+tt.a2);
}
//Clean up resources
public void destroy() {
}
}
test:
public class test {
String a1="aaaa";
String a2="bbbb";
}
|
你用的是什么服务器? tomcat?
可能是你的 servlet 路径设置不对吧, 没有联上.
可能是你的 servlet 路径设置不对吧, 没有联上.