您的位置:首页 > 编程语言 > Java开发

jsp 页面静态化 java.net.MalformedURLException: no protocol: index.jsp和java.net.MalformedURLException: unknown protocol: d

2010-04-18 15:13 751 查看
一般情况下,应用程序首页(default.jsp), 要显示来自于数据库的大量数据,每次用户来访问这个页面的时候,会花掉大量的时间, 所以我们可能用一个servlet,每次服务器启动的时候,读出default.jsp页面中的内容,生成一个静态的页面index.html ,将首页设制为index.html,这样就减少了用户访问首页的时间.

package com.newer.servlet;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

public LoginServlet() {
super();
}
public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

public void init() throws ServletException {
System.out.println("页面静态化");
String path = this.getServletContext().getRealPath("/");
ThreadStatic ts = new ThreadStatic(path);
ts.start();
}

}
/**
* 该类用于静态化页面
* @author Administrator
*
*/
class ThreadStatic extends Thread{
private String path;
public ThreadStatic(){
}
public ThreadStatic(String path){
this.path = path;
}
@Override
public void run() {
try {
Thread.sleep(10000);//睡眠10秒
URL url = new URL("http://localhost:8088/prj/index.jsp");
URLConnection conn = url.openConnection();//打开连接
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));//转换字符流

FileWriter fw = new FileWriter(path + "default.html");
PrintWriter out = new PrintWriter(fw,true);
String line = "";
while((line = br.readLine()) != null){
out.println(line);
}
System.out.println("静态化完毕...");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


对于页面静态化时出现的错误

java.net.MalformedURLException: unknown protocol: d
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.newer.servlet.ThreadStatic.run(LoginServlet.java:80)

这个可能就是URL url = new URL("http://localhost:8088/prj/index.jsp");地址写得不对

java.net.MalformedURLException: no protocol: index.jsp
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.newer.servlet.ThreadStatic.run(LoginServlet.java:80)

针对这个错误,可能URL的地址找不到,因为你的servlet还没实列化完成

当然我这里采用里多线程来处理的,Thread.sleep(10000);线程睡眠10秒,等待servlet实列化完成

这样就不会出现,找不到对应的要静态化的页面了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐