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 receive request address in Web Application Development

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of "how to receive the request address in the Web application development". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to receive the request address in the Web application development" can help you solve the problem.

In the development of Web applications, it is often necessary to obtain the requested ServerName, request port, etc., so as to construct an absolute URL, get the user's request address, and then perform operations such as jump, redirect and so on.

Maybe you say it's not easy. There are four ways to write a reply. I have a variety of ways to get this request address.

Req.getServerName ()

Req.getRemoteHost ()

Req.getHeader ("host")

Req.getRequestURI ()

Req.getRequestURL () .toString ()

We usually use the above methods to encapsulate an absolute address through a request. The difference between http/https can be obtained through getSchema.

Does the address you get here must be the URL entered by the actual user?

The answer is uncertain.

If you are using a separate Web Container, for example, static resources and Servlet are handled by Tomcat, in this standalone mode, the acquired ServerName is directly processed by request, the direct request is passed, and the port is the port number that Connector is listening to. At this time, these request methods can get the content we want.

But if we put Tomcat after a proxy Server, at this time, all user requests come from the proxy server first, and then when the above method is called in the Java Web application, your request serverName, port number, and so on, essentially want to get the address and port requested by the real user, not the one that Connector listens on. And depending on the configuration of proxy server, you may not get the expected result.

Why is that?

For requests passed from the proxy server, take Nginx as an example. In the default proxy header, $proxy_host is used for Host. In this case, what we get is not the Host domain that is actually passed in the request header of Http, but the host that we set when we configure the proxy_pass directive. In fact, users usually request through the domain name, and the two will be inconsistent.

For example, proxy_pass is configured in the form of http://localhost:port, at this time in

When you request.getServerName these methods, you return localhost, and then getHeader takes host, and you won't get the correct result.

In order to get the correct request address after being passed by the proxy server in this non-Standalone mode, some configuration is needed. Examples include ProxyParseHost on in Apache and proxy_set_header Host $http_host in Nginx.

In this way, when it is actually passed to the Java Web container, it is the Host in the actual user request header, and then assembling the absolute URL meets our expectations.

In addition to setting the settings for the proxy server, we can also do some configuration here in Tomcat.

In the Connector component, there are two properties:

ProxyName

ProxyPort

In the document, it is explained as follows:

If Connector is used in an agent scenario

Configure this property to specify the ServerName returned when the request.getServerName () method is called and the ServerPort returned when the request.getServerPort () method is called.

In this way, even if the value from the proxy server changes, we configure the value in Connector according to the specific convention, so that we can assemble the absolute URL when we get it.

In some other programming languages, some distinguish between ServerName and Host, such as PHP, where the two get different information.

$_ SERVER ['SERVER_NAME']

$_ SERVER ['HTTP_HOST']

Therefore, when constructing an absolute URL, you also need to distinguish between deployment environments, scenarios, and so on.

This is the end of the content about "how to receive request addresses in Web application development". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Internet Technology

Wechat

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

12
Report