如何使用jsp Include在应用程序(war)之外包含文件
我使用“jsp:include”在我的一个jsp文件中包含一个静态文件。当静态html文件位于应用程序文件夹内时,它可以正常工作。但是,如果将其保存在应用程序文件夹之外,则不会包含在JSP文件中 注意:我已经为保存静态文件的文件夹创建了一个上下文,我可以使用直接url查看html文件如何使用jsp Include在应用程序(war)之外包含文件,jsp,include,Jsp,Include,我使用“jsp:include”在我的一个jsp文件中包含一个静态文件。当静态html文件位于应用程序文件夹内时,它可以正常工作。但是,如果将其保存在应用程序文件夹之外,则不会包含在JSP文件中 注意:我已经为保存静态文件的文件夹创建了一个上下文,我可以使用直接url查看html文件 请帮助..您只能对web应用程序上下文中的资源使用jsp:include。您需要使用java.io.File或类似的方法从文件系统路径加载,或从类路径加载资源。只是出于兴趣-需要这样做的原因是什么可能有另一种方法
请帮助..您只能对web应用程序上下文中的资源使用
jsp:include
。您需要使用java.io.File
或类似的方法从文件系统路径加载,或从类路径加载资源。只是出于兴趣-需要这样做的原因是什么可能有另一种方法
我怀疑您想要一些独立于WAR文件的配置,并且对于WAR部署到的每个环境都是唯一的。我使用c:import标记解决了这个问题
为了定义动态URL,我使用了bean:define标记。感谢朋友们的建议和帮助。参见此使用
方法的附加好处是,您可以使用charEncoding
属性设置编码。
您不能使用
或
语句执行此操作。我使用的类具有从URL获取内容的方法:
例:
公共类URLReader
{
公共URL阅读器()
{
in=null;
out=null;
requestType=null;
headers=null;
内容=空;
headers=newhashtable();
}
公共void doGet(字符串服务器、字符串uri、int端口)
{
试一试{
套接字客户端=新套接字(服务器、端口);
client.setKeepAlive(true);
in=新的DataInputStream(client.getInputStream());
out=新的DataOutputStream(client.getOutputStream());
out.writeBytes(“GET”+uri+“HTTP/1.0\r\n”);
out.writeBytes(“主机:“+server+”\r\n”);
out.writeBytes(“接受: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(“接受语言:en-us\r\n”);
writeBytes(“接受编码:gzip,deflate\r\n”);
out.writeBytes(“用户代理:Mozilla/4.0(兼容;MSIE 6.0;Windows NT 5.0)\r\n”);
out.writeBytes(“连接:保持活动\r\n”);
out.writeBytes(“内容长度:0\r\n\r\n”);
out.flush();
parseRequest();
out.close();
in.close();
client.close();
}捕获(例外e){
System.out.println(e.getMessage());
}
返回;
}
公共字节[]getContent()
{
返回内容;
}
公共字符串getHeader(字符串名称)
{
字符串键=(字符串)头.get(名称);
返回键;
}
公共哈希表getHeaders()
{
返回标题;
}
公共字符串getRequestType()
{
返回请求类型;
}
公共静态void main(字符串args[])引发IOException
{
URLReader=新的URLReader();
reader.doGet(“link.inet.vn”,“/seo-website/inet.html”,80);
if(reader.getContent()!=null)
System.out.println(新字符串(reader.getContent(),“UTF-8”);
}
私有布尔parseRequest()
{
字节匹配[];
整数指数;
弦线;
匹配=(新字节[]{
13, 10, 13, 10
});
指数=0;
第“”行;
int i;
试一试{
而((i=in.read())>=0)
{
如果(i==match[index]&&index 2&&i==10)
{
int pos=line.indexOf(':');
如果(位置!=-1)
{
字符串名称=行。子字符串(0,位置);
字符串值=line.substring(位置+1,line.length()).trim();
setHeader(名称、值);
}否则
{
setRequestType(line.substring(0,line.length()).trim());
}
第“”行;
}
}
返回true;
}捕获(例外e){
System.out.println(e.getMessage());
返回false;
}
}
专用字节[]readHTTPContent()
抛出IOException
{
ByteArrayOutputStream内容=新建ByteArrayOutputStream();
int contentLength=0;
试一试{
contentLength=Integer.parseInt((字符串)headers.get(“内容长度”);
}捕获(例外情况除外){
contentLength=1024*1024;
}
int bytesToRead=0;
int字节读取=0;
int totalBytesRead=0;
int bufferSize=1024;
字节[]缓冲区=新字节[bufferSize];
if(contentLength0){
//////////////////////////////////////
写入(缓冲区,0,字节读取);
//////////////////////////////////////
totalBytesRead+=字节读取;
}
//要读取的左字节
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;
}