Using Scriptlets in JSP with Example

We may need to write Java code in our JSP program instead of just printing something on the web page. The code usually is not of a single line. So, we need to write multiple lines of code may be 100. We can achieve this with scriptlets.

A scriptlet is a fragment of code. Code fragment is written in a scriptlet tag. You can write as many scriptlet tags you want. There is no restriction. You can open a curly brace in one scriptlet tag and close it in the other scriptlet tag. The code fragment is enclosed between <% and %>.

Therefore it is obvious that though the code fragments are written in different tags, they are contiguous.

Code

Folder structure

Project folder: D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\scriptletjsp

scriptletjsp
      |_ index.html
      |_ print.jsp
      |_ WEB-INF/
            |_  web.xml

index.html

<html>
    <head>
        <title>Print JSP Page</title>
    </head>
    
    <body>
    
    <center>
    <h1>Generate a Password for me</h1>

        <form action="./print.jsp">
            Enter your name <input type="text" name="name">
            <input type="submit" value="GENERATE">
        </form>
    </center>
    </body>
</html>

print.jsp

<html>
    <head>
        <title>Print JSP Page</title>
    </head>
    
    <body>
        <%
    
        // Create PrintWriter object
        java.io.PrintWriter pw=response.getWriter();
        
        // Get the value submitted by user in 'name' form
        String name=request.getParameter("name");

        // Generate random number
        java.util.Random r=new java.util.Random();

        // Store the generated password
        String password=name+r.nextInt(1000);
        %>

        <!-- Printing the password -->
        <%
        pw.println("<h1>Your password is "+password+"</h1>");
        %>
    </body>
</html>

Output

Generating Password in JSP from User input

The Generated Password from JSP

Code explanation

The code is very simple. I hope you can understand the HTML file. The HTML file consists of code tags that create an input field of type text and the other of type SUBMIT (which submits the form). The tag encloses these two input fields.

The name of the input field of type text is name, the value will be entered by the user. We can get this value in our JSP program through a single line statement.

The action of the form is where the request has to be sent. The web server passes the request to the print.jsp file which is in the same folder as that of the HTML file.

Now, the JSP file handles this request. Our code does the job. As you can see in the JSP there are <% and %> repeated twice. But the code isn’t repeated twice. <% and %> enclose the code fragment. The code fragment is written in Java.

The request, response objects have life (i.e. memory). So we can call them to get things done. These names are already defined.

<% is the starting of the scriptlet tag and %> is the closing. Between these two the code fragment is written.

For generating random numbers see my post about it.

request.getParameter("name"): This method returns the value in the input field named name.

response.getWriter(): This returns the java.io.PrintWriter object.

pw.println("<h1>Your password is "+password+"</h1>"); This prints the password within the <h1> tag to just make it big.

Also see my other posts on Hello World! Example in JSP and adding two numbers in JSP easily.

ధన్యవాదాలు (Thanks)

No comments: