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

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