Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize website URL rewriting by Java UrlRewrite

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces Java UrlRewrite how to achieve website URL rewriting, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Now most websites and malls will use URL rewriting, access to this, but also because of the e-commerce mall that is being done. URL rewriting is to display the original URL with another rule, which makes it convenient for users to access and shield some information at the same time.

Here to talk about its benefits, in the development process, we often encounter some URL with a lot of parameters, on the one hand, it seems annoying, on the other hand, some information is directly displayed on the URL, there will be some security problems. Using URL rewriting, you can make URL with parameters be represented in a more regular way, such as:

/ demoAction?id=1 = > / demo1.html

It also conceals the parameters that should be displayed on the URL, hiding the technical implementation and sensitive information. In addition, URL rewriting is also conducive to the access of search engines.

Recently, the project has been exposed to URL rewriting using UrlRewrite, which mainly uses Filter technology to deal with the accessed URL when the user requests, to achieve the role of rewriting.

The following are examples of the use of UrlRewrite (in my opinion, the official document of UrlRewrite is relatively comprehensive and easy to understand)

Import of UrlRewrite:

The import of UrlRewrite is very simple. First, add the urlrewrite-3.2.0.jar package to the lib folder of the project, and then declare Filter in web.xml.

UrlRewriteFilter org.tuckey.web.filters.urlrewrite.UrlRewriteFilter UrlRewriteFilter / * REQUEST FORWARD

After declaring filter, you need to create a new urlrewrite.xml file in the WEB-INF directory

This file is the rule-making file of UrlRewrite, and it is mainly configured to rewrite URL.

At this point, the import of UrlRewrite is complete.

After the UrlRewrite import is successful, you can rewrite the URL mainly by adding rules to the urlrewrite.xml. Here are some common rules.

^ / demo/ (\ w +) .html$ / Struts/$1

Rule is a child node under urlrewrite and the main rule node of urlrewrite. It contains two child nodes: from and to. From represents the URL,to of the request and the real URL to which it will be transferred. There are two matching patterns for from,UrlRewrite, one is regular expression matching and the other is wildcard matching, such as regular expression matching. When matching is carried out, the regularity of the matching part can be extracted as parameters to pass.

As set by the rule above, when the URL accessed by the client is http://127.0.0.1:8080/Struts/demo/hello.html, it jumps to http://127.0.0.1:8080/Struts/hello because the matching part is hello. When there are multiple regularities in the URL rule, the matching parameters will increase accordingly. Such as:

^ / demo1/ (\ w +) / (\ w +) .html$ / Struts/$1.action?age=$2

The default way for rule to match is regular expressions, but sometimes it is possible to match in the form of wildcards. When writing rules, you only need to add an attribute of match-type= "wildcard" to rule.

/ demo2/*/* / Struts/$1.action?age=$2

With regard to to nodes, UrlRewrite provides a variety of ways to redirect URLs, such as forward and redirect, which, like the functions provided by most MVC frameworks, are not discussed here.

In addition to supporting the jump of specified rules, UrlRewrite also supports the execution of a function of an object when a rule is matched

^ / demo3/ (\ w +) / (\ w +) .html$ / Struts/$1.action?age=$2

As set above, to implement the matching rule is to execute a function, you need to add one more run node and the corresponding class and method attributes on the node. At the same time, the corresponding class must inherit the RewriteRule class, and the executed method must pass in two parameters, HttpServletRequest and HttpServletResponse

Public class Demo extends RewriteRule {public void log (HttpServletRequest request,HttpServletResponse response) {System.out.println ("1");} public void log2 (HttpServletRequest request,HttpServletResponse response) {System.out.println ("2");}}

In this way, when the URL entered by the client matches the specified rule * * times, UrlRewrite will execute the corresponding function. The function will only be executed when the * times match succeeds.

If you want to execute a function for each matching rule, you can add a class-rule child node to urlrewrite, and after this node is set, the specified function will be executed once after each matching rule.

In addition to processing the requested URL, UrlRewrite also provides the ability to rewrite the address in the returned page. Using rule is to deal with the url entered by the user, but in the development process, it is often necessary to add some URL requests to the page. UrlRewrite can also rewrite the URL in the page through the rules. Such as:

/ (\ w+) .action\? id= (\ w+) $/ $1.html

Add the rule to UrlRewrite

In this way, it can hide a lot of technologies in development and is more secure.

These are some common uses of UrlRewrite. With regard to UrlRewrite, some people on the Internet also say that this will affect performance, because each request needs to be filtered again, but this still depends on different opinions. After all, it is good for the URL to use URL rewriting.

On how Java UrlRewrite to achieve URL rewriting site to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report