解決java中的高併發問題能夠從硬件軟件等方面入手,硬件如:服務器;軟件如:系統緩存、頁面靜態化等。這裏我寫的是頁面靜態化的簡單小例子。之因此將這個小例子記錄下來是由於以前對頁面靜態化有誤解,原覺得靜態化頁面就是在項目編寫中前端頁面利用html就能夠。可是在參考網上資料後發現理解偏差很大,如下是我在參考了一些網上資料後寫的實現頁面靜態化的簡單實例:css
項目結構圖html
![](http://static.javashuo.com/static/loading.gif)
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;
-
- public class CreateStaticHTMLPage {
-
- 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");
- RequestDispatcher rd = servletContext.getRequestDispatcher(jspPath);
- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final ServletOutputStream servletOuputStream = new ServletOutputStream(){
- 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){
- public ServletOutputStream getOutputStream(){
- return servletOuputStream;
- }
- public PrintWriter getWriter(){
- return printWriter;
- }
- };
- rd.include(request, httpServletResponse);
- printWriter.flush();
- FileOutputStream fileOutputStream = new FileOutputStream(fileFullPath);
- byteArrayOutputStream.writeTo(fileOutputStream);
- fileOutputStream.close();
- response.sendRedirect(fileName);
- }
-
- }
3.web.xml配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
- <servlet>
- <servlet-name>htmlServlet</servlet-name>
- <servlet-class>com.servlet.Servlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>htmlServlet</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
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+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>首頁面</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
-
- </head>
-
- <body>
- <form action="kanName.do">
- <input name="chapterId" type="text" value="122"/>
- <input name="userName" type="text" value="張慶偉"/>
- <input type="submit" value="提交"/>
- </form>
- </body>
- </html>
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+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <%
- String lastPageId=(String)request.getAttribute("lastPageId");
- %>
- <base href="<%=basePath%>">
-
- <title>book頁</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
-
- </head>
-
- <body>
- 這是跳轉頁面:
- <%=lastPageId %>
- </body>
- </html>
以上代碼是實例的全部代碼,啓動項目後點擊提交按鈕後,會看到跳轉到一個html頁面。java
以上代碼參考自:http://blog.csdn.net/jimmy609/article/details/37810591web