Reader Level:
ARTICLE

Working With Strings in JSP

Posted by Vikas Mishra Articles | JSP February 06, 2012
In this article I am going to describe how to work with Strings in JSP. Strings, which are widely used in Java programming, are a sequence of characters.
  • 0
  • 0
  • 22626
Download Files:
 

String In JSP

In Java, strings are objects designed to represent a sequence of characters. Because character strings are commonly used in programs, Java supports the ability to declare String constants and perform concatenation of Strings directly without requiring access to methods of the String class. This additional support provided for Java Strings allows programmers to use Strings in a similar manner as other common programming languages. A Java String is read-only and once created the contents cannot be modified. The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

For example:

String str = "abcd"; is equivalent to:
char data[] = {'a', 'b', 'c','d'};
String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abcd");
String cdef = "cdef";
System.out.println("abcd" + cdef);

Now we are going to develop an application to describe Strings. For this we should use the following step.

Step 1: Creating a new project.

This is the first application; in this step we select the New Project option from the File menu.

create new project.jpg

Step 2:
Select Application Type.

In this step we select a web application option from Java web and then click on the next Button.

select new web application.jpg

Step 3:
Name and Location.

In this step we give it a specific name StringInJsp and select a specific location for it and then click on the next Button.

name.jpg

Step 4:
Server and Setting.

In this step we select a specific server glassfish and configure it.

server and setting.jpg

Step 5: Select Framework.

There is no need to select any framework for this application.

selectframework.jpg

Step 6: Create new JSP file.

In this application we create three JSP file (index.jsp, create.jsp and getting.jsp).

create new jsp file.jpg

index.jsp

Through this code we simple print the string.

<%@ page session="false" %>
<%
       String[] colors = {"red", "green", "blue"};
       for (int i = 0; i < colors.length; i++)
       {
           out.print("<p>" + colors[i] + "</p>");
       }
%>

create.jsp

Through this code we simple create the string in jsp

<HTML>
     <HEAD>
          <TITLE>
Creating a String In JSP</TITLE>
     </HEAD>
          <BODY BGCOLOR="cyan">
               <H1>Creating a String In JSP</H1>
                <%
                    String greeting = "Hello from JSP String!";
                    out.println(greeting);
                 %>
          </BODY>
</HTML>



getting.jsp

Through this code we simple get the length of string in jsp

<HTML>
     <HEAD>
         <TITLE>
Getting String Length In JSP</TITLE>
     </HEAD>
         <BODY BGCOLOR="green">
               <H1>Getting String Length In JSP</H1>
                <%
                     String s1 = "Hello from JSP!";
                     out.println("\"" + s1 + "\"" + " is " + s1.length()
                     + " characters long.");
                 %>
         </BODY>
</HTML>

Step 7:
Compiling and running the application.

After completion of the coding, now we first compile it and then run it on the specific web server and find the output.

Output

index.jsp

index.jpg

create.jsp

creating.jpg

getting.jsp

getting.jpg

Resources related to this article

COMMENT USING

Trending up