0

I am trying to call jsp code in javascript.

The error is as follow

org.apache.jasper.JasperException: Unable to compile class for JSP:

     An error occurred at line: 16 in the jsp file: /testng_index.jsp
     String cannot be resolved
     13:            var mai=document.getElementById("j");
     14:            //mai.value = mai.value.toUpperCase();
     15:            var m=mai.value;
     16:            <%=String value=document.writeln(m)%>
     17:            var mo= <%=  new  PlaneBo().getOwnerId(value)%>;
     18: //             document.writeln(mo);
     19:            if(mo==0)

here is the code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.ams.services.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" language="javascript">
    function emailCheck()
    { 
        var mai=document.getElementById("j");
        //mai.value = mai.value.toUpperCase();
        var m=mai.value;
        <%=String value=document.writeln(m)%>
        var mo= <%=  new  PlaneBo().getOwnerId(value)%>;
//          document.writeln(mo);
        if(mo==0)
            {
//                  document.writeln(m);
                 var tab = document.getElementById("t");
                var row = tab.insertRow(3);
                var cell1=row.insertCell(0);
                var cell2=row.insertCell(1);
                var inpt= document.createElement("input");        
                inpt.setAttribute("name","jho");
                inpt.setAttribute("type","text");

                cell1.innerHTML="Name";

Please provide me suitable solution of this problem.

1 Answers1

0

It looks to me as if you have some confusion about when and where Java and JavaScript code executes.

The Java code and JSP runs on the server when the browser requests the page. The server knows nothing about HTML and JavaScript. As far as the server is concerned, this is your JSP page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="com.ams.services.*" %>
TEXT 
        <%=String value=document.writeln(m)%>
TEXT <%=  new  PlaneBo().getOwnerId(value)%>
TEXT 

The server doesn't really care what's in the sections marked TEXT; it just sends them straight to the browser as they are. But it does care about what's in the <%@ ... %> and <%= ... %> tags.

You get an error because the server doesn't understand document.writeln(m). It's not aware of any object named document, so it can't evaluate document.writeln(m). It happens that in JavaScript there is an object document and a function document.writeln, but that's irrelevant. The server doesn't know anything about JavaScript.

The JavaScript code doesn't execute until the page has finished being sent to the browser. You haven't shown when the function is called: it may be after an input field's value has been changed or a button has been clicked. The JavaScript runs in the browser, not on the server, so it can't directly call the Java code on the server.

If you really want to execute some Java code on the server during a call to your JavaScript function, you will need to use an AJAX call. See this question for more information on how to do this.

Community
  • 1
  • 1
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104