如何处理java高并发问题---之页面静态化

解决java中的高并发问题可以从硬件软件等方面入手,硬件如:服务器;软件如:系统缓存、页面静态化等。这里我写的是页面静态化的简单小例子。之所以将这个小例子记录下来是因为之前对页面静态化有误解,原以为静态化页面就是在项目编写中前端页面利用html就可以。但是在参考网上资料后发现理解误差很大,以下是我在参考了一些网上资料后写的实现页面静态化的简单实例:

项目结构图

如何处理java高并发问题---之页面静态化_第1张图片


1.业务处理类Servlet.java(这里为了方便采用的servlet,可根据项目需求在其他的架构中使用其中的业务逻辑)

package com.servlet;

import java.io.File;
import java.io.IOException;

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

public class Servlet extends HttpServlet{
	
	public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		System.out.println("chapterId=====//===="+request.getParameter("chapterId"));
		if(request.getParameter("chapterId")!= null){
	        String chapterFileName = "bookChapterRead_"+request.getParameter("chapterId")+".html";  
	        String chapterFilePath = getServletContext().getRealPath("/") + chapterFileName; 
	        System.out.println("chapterFilePath===//============="+chapterFilePath);
	        File chapterFile = new File(chapterFilePath);
	        System.out.println("chapterFile===//============="+chapterFile);
	        if(chapterFile.exists()){//如果有这个文件就告诉浏览器转向
	        	System.out.println("有此静态页面======");
	        	response.sendRedirect(chapterFileName);
	        	return;
	          }
	        System.out.println("没有此静态页面======");
	        //======存放业务处理后得到的信息
	        request.setAttribute("novelChapter", "测试单节信息");
	        request.setAttribute("lastPageId", "lastPageId111");
	        request.setAttribute("nextPageId", "nextPageId222");
	        //============
	        System.out.println("getServletContext()==========//========="+getServletContext());
	        new CreateStaticHTMLPage().createStaticHTMLPage(request, response, getServletContext(),
	                chapterFileName, chapterFilePath, "/bookRead.jsp");
	    }
		
	}

}

2.创建静态页面类CreateStaticHTMLPage.java

package com.servlet;

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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/** 
* 创建HTML静态页面 
* 功能:创建HTML静态页面 
*/  
public class CreateStaticHTMLPage {
	 /**
     * 生成静态HTML页面的方法 
     * @param request 请求对象
     * @param response 响应对象
     * @param servletContext Servlet上下文
     * @param fileName 文件名称
     * @param fileFullPath 文件完整路径
     * @param jspPath 需要生成静态文件的JSP路径(相对即可)
     * @throws IOException
     * @throws ServletException
     */
	public void createStaticHTMLPage(HttpServletRequest request, HttpServletResponse response,ServletContext servletContext,String fileName,String fileFullPath,String jspPath) throws ServletException, IOException{  
        response.setContentType("text/html;charset=utf-8");//设置HTML结果流编码(即HTML文件编码)   
        RequestDispatcher rd = servletContext.getRequestDispatcher(jspPath);//得到JSP资源
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//用于从ServletOutputStream中接收资源   
        final ServletOutputStream servletOuputStream = new ServletOutputStream(){//用于从HttpServletResponse中接收资源   
            public void write(byte[] b, int off,int len){
                byteArrayOutputStream.write(b, off, len);  
            }  
            public void write(int b){
                byteArrayOutputStream.write(b);
            }  
        };  
        final PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream));//把转换字节流转换成字符流   
        HttpServletResponse httpServletResponse = new HttpServletResponseWrapper(response){//用于从response获取结果流资源(重写了两个方法)   
            public ServletOutputStream getOutputStream(){
                return servletOuputStream;  
            }
            public PrintWriter getWriter(){
                return printWriter;  
            }
        };  
        rd.include(request, httpServletResponse);//发送结果流   
        printWriter.flush();//刷新缓冲区,把缓冲区的数据输出   
        FileOutputStream fileOutputStream = new FileOutputStream(fileFullPath);  
        byteArrayOutputStream.writeTo(fileOutputStream);//把byteArrayOuputStream中的资源全部写入到fileOuputStream中   
        fileOutputStream.close();//关闭输出流,并释放相关资源   
        response.sendRedirect(fileName);//发送指定文件流到客户端   
    }

}

3.web.xml配置文件



	
	
	  htmlServlet
	  com.servlet.Servlet
	
	
	  htmlServlet
	  *.do
	

  
    index.jsp
  


4.首页面index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    
    
    首页面
	
	
	    
	
	
	
  
  
  
   

5.跳转后的结果页面bookRead.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
  <%
    String lastPageId=(String)request.getAttribute("lastPageId");
  %>
    
    
    book页
	
	
	    
	
	
	
  
  
  
  这是跳转页面:
   <%=lastPageId %>
  



以上代码是实例的所有代码,启动项目后点击提交按钮后,会看到跳转到一个html页面。


以上代码参考自:http://blog.csdn.net/jimmy609/article/details/37810591


你可能感兴趣的:(java,jsp,页面静态化,html静态化)