更改bg颜色jsp

时间:2018-11-02 02:51:19

标签: java jsp servlets background-color

Previously asked a question对此。运行代码时-表达式中出现错误:

org.apache.jasper.JasperException: An exception
occurred processing JSP page [/apps.jsp] at line [15]

12:         <input  type="button" name="red" value ="red" >
13: <%
14: String button1Click = request.getParameter("red");
15: if(button1Click.equals("red")){
16:     %>
17: <body style='background-color: red;'>
18: <%

我该如何解决?所有代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <form action = "apps" method="POST">
            <input  type="button" name="red" value ="red" >
    <%
    String button1Click = request.getParameter("red");
    if(button1Click.equals("red")){
        %>
    <body style='background-color: red;'>
    <%
    }

    %>

            <input  type="submit" name="blue" value ="blue" >
            <input  type="submit" name="green" value ="green" >
            <input  type="submit" name="yellow" value ="yellow" >
            <input  type="submit" name="reset" value ="reset" >
            </form>
        </form>
    </body>
    </html>

2 个答案:

答案 0 :(得分:1)

这可能是由于NullPointerException引起的。试试

if(button1Click != null && button1Click.equals("red"))

<body>标记后的</header>标记替换为整个Java代码。

答案 1 :(得分:0)

您正在jsp页面中使用Java代码。 Jave代码始终首先执行。这样您的颜色不会在单击按钮时改变