Static JSP and pseudo-static JSP

Source: Internet
Author: User

First, let's talk about why it should be static.
For the current web application, dynamic pages have a very high position. It is because of the dynamic nature that the current web is rich and colorful, but just like all other facts, the benefits are often at a cost.
To produce dynamic results, each page request requires the server to compile or execute the page. These operations consume system resources. If there is still communication with the database during this period, the cost will be even greater.
If the content of a page does not change within a certain period of time, it is not necessary to compile or execute a new one for each access. We can save the result of this period of time that has not changed to a static page, and then each time we access this page, we will use the static page we just saved to reply. This greatly reduces the consumption of system resources and increases the response speed to customers. This process is called static page.

The problem is how to save the page content in JSP.
When accessing the JSP page, the server first compiles the JSP file into a Servlet File, then compiles and executes the servlet, and finally returns the result to the client. Our task is to save the final HTML static page and store it on the server.
The solution is actually very simple. Normally, for JSP access, the final data stream is written back to the client. If we redirect it, let it write back to a local file on the server, in this way, the execution result of JSP can be saved, and this file is the static result we want to get.

Now I have talked about how to implement it.
After reading the Servlet File compiled from JSP, you can know that this servlet writes data back to the client through a "jspwriter" type object, this object is the built-in "out" Object in JSP. Now we only need to define a class of our own, so that it can inherit the "jspwriter" class and overwrite the output functions, output data to a specified file.
For example (some overload functions are omitted ):

Program code

Import java. Io .*;
Import javax. servlet. jsp .*;
Public class constpagewriter extends jspwriter {
Private printwriter PW;
Public constpagewriter (string filename ){
Super (jspwriter. default_buffer, false );
Try {
PW = new printwriter (filename, "UTF-8 ");
} Catch (exception e ){
E. printstacktrace ();
}
}
@ Override
Public void print (string arg0) throws ioexception {
PW. Print (arg0 );
}
......
@ Override
Public void println () throws ioexception {
PW. println ();
}
@ Override
Public void println (Object arg0) throws ioexception {
PW. println (arg0 );
}
......
@ Override
Public void write (char [] arg0, int arg1, int arg2) throws ioexception {
PW. Write (arg0, arg1, arg2 );
}
}

Then, write the page to be static as follows:

Program code

<%
String filename = "constpagename"; // generated static page file name
If (notneedupdate) {// determine whether to update
Response. sendredirect (filename );
Return;
}
Out = new constpagewriter (pagecontext. getservletcontext (). getrealpath (filename ));
%>
<%
// Build this page to update the page
%>
<%
Out. Close ();
Response. sendredirect (filename );
%>

In this way, the static page is basically completed, and the remaining operations are the processing time to be updated. As to when the update is related to a specific application, we will not detail it here.

JSP uses urlrewirte to implement pseudo-static URL address

I found a lot of Use Cases about urlrewirte on the Internet, mostly about ASP, and few JSP-related documents. Most of them are vague and I am not clear after reading it, in addition, it cannot be used successfully in the project. In view of this, I sorted out the usage of urlrewirte. this method only achieves pseudo-static URL, not real static URL.

Working environment: eclipse3.2.2 + myeclipse_5.5.1 + tomcat5.0.28

Urlrewirte version: urlrewrite-2.6.0.jar

Prerequisites: there is a normal Web application project.

Use of urlrewirte:

1. Meet Search Engine Requirements

2. Hiding Technology to Improve the portability of websites

3. satisfying aesthetic requirements (I personally think this is too far-fetched)

Project deployment

1. First load urlrewirtefilter at http://tuckey.org/urlrewirte/

2. Unzip the downloaded file, copy the urlrewrite-2.6.0.jar to the project's webroot/WEB-INF/lib/directory, and then compile

3. Copy urlrewrite. XML to the project's webroot/WEB-INF/directory.

4. Add the following to the Web. xml file:

<! -- Static dynamic address -->
<Filter>
<Filter-Name> urlrewritefilter </filter-Name>
<Filter-class> org. Tuckey. Web. Filters. urlrewrite. urlrewritefilter </filter-class>
<Init-param>
<Param-Name> loglevel </param-Name>
<Param-value> warn </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> urlrewritefilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

5. You need to configure the urlrewrite. xml file to achieve URL static. The following describes in detail

The related configuration has been completed. The following describes how to make dynamic addresses static.

1. Static normal URLs

For example, you need to convert http: // localhost/prjtest/user/list. jsp to http: // localhost/prjtest/user/list.html.

This is the simplest method. When a servlet jumps to the list. jsp page to list the user list, it is configured in urlrewrite. XML as follows:

<Rule>
<From> ^/user/list.html </from>
<To>/user/list. jsp </to>
</Rule>

When you request the/user/list.html page, it is actually equivalent to the request/user/list. jsp page.

In the servlet jump, write as follows:

Response. sendredirect ("./user/list.html ");

2. Static URL with Parameters

For example, http: // localhost/prjtest/user/view. jsp? Cid = 1 & cname = admin to switch

Http: // localhost/prjtest/user/View/paiadmin.html

In urlrewrite. XML, configure as follows:

<Rule>
<From> ^/user/View/(%0-9%%%%%_(%a-z%%%%%.html $ </from>
<To>/user/view. jsp? Cid = $1 & amp; cname = $2 </to>
</Rule>

When the page/user/View/1_admin.html is requested, it is actually equivalent to the request/user/list. jsp? Cid = 1 & cname = admin page

In the servlet jump, write (CID, cname is a variable) as follows ):

Response. sendredirect ("./user/View/" + CID + "_" + cname + ". html ");

Note: Replace "& amp;" with "&" in the configuration file "&"

Related Article

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.