How does java determine whether it is PC access or mobile access?

  Recently, I received a request. Since the original login page was for PC, if the mobile terminal accessed it directly, the login box would not be vertically centered. Later, I thought about changing the login.html style on the PC side to be vertically centered, so that there would be no problem accessing it on both PC and mobile terminals. However, later customers were dissatisfied with this modification and requested that the original style of the PC version be retained and adapted to the mobile version. Modifications to the style can only be determined based on the client server the user accesses.

1. The request header uses HttpRequest request to receive parameters.

Debug view can be accessed via mobile terminal or PC terminal.

2. Back-end code implementation:
String userAgent = request.getHeader("user-agent");
String device = null;
//移动端访问
if (userAgent.indexOf("Android")!=-1 || userAgent.indexOf("iPhone") != -1 || userAgent.indexOf("iPad") != -1){
    
    
	device = "mobileTerminal";
	request.setAttribute("device", device);
}else {
    
    
	//PC端访问
	device = "pcTerminal";
	request.setAttribute("device", device);
}
3. The front end receives parameters
<script th:inline="javascript">
    $(document).ready(function (e) {
    
    
        var device = [[${
    
    device}]]
        if (device === 'mobileTerminal'){
    
    
            $('.login').attr('style','left: 50%;right: 160px;transform: translate(-50%, -50%);margin-top: 0');
        }
    });
</script>

Guess you like

Origin blog.csdn.net/qq_45278500/article/details/122602796