Difference between jsp expression tags <% and <%=
Asked Answered
T

2

21

I more or less know the difference between <%! and <%, but I can't seem to find the difference between <%= and <%. I'm trying to avoid a null value error by introducing some logic into my expression that currently uses <%= ... %>. I get an error unless I replace the tags with <%...%>. However after my build I get a jsp error instead of the servlet error. I can't really paste my original code in here but the code inside <%= ... %> essentially retrieves a nested array object (more like array object within another array object) passed as a servlet argument in a Struts 1 project. I just want to add a try...catch statement in case the object's property isn't instantiated yet.

<%=((package.package.package.ClassName)session.getAttribute("attrName")).getObjectList()[0].getSecondObject.length%>; 

Is this a jsp issue, or is it a Struts 1 issue? And again, what is the difference between the 2 tags?

Transported answered 29/4, 2013 at 17:54 Comment(0)
R
30

Between <%...%> you can write any logic that you want in Java.

Using <%=...%> will output the result of the expression between the brackets to the screen. So instead of writing for example

<% System.out.println("Hello World") %> 

you can simply write

<%= "Hello world" %> 

Basically, what <%= %> does is to call the toString() method of the expression that is being evaluated.

If you need to add null check logic as you said you need you can use

 <%..%>

Here are links you can refer to:

https://web.archive.org/web/20200711234346/http://www.easywayserver.com/jsp/JSP-example.htm

https://www.tutorialspoint.com/jsp/jsp_syntax.htm

Racialism answered 29/4, 2013 at 18:20 Comment(5)
Oh, ok. Yeah, I kept working at it. I think I fixed the code. I had to add another scriplet at the top of the document that did the validation for me as such: <% int number = 0; try{((package.package.package.ClassName)session.getAttribute("attrName")).getObjectList()[0].getSecondObject.length} catch(Exception e){ number= 1 } %> and then replace my original code with <%= number %>. I still wasn't very clear on the difference, but your answer makes sense. Thanks.Transported
I think you mean <% out.print("Hello World") %>Voguish
@KorayTugay I really don't understand the advantage of <% out.print("Hello World") %> over <%= "Hello World"%>Putupon
@AnkushGatfane - I don't think he is saying print is better. I think he is saying that the tag acts like "print", not "println".Contumely
but does it actually call toString()? If so, I cannot use a primative or null value...Cavender
V
1

<% %> ------> This tag we call as scriptlet tag in JSP. Actually, whatever you do in a .jsp file it will convert back to Servlet internally, Because in Servers only thing which runs internally are Servlets,You can write all your Html code inside the out.println() inside the Servlets, But as developers, it's easy for us to have separate sections for back-end and front-end , That's the main reason why we need JSP files. So If you need to do something relevant to service() method in Servlets, do that inside <% %> this tag. If you need to just need to get an output of something use <%= %>----> expression tag. If you need to see how JSP files internally converting back to servlets please use netbeans IDE(It has a separate tool to view it.).

  • Writing a code inside a servlet's service() method == <% %> [coding inside scriptlet tag]

  • Writing a code outside the service method but inside the Servlet class == <%= %>[coding inside expression tag ]

Vino answered 1/12, 2019 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.