jsp servlet 如何快速生成动态页面?求简单方法!
我知道两种方法:
1.在servlet中写入很多的html标签
2.在jsp页面中用很多的java代码
请问有没有更加方便的方法来实现动态页面的编写!?????????????? java jsp servlet html web
[解决办法]
用struts2之类的框架,你会发现你的1、2两种方法中的弊端都可以适当避免。
[解决办法]
建议使用mvc框架:struts2,spring-mvc。试图层用:velocity
[解决办法]
前台可以JS动态显示数据
[解决办法]
最快的方法就是jsp,所有代码都写在jsp中,前台,服务器端代码。这是最快也是最不好的一种方法。既然要快,就这样做吧。
[解决办法]
iframe试试
[解决办法]
package xxcw.common.tools;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class toHtml extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String url="";
String name="";
ServletContext sc = getServletContext();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//String file_name=request.getParameter("file_name");//你要访问的jsp文件,如index.jsp
//则你访问这个servlet时加参数.如http://localhost/toHtml file_name=index
//url = "/jsp/indexmodel.jsp";//这是你要生成HTML的jsp文件,如
url = request.getParameter("sourceurl");
//http://localhost/index.jsp的执行结果.
//name=request.getRealPath("")+"\\jsp\\index.html";//这是生成的html文件名,如index.htm.
name=request.getRealPath("")+request.getParameter("aimurl");//这是生成的html文件名,如index.htm.
RequestDispatcher rd = sc.getRequestDispatcher(url);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final ServletOutputStream stream = new ServletOutputStream()
{
public void write(byte[] data, int offset, int length)
{
os.write(data, offset, length);
}
public void write(int b) throws IOException
{
os.write(b);
}
};
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response)
{
public ServletOutputStream getOutputStream()
{
return stream;
}
public PrintWriter getWriter()
{
return pw;
}
};
rd.include(request, rep);
pw.flush();
FileOutputStream fos = new FileOutputStream(name); //把jsp输出的内容写到d:\test.htm
os.writeTo(fos);
fos.close();
response.setContentType("application/octet-stream");
response.setContentType("text/html; charset=GBK");
PrintWriter out=response.getWriter();
//out.println("首页生成完毕!");
String aim=request.getParameter("aim")==null?"":request.getParameter("aim");
if(aim.equals("")){
aim="self";
}
if(request.getParameter("turnjsp")!=null){
out.println("<script language='JavaScript'>");
out.println(request.getParameter("aim")+".location.href='"+request.getParameter("turnjsp").replace('
[解决办法]
', '&')+"'");
out.println("</script>");
}else
if(request.getParameter("functionname")!=null){
out.println("<script language='JavaScript'>");
out.println(request.getParameter("aim")+"."+request.getParameter("functionname")+"();");
out.println("</script>");
}else
if(request.getParameter("refurbishinfo")!=null){
out.println("<script language='JavaScript'>");
out.println("alert('刷新"+new String(request.getParameter("refurbishinfo").getBytes("ISO8859_1"), "GBK")+"成功!');");
out.println("</script>");
}
}
}
[解决办法]
我之前做的静态化原理是,get获得url页面数据,输出为html。
[解决办法]
ajax,前台都不用jsp,html就好了
[解决办法]