How to print variables in JSP?


Posted On: Mar 22, 2020


    1 Answer Written

  •  devquora
    Answered by devquora

    In JSP (JavaServer Pages), you can print variables or values using a combination of Java code and JSP expression tags. Here's how you can print variables in JSP: Use JSP expression tags: JSP expression tags <%= %> allow you to directly output the value of a variable or expression within your JSP page. Here's an example:

    <% String message = "Hello, World!"; %>
    Message: <%= message %>

    In this example, the variable message is assigned the value "Hello, World!" using Java code. Then, within the HTML section of the JSP page, the value of the message is printed using the JSP expression tag <%= %>. The output will be

    Message: Hello, World!

    Use JSP scriptlet tags: JSP scriptlet tags <% %> allow you to include Java code within your JSP page. You can use scriptlet tags to declare variables, perform calculations, or retrieve data from external sources. Here's an example of printing a variable using scriptlet tags:

    <% String name = "John Doe"; %>
    Name: <% out.print(name); %>

    In this example, the variable name is assigned the value "John Doe" using a scriptlet tag. Then, within the HTML section, the value of name is printed using out.print() method from the out object.

Related Questions

Please Login or Register to leave a response.

Related Questions

JSP Interview Questions

Which attribute specifies a JSP page that should process any exceptions thrown but not caught in the current page?

ErrorPage Attribute process specifies a JSP page that should process any exceptions thrown but not caught in the current page....

JSP Interview Questions

Explain What is JSP?

JSP, expanded as Java Server Pages is a Java-based technology to create dynamic web pages. Released in 1999 by Sun Microsystems, JSP is based on technologies such as HTML, SOAP, XML, etc...

JSP Interview Questions

What is the el in JSP?

The EL (Expression Language) in the JSP is used to access the data stored in the JavaBeans along with implicit objects like requests, sessions, etc...