2012-10-09 19 views
7

मैं जावा क्लास से एक सर्वलेट ऑब्जेक्ट को एक सर्वलेट में भेजना चाहता हूं जहां सर्वलेट को ऑब्जेक्ट को पुनर्प्राप्त करना चाहिए और ऑब्जेक्ट को फ़ाइल के रूप में सहेजना चाहिए। मुझे पता है कि मुझे सर्वलेट को POST अनुरोध करने के लिए HttpURLConnection का उपयोग करना है, लेकिन मुझे नहीं पता कि नीचे दिया गया कोड सही है या नहीं।जावा क्लास से सर्वलेट को क्रमबद्ध ऑब्जेक्ट भेजने के लिए HttpURLConnection का उपयोग कैसे करें?

private static HttpURLConnection urlCon; 
private static ObjectOutputStream out; 

public static void main(String[] args) { 

    Names names = new Names(); 
    names.setName("ABC"); 
    names.setPlace("Bangalore"); 
    URL url; 
    try { 
     url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet"); 
    try { 
     out = (ObjectOutputStream) urlCon.getOutputStream(); 
     out.writeObject(names); 
     urlCon = (HttpURLConnection) url.openConnection(); 
     urlCon.setRequestMethod("POST"); 
     urlCon.setDoOutput(true); 
     out.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     } catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
     } 
} 

और सर्वलेट में, मैं निम्नलिखित कोड है:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     ObjectInputStream in = new ObjectInputStream(request.getInputStream()); 
     try { 
      names = (Names) in.readObject(); 

     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     in.close(); 
     ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names")); 
     out.writeObject(names); 
     out.close(); 

    } 

क्या मैं इसे काम करने के लिए क्या करना चाहिए? साथ ही, मैं चाहता हूं कि सर्वलेट उस ऑब्जेक्ट को वापस भेज दें जिसे वह प्रतिक्रिया के रूप में प्राप्त करता है।

किसी भी मदद की सराहना की जाएगी। धन्यवाद!

+0

आप क्यों नहीं बस JSON या आंकड़ा अंतरण प्रारूप के रूप में XML का उपयोग नहीं करते हैं? – BalusC

+0

मैंने 'जीसन' लाइब्रेरी का उपयोग किया और यह ठीक काम किया। बस धारावाहिक वस्तुओं को भेजने की कोशिश करना चाहता था। – Rajath

+0

मुझे आशा है कि आप जल्द ही अपने प्रमुख नुकसान को प्रभावित करेंगे। यह अक्सर लंबी अवधि पर खुलासा करता है। – BalusC

उत्तर

8

आप

  • करने के लिए सुनिश्चित Names वर्ग को लागू करता है java.io.Serializable मार्कर इंटरफेस बनाने की आवश्यकता होगी।
  • सर्वलेट का outputstream से एक ObjectOutputStream बनाएं इस प्रकार है:

    out = new ObjectOutputStream(urlCon.getOutputStream()); 
    
  • रिसीवर पक्ष पर, एक बार आप सर्वलेट का InputStream से वस्तु पढ़ सकते हैं और फ़ाइल में जारी रहती है, इसे वापस प्रतिक्रिया के उत्पादन स्ट्रीम में लिखें इस प्रकार है:

    out = new ObjectOutputStream(response.getOutputStream()); 
    out.writeObject(names); 
    out.close(); 
    

कि प्रेषक की पर:

Names names = new Names(); 
names.setName("ABC"); 
names.setPlace("Bangalore"); 
URL url; 
try { 
    url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet"); 
    urlCon = (HttpURLConnection) url.openConnection(); 

    urlCon.setDoOutput(true); // to be able to write. 
    urlCon.setDoInput(true); // to be able to read. 

    out = new ObjectOutputStream(urlCon.getOutputStream()); 
    out.writeObject(names); 
    out.close(); 

    ObjectInputStream ois = new ObjectInputStream(urlCon.getInputStream()); 
    names = (Names) ois.readObject(); 
    ois.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
catch (MalformedURLException e1) { 
    e1.printStackTrace(); 
} 

रिसीवर की ओर से:

ObjectInputStream in = new ObjectInputStream(request.getInputStream()); 
try { 
    names = (Names) in.readObject(); 
} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} 
in.close(); 
ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names")); 
out.writeObject(names); 
out.close(); 

ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream()); 
oos.writeObject(names); 
oos.close(); 
+0

आपको बहुत बहुत धन्यवाद! लेकिन ऑब्जेक्ट 'आउट' किस प्रकार का होना चाहिए? मुझे 'java.lang.ClassCastException मिल रहा है: sun.net.www.http.PosterOutputStream को java.io.ObjectOutputStream' को लाइन 'out = new ऑब्जेक्टऑटपुटस्ट्रीम (urlCon.getOutputStream()) के लिए नहीं डाला जा सकता है; प्रेषक में । 'आउट'' का प्रकार घोषित किया गया है 'ऑब्जेक्टऑटपुटस्ट्रीम'। – Rajath

+0

सुनिश्चित नहीं है कि आपको वह त्रुटि क्यों मिल रही है लेकिन मैं इसे सामान्य रूप से चलाने में सक्षम हूं। E.g यूआरएल यूआरएल = नया यूआरएल ("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod ("POST"); con.setDoOutput (सत्य); con.setDoInput (सत्य); ऑब्जेक्टऑटपुटस्ट्रीम ओओएस = नया ऑब्जेक्टऑटपुटस्ट्रीम (con.getOutputStream()); oos.close(); – Vikdor

+0

पूरी तरह से ठीक काम किया! धन्यवाद! :) – Rajath

संबंधित मुद्दे

 संबंधित मुद्दे