JSP实现HTTP隧道
2009-10-25 16:35:25 来源:WEB开发网共有两个java类和一个servlet(在同一个java包JavaSerializable中):
java:StudentList StudentListTunnetApp(客户端)
servlet:StudentListTunnetServlet(服务器端)
StudentList(java类)定义一个类,StudentListTunnetApp(java类)运行在客户端,实例化一个StudentList对象并将其写到与StudentListTunnetServlet连接的HTTP中,然后发送请求到服务器端StudentListTunnetServlet(servlet)读取先前写入连接的StudentList对象
1.StudentList.java:
/**
*
* @author lucifer
*/
package JavaSerializable;
import java.util.*;
import java.io.*;
public class StudentList implements Serializable{
Vector list = new Vector(6);
public StudentList(){}
public void addStudent(String name){
if(name != null)
list.addElement(name);
}
public void listStudent(){
for(int i = 0;i < list.size();i++){
System.out.PRintln("Student" + i + ":" + (String)list.elementAt( i ));
}
}
}
2.StudentListTunnetApp.java:
/**
*
* @author lucifer
*/
package JavaSerializable;
import java.io.*;
import java.net.*;
public class StudentListTunnetApp {
public StudentListTunnetApp(){}
public void buildStudentList(StudentList list){
list.addStudent("Bob Robinson");
list.addStudent("Steve Robinson");
list.addStudent("Rob Stevinson");
list.addStudent("Tod Thomson");
list.addStudent("Jack Jones");
list.addStudent("Micheal Jackson");
}
public void writeStudentList(URLConnection connection,StudentList list){
try{
//ignore caching
connection.setUseCaches(false);
connection.setRequestProperty("CONTENT_TYPE","application/octet-stream");
//使得发送和接收能使用用一个连接
connection.setDoInput(true);
connection.setDoOutput(true);
ObjectOutputStream os = new ObjectOutputStream(connection.getOutputStream());
System.err.println("Writing an object.");
os.writeObject( list );
os.flush();
os.close();
}
catch(IOException e){
System.err.println(e.getMessage());
}
}
public StudentList readStudentList(URLConnection connection){
StudentList list = null;
try{
ObjectInputStream is = new ObjectInputStream(connection.getInputStream());
System.err.println("Waiting for response.");
list = (StudentList)is.readObject();
is.close();
}
catch(IOException e){
System.err.println(e.getMessage());
}
catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
return list;
}
public void invoke(){
try{
URL url = new URL("http://localhost:8084/LearnServlet/StudentListTunnetServlet");
StudentList list = new StudentList();
buildStudentList(list);
list.listStudent();
System.err.println("Opening an connection.");
URLConnection connection = url.openConnection();
writeStudentList(connection,list);
StudentList inlist = readStudentList(connection);
if(inlist != null){
inlist.listStudent();
}
else{
System.err.println("readObject failed.");
}
System.out.println("press enter to quit.");
System.in.read();
}
catch(MalformedURLException e){
System.err.println(e.getMessage());
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
public static void main(String[] args){
StudentListTunnetApp studentlist = new StudentListTunnetApp();
studentlist.invoke();
}
}
3.StudentListTunnetServlet.java(servlet):
/**
*
* @author lucifer
*/
package JavaSerializable;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class StudentListTunnetServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet StudentListTunnetServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet StudentListTunnetServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
@Override
//用的是这个方法
public void service (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
try{
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
StudentList list = (StudentList)ois.readObject();
response.setContentType("application/octet-stream");
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(list);
oos.flush();
oos.close();
}
catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
@Override
public void init(ServletConfig config)throws ServletException{
super.init(config);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
赞助商链接