首页 > 解决方案 > 页面没有在jsp中重定向

问题描述

我正在处理一个 jsp 页面,我想根据以下条件重定向到页面。即使我在文本字段中键入 Google,也只有 else 部分在工作。PS 我将它托管在 localhost Glassfish 服务器上

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
<%
    String x = request.getParameter("browse");
    if (x == "Google" || x == "google") {
        response.sendRedirect("http://www.google.com");
    } else if (x == "youtube" || x == "Youtube" || x == "you tube" || x == "Music") {
        response.sendRedirect("http://www.youtube.com");
    }else {
        response.sendRedirect("http://localhost:11146/Project_Julie/" + x + ".jsp");
    }
%>
</body>
</html>

标签: javajspservlets

解决方案


使用equals()方法而不是==运算符,您的代码将起作用。

替换if(x=="Google" || x=="google")if("google".equals(x.toLowerCase()))在 else if 子句中应用相同的更改。


推荐阅读