五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網 > 365文章 > JSP實現(xiàn)HTTP隧道

JSP實現(xiàn)HTTP隧道

文章來源:365jz.com     點擊數:315    更新時間:2009-10-14 10:51   參與評論

共有兩個java類和一個servlet(在同一個java包JavaSerializable中):

java:StudentList   StudentListTunnetApp(客戶端)

servlet:StudentListTunnetServlet(服務器端)


StudentList(java類)定義一個類,StudentListTunnetApp(java類)運行在客戶端,實例化一個StudentList對象并將其寫到與StudentListTunnetServlet連接的HTTP中,然后發(fā)送請求到服務器端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");

                  //使得發(fā)送和接收能使用用一個連接
                  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";
    }

}

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網友會為你解答??! 點擊進入論壇

發(fā)表評論 (315人查看,0條評論)
請自覺遵守互聯(lián)網相關的政策法規(guī),嚴禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學習

業(yè)務咨詢

· 技術支持
· 服務時間:9:00-18:00
365建站網二維碼

Powered by 365建站網 RSS地圖 HTML地圖

copyright © 2013-2024 版權所有 鄂ICP備17013400號