Difference between jsp expression tags <% and <%=

前端 未结 2 1119
我寻月下人不归
我寻月下人不归 2021-02-04 08:47

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 intr

相关标签:
2条回答
  • 2021-02-04 09:07

    <% %> ------> 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 ]

    0 讨论(0)
  • 2021-02-04 09:23

    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's a link you can refer to:

    http://www.easywayserver.com/jsp/JSP-example.htm http://www.tutorialspoint.com/jsp/jsp_syntax.htm

    0 讨论(0)
提交回复
热议问题