Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

java - Comparing strings in JSP gives really weird error not specyfying what can e the cause

I am comparing the two string in the IF block and both of the strings are not null but a really weird error is being shown. Even though this same code is working with using simply servlets. But when I run this code in JSP it gives the error. I have already checked if the values were null but the output is exactly what was input in the form. My code is this

    <%
        String sender = (String) request.getAttribute("sender");
        System.out.println(request.getParameter("data") + "    " + request.getParameter("sender"));
        String process = "process";
        if (sender.equals(process)) {

            String itemName = request.getParameter("data");
    %>
            <%="No Item with name: " + itemName + " Found"%>
    <%
        }
    %>

This is the error that is being shown.

Some Error: An exception occurred processing [/error.jsp] at line [31] 28: String sender = (String) request.getAttribute("sender"); 29: System.out.println(request.getParameter("data") + " " + request.getParameter("sender")); 30: String process = "process"; 31: if (sender.equals(process)) { 32: 33: String itemName = request.getParameter("data"); 34: %> Stacktrace: org.apache.jasper.JasperException: An exception occurred processing [/error.jsp] at line [31] 28: String sender = (String) request.getAttribute("sender"); 29: System.out.println(request.getParameter("data") + " " + request.getParameter("sender")); 30: String process = "process"; 31: if (sender.equals(process)) { 32: 33: String itemName = request.getParameter("data"); 34: %> Stacktrace: [Ljava.lang.StackTraceElement;@6fb251a5

Folllowing is my complete page code

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html class="no-js" lang="">
<head>
    <meta charset="utf-8">
    <title>No Data</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/main.css">

    <meta name="theme-color" content="#fafafa">
</head>
<body>
   <section id="google-form" class="section google-form">
      <div class="account_create-text center  ">
         <h4>Oops No data found. </h4>
      </div>
      <br>

      <span class="username_curr-email blue ce block spacer-10">!
      <%
           String sender = (String) request.getAttribute("sender");
           System.out.println(request.getParameter("data") + "    " + request.getParameter("sender"));
           String process = "process";
           if (sender.equals(process)) {

               String itemName = request.getParameter("data");
       %>
               <%="No Item with name: " + itemName + " Found"%>
       <%
           }
       %>
       </span>
       <span class="spacer-10 block"></span>
       <span class="spacer-10 block"></span>
       <span class="spacer-10 block"></span>
      <br>
      <br>
      <br>
      <div class="bb-30 spacer-10">
          <a href="Task1.html" style="text-decoration: none;" class="button-elevated ">
              Go back
          </a>

      </div>
     <br>
     <br>


 </section>
 </body>

 </html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Found the answer. Use wrong method.

request.getParameter()

should have been used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...