2009-05-06 8 views
5

मैं अपने जेएसपी फाइलों में से एक में स्थिर फ़ाइल को शामिल करने के लिए "jsp: include" का उपयोग कर रहा हूं। जब यह स्थिर फ़ोल्डर फ़ाइल अनुप्रयोग फ़ोल्डर के अंदर स्थित होता है तो यह ठीक काम करता है। हालांकि अगर यह एप्लिकेशन फ़ोल्डर के बाहर रखा जाता है तो इसे जेएसपी फ़ाइल में शामिल नहीं किया जाता है।जेएसपी का उपयोग कर एप्लिकेशन (युद्ध) के बाहर एक फ़ाइल को शामिल करने के लिए

नोट: मैंने उस फ़ोल्डर के लिए एक संदर्भ बनाया है जहां स्थिर फ़ाइल सहेजी गई है और मैं सीधे यूआरएल के साथ एचटीएमएल फाइल देखने में सक्षम हूं।

कृपया मदद ..

उत्तर

11

मैंने सी का उपयोग करके इस समस्या को हल किया है: आयात टैग।

गतिशील यूआरएल मैं सेम का इस्तेमाल किया है परिभाषित करने के लिए: टैग परिभाषित करते हैं। सुझावों और सहायता के लिए दोस्तों का धन्यवाद।

+0

प्रोटोकॉल उपसर्ग का उपयोग करें: "फ़ाइल: /// $ {आपकी फ़ाइल पथ}" –

+0

@EdgardLeal मुझे लगता है कि 'फ़ाइल' प्रोटोकॉल का उपयोग करने से फ़ाइल क्लाइंट कंप्यूटर में स्थित है। 'सी: आयात' ने इसे मेरे लिए हल किया। –

3

आप केवल अपने वेब अनुप्रयोग संदर्भ अंदर संसाधनों के लिए jsp:include उपयोग कर सकते हैं। क्लासपाथ से संसाधन लोड करने के लिए आपको या तो java.io.File या फ़ाइल सिस्टम पथ से लोड के समान या ClassLoader.getResource का उपयोग करने की आवश्यकता होगी।

+0

हाय पीटर हिल्टन, सुझाव के लिए धन्यवाद। मैंने इस समस्या को हल करने के लिए c: आयात टैग का उपयोग किया है। –

0

बस ब्याज से बाहर - ऐसा करने का क्या कारण है? - एक वैकल्पिक दृष्टिकोण हो सकता है।

मुझे संदेह है कि आप कुछ कॉन्फ़िगरेशन चाहते हैं जो WAR फ़ाइल से स्वतंत्र है, और प्रत्येक वातावरण के लिए अद्वितीय है जिसे WAR तैनात किया गया है।

+1

हाय बेलुगाबोब, मेरे पास युद्ध के तहत सभी जेएसपी हैं और मैं जेएसपी फ़ाइल के भीतर एक स्थिर एचटीएमएल फ़ाइल (हेल्पकार्ड की तरह कुछ कहना) शामिल करना चाहता हूं। मेरे पास इन HTML फ़ाइलों को युद्ध फ़ाइल के बाहर एक और अलग संदर्भ के तहत है। मैंने इसे c का उपयोग करके हल किया है: आयात –

2

जोड़ा गया <c:import> विधि के लाभ आप charEncoding विशेषता का उपयोग एन्कोडिंग सेट कर सकते हैं। आप इसे <%@include%> या <jsp:include> कथन के साथ नहीं कर सकते हैं।

1

मैं एक वर्ग URL से सामग्री प्राप्त करने के लिए एक विधि है का उपयोग करते हुए: पूर्व: http://link.inet.vn/seo-website/inet.html

public class URLReader 
{ 
    public URLReader() 
    { 
     in = null; 
     out = null; 
     requestType = null; 
     headers = null; 
     content = null; 
     headers = new Hashtable(); 
    } 

    public void doGet(String server, String uri, int port) 
    { 
     try{ 
      Socket client = new Socket(server, port); 
      client.setKeepAlive(true); 
      in = new DataInputStream(client.getInputStream()); 
      out = new DataOutputStream(client.getOutputStream()); 
      out.writeBytes("GET " + uri + " HTTP/1.0\r\n"); 
      out.writeBytes("Host: " + server + "\r\n"); 
      out.writeBytes("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n"); 
      out.writeBytes("Accept-Language: en-us\r\n"); 
      out.writeBytes("Accept-Encoding: gzip, deflate\r\n"); 
      out.writeBytes("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n"); 
      out.writeBytes("Connection: Keep-Alive\r\n"); 
      out.writeBytes("Content-Length: 0\r\n\r\n"); 
      out.flush(); 
      parseRequest(); 
      out.close(); 
      in.close(); 
      client.close();   
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 

     return; 
    } 

    public byte[] getContent() 
    { 
     return content; 
    } 

    public String getHeader(String name) 
    { 
     String key = (String)headers.get(name); 
     return key; 
    } 

    public Hashtable getHeaders() 
    { 
     return headers; 
    } 

    public String getRequestType() 
    { 
     return requestType; 
    } 

    public static void main(String args[]) throws IOException 
    { 
     URLReader reader = new URLReader(); 

     reader.doGet("link.inet.vn", "/seo-website/inet.html", 80); 
     if(reader.getContent() != null) 
      System.out.println(new String(reader.getContent(),"UTF-8")); 

    } 

    private boolean parseRequest() 
    { 
     byte match[]; 
     int index; 
     String line; 
     match = (new byte[] { 
      13, 10, 13, 10 
     }); 
     index = 0; 
     line = ""; 
     int i; 
     try{ 
      while((i = in.read()) >= 0) 
      { 
       if(i == match[index] && index <= 3) 
        index++; 
       else 
        index = 0; 
       if(index == 4) 
       { 
        content = readHTTPContent(); 
        break; 
       } 
       line = line + (char)i; 
       if(line.length() > 2 && i == 10) 
       { 
        int pos = line.indexOf(':'); 
        if(pos != -1) 
        { 
         String name = line.substring(0, pos); 
         String value = line.substring(pos + 1, line.length()).trim(); 
         setHeader(name, value); 
        } else 
        { 
         setRequestType(line.substring(0, line.length()).trim()); 
        } 
        line = ""; 
       } 
      } 

      return true; 
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
      return false; 
     }     
    } 

    private byte[] readHTTPContent() 
     throws IOException 
    { 
     ByteArrayOutputStream baosContent = new ByteArrayOutputStream(); 
     int contentLength = 0; 
     try { 
      contentLength = Integer.parseInt((String) headers.get("content-length")); 
     } catch (Exception ex) { 
      contentLength = 1024 * 1024; 
     } 
     int bytesToRead = 0; 
     int bytesRead = 0; 
     int totalBytesRead = 0; 
     int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 

     if (contentLength < bufferSize) { 
      bytesToRead = contentLength; 
     } else { 
      bytesToRead = bufferSize; 
     } 
     do { 
      try { 
       bytesRead = in.read(buffer, 0, bytesToRead); 
      } catch (InterruptedIOException e) { 
       /* comms read timeout expired, no problem */ 
       System.out.println("Timeout reading from socket"); 
      } 
      if (bytesRead == -1) { 
       in.close(); 
       // throw new IOException("Connection was closed by client."); 
       break; 
      } else if (bytesRead > 0) { 
       ////////////////////////////////////// 
       baosContent.write(buffer, 0, bytesRead); 
       ////////////////////////////////////// 
       totalBytesRead += bytesRead; 
      } 
      // Left bytes to read 
      if (contentLength - totalBytesRead > bufferSize) { 
       bytesToRead = bufferSize; 
      } else { 
       bytesToRead = contentLength - totalBytesRead; 
      } 
     } while (totalBytesRead < contentLength); 

     return baosContent.toByteArray();   
    } 


    public void saveToFile(byte data[], String filename) 
    { 
     try{ 
      File f = new File(filename); 
      FileOutputStream fout = new FileOutputStream(f); 
      fout.write(data); 
      fout.close(); 
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     }   
     return; 
    } 


    private void setHeader(String key, String value) 
    { 
     headers.put(key.toLowerCase(), value); 
    } 

    private void setRequestType(String s) 
    { 
     requestType = new String(s); 
    } 

    private byte content[]; 
    private Hashtable headers; 
    private DataInputStream in; 
    private DataOutputStream out; 
    private String requestType; 
} 
संबंधित मुद्दे