• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Mock exam question][JSP script]

 
Sam Sun
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the result of an attemp to access this jsp page?
<% x = 10; %>
<% int x=5; %>
<%! int x=5; %>
x= <%=x%>
x= <%=page.x%>

select one
1. code doesn't compile because x is used before declaration
2. prints x=5 followed by x=10
3. prints x=10 followed by x=5
4. prints x=10 followed by x=0
5. none of above

Answer is 5, not 1.
[ June 30, 2005: Message edited by: Bruce Sun ]
 
srinath anand
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file will not compile because of page.x. However if you take that out x should print 5.

A declaration in JSP is global and is available to the whole class. However anything inside the scriptlet code is inside the _jspService method. So when u say x= <%=x%> it prints the value of x inside the _jspService method which is 5.
 
Thejaswi Narayana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x is being used in line 1 before it is declared in line 2. Hence option 1 would be the right choice.
 
Sam Sun
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I tried the code, I got the answer 5 which is different with what I thought answer 1.

So I think, when the code is executed, <%! %> declaratioon will be executed first no matter where they are and then does the rest of the code.
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class {

int x=5;

public ---- service(http------){

int x=5;
x=10;
}

}
So I think, when the code is executed, <%! %> declaratioon will be executed first no matter where they are and then does the rest of the code


You are right.The above is the dummy code that the generated servlet will have. The place where the error will occur is in bold. The declaration will be executed first and hence 5th option is correct.

Hope this helps.
 
Sanjivi Syamsundar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the bold line an error. It is just like declaring a local variable x. The other x is an instance variable. So the answer is x=5 and x=5 which is not one of the choices and hence the error.

Correct me if there is anything wrong with my conclusion.
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right. My mistake. This will the code.

public class {

int x=5;

public ---- service(http------){
x=10;
int x=5;

}

}
 
S.L.Narayanan
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer given in the mock exam is correct since u will get a compilation error because there is no variable named as 'x' present in page implicit object and not because of u initialized before declaring it.
 
Radmika Arunachalam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% x = 10; %> ----> set instance variable x = 10
<% int x=5; %> ---> declare local variable x
<%! int x=5; %> ---> declare instance variable x
x= <%=x%> ----> print local variable x value (5)
x= <%=page.x%> ----> Error because "page" is on object if changed to <%= this.x %> then it will print 10..

Hope i am not confusing.. Please let me know if i am wrong..
 
sergio mendez-rueda
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bruce Sun:
[QB]What's the result of an attemp to access this jsp page?
<% x = 10; %>
<% int x=5; %>
<%! int x=5; %>
x= <%=x%>
x= <%=page.x%>

select one
1. code doesn't compile because x is used before declaration
2. prints x=5 followed by x=10
3. prints x=10 followed by x=5
4. prints x=10 followed by x=0
5. none of above

Answer is 5
why:
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

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

An error occurred at line: 13 in the jsp file: /usingTag.jsp
Generated servlet error:
C:\Programme\Tomcat5.0\work\Catalina\localhost\webTres\org\apache\jsp\usingTag_jsp.java:103: cannot resolve symbol
symbol : variable x
location: class java.lang.Object
out.print(page.x);
^
1 error



org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic