Java Web----page static

Source: Internet
Author: User

1 description

The static page itself is much faster than the dynamic page, and the dynamic page is always going to the database query, which will lower the speed!

Page static is to save the dynamic page generated HTML to the server file, and then the same request, no longer to execute the dynamic page, but directly to the user to respond to the last generated static page. and static pages also help with search engines to find you!

2 View Book categories

Let's start by writing a small example to see the different categories of books. Then we will think about how to make dynamic page static problem.

index.jsp

  <body><a href= "<c:url value= '/bookservlet '/>" > All Books </a><br/><a href= "<c:url Value= '/bookservlet?category=1 '/> ' >javase category </a><br/><a href= ' <c:url value= '/BookServlet? category=2 '/> ' >javaee category </a><br/><a href= "<c:url value= '/bookservlet?category=3 '/>" >java Framework Classification </a><br/>  </body>

public class Bookservlet extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response ) throws Servletexception, IOException {bookservice bookservice = new Bookservice (); List<book> Booklist = null; String param = request.getparameter ("category"), if (param = = NULL | | param.isempty ()) {Booklist = Bookservice.findall ();} else {int category = Integer.parseint (param); booklist = bookservice.findbycategory (category);} Request.setattribute ("Booklist", Booklist), Request.getrequestdispatcher ("/show.jsp"). Forward (request, response);}}

show.jsp

<table border= "1" align= "center" width= "50%" ><tr><th> book name </th><th> book price </th> <th> category </th></tr>  <c:foreach items= "${booklist}" var= "book" ><tr><td>${ Book.bname}</td><td>${book.price}</td><td><c:choose><c:when test= "${ Book.category eq 1} "><p style=" color:red; >javase category </p></c:when><c:when test= "${book.category eq 2}" ><p style= "Color:blue;" >javaee category </p></c:when><c:when test= "${book.category eq 3}" ><p style= "Color:green;" >java Framework Classification </p></c:when></c:choose></td></tr>  </c:foreach></table >

3 Analysis

The first time a user accesses a page, a static page is generated, and then the request is redirected to a static page. When the user accesses again, redirect directly to the static page.

We need to generate static pages for different requests, such as when a user accesses Bookservlet?category=1, we want to generate a static page, and when the user accesses bookservlet?category=2, a static page is also generated. That is, different parameters generate different static pages!

We can use category as key, the path of the static page is value, save it to a map, and then save the map to ServletContext. When there is no corresponding static page, we generate a static page, then redirect to a static page, if there is a static page, then redirect directly.



public class Staticresponse extends Httpservletresponsewrapper {private PrintWriter pw;public staticresponse ( HttpServletResponse response, String filepath) throws FileNotFoundException, unsupportedencodingexception {super ( Response);p w = new PrintWriter (filepath, "UTF-8");} Public PrintWriter getwriter () throws IOException {return pw;} public void Close () throws IOException {Pw.close ();}}

public class Staticfilter implements Filter {private ServletContext sc;public void Destroy () {}public void DoFilter (Servle Trequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {httpservletrequest Req = (httpservletrequest) request; HttpServletResponse res = (httpservletresponse) response; String key = "Key_" + request.getparameter ("category"); map<string,string> map = (map<string, string>) sc.getattribute ("pages"); if (map = = null) {map = new hashmap&lt ; String,string> (); Sc.setattribute ("pages", map); if (Map.containskey (key)) {Res.sendredirect (Req.getcontextpath () + "/staticpages/" + map.get (key)); return;} String HTML = key + ". html"; String Realpath = Sc.getrealpath ("/staticpages/" + html); staticresponse sr = new Staticresponse (res, realpath); Chain.dofilter (Request, SR); Sr.close (); Res.sendredirect ( Req.getcontextpath () + "/staticpages/" + html); Map.put (key, HTML);} public void init (Filterconfig fconfig) throws Servletexception {This.sc = Fconfig.getservletcontext ();}} 


Java Web----page static

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.