Configure Servlet to map to a JSP page through url-pattern

JSP pages are directly used as servlets through url-pattern mapping.

Preface

A few days ago, some data needs to be extracted from the business database and returned to the third-party system in Json format. At first, I wanted to use a simple combination of Tomcat+JSP, but after another thought, I still use JSP now, which may be a bit low, and it's not very attractive to open to the outside world.

Although, I think that the strength of programming ability is the understanding and optimization of algorithms, and the precise planning and reasonable layout of business segmentation. However, many young people think that the strength of programming ability depends on whether the technology you use is better than that, whether it is the latest, and whether it is advanced. It is normal to have this kind of thinking. In addition to the desire for technology, it is also caused by the current market employment factors.

I have seen too many projects because of the use of the Spring ORM framework and subsequent development inefficiencies and difficulties in iterative business modification, as well as memory leaks caused by some newcomer codes. Small projects generally don't need this stuff.

How to deal with it? Recall that there are many frameworks for URL rewriting and file suffix switching in PHP, but Oracle is to be accessed here, and PHP's support for the oracle driver access interface is not stable and not easy to use. It is better to use JAVA. The key here is: as long as the jsp suffix can be removed.

After some searching, I found the urlrewritefilter library. With this library, when the client browser visits xxx.do?xxx, it will be redirected to xxx.jsp?xxx.

Then, all the business is written in JSP, and then the code is updated on the server, restarting is also saved, the development efficiency is high, the modification is easy, the startup is fast, and the CPU and memory resource usage is extremely low.

 

1. Create a new Tomcat project. Open web.xml and add the following configuration

	<filter>
		<filter-name>UrlRewriteFilter</filter-name>
		<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>UrlRewriteFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</filter-mapping>


	<error-page> 
		<error-code>404</error-code>  
		<location>/error.html</location>  
	</error-page> 

2. Go to the official website http://tuckey.org/ to download urlrewritefilter, or Maven

<dependency>
    <groupId>org.tuckey</groupId>
    <artifactId>urlrewritefilter</artifactId>
    <version>4.0.3</version>
</dependency>

Three, the newly created urlrewrite.xml is placed under WEB-INF, the content is as follows

<urlrewrite>
	<rule>
		<from>/(\w*).do(\w*)</from>
		<to type="forward">/$1.jsp$2</to>
		
		<!-- 其实就是 htaccess 的语法,后面有提到  -->
	</rule>
</urlrewrite>

 

Fourth, create a new test.jsp content as follows

<%@ page trimDirectiveWhitespaces="true" %>
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

<%
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	
	out.print("hello:" + request.getParameter("p"));
%>

5. Start the server, open the webpage http://localhost:8080/test.do?p=good, you can see the effect.

Visit http://localhost:8080/rewrite-status (or http://localhost:8080/application/rewrite-status) and you can also see related rewrite information

 

6. Under curiosity, check the source code of urlrewritefilter.jar to see what is written.

Find

* UrlRewriteFilter found two final variables
   public static final String DEFAULT_WEB_CONF_PATH = "/WEB-INF/urlrewrite.xml";
   public static final String DEFAULT_MOD_REWRITE_STYLE_CONF_PATH = "/WEB-INF/.htaccess";
   
   indicating that this library can support .htaccess files
   
* reloadConf The function intermittently determines whether the config file is updated.

  Going further in is File.lastModified


* There are multiple files under the org.tuckey.web.filters.urlrewrite.dtds package, which can be quoted directly to prevent being walled.
   
* UrlRewriteWrappedResponse.urlRerwiter.processEncodeURL converts the URL requested by the user into outputUrl according to the rules

  The core logic and a considerable part of the code in the package are spent on this function and its subsequent interpretation. If the htaccess (all) syntax is not used, there may be a lot less code.
  
  This package provides a source reference for Java interpreting htaccess.

* Use the getNewChain function to be responsible for generating a new chain, and finally execute chain.doFilter and hand it over to Tomcat for subsequent operations.

 

Guess you like

Origin blog.csdn.net/RoadToTheExpert/article/details/115306081