JSP Scriptlet Element
JSP Scriptlet :
- Scriptlet tag is used to execute java source code in JSP.
- Scriplet can also have variable and method declaration
Syntax of Scriptlet :
<% code fragment %>
XML equivalent of the above syntax as follows :
<jsp:scriptlet> code fragment </jsp:scriptlet>
Simple Snippet :
We can embed Scriplets inside the HTML tags to make them stylish.
<?php <html> <head><title>Hello World</title></head> <body> <% out.println("Remote IP : " + request.getRemoteAddr()); %> </body> </html> ?>
Output :
Remote IP : 127.0.0.1
Consider the following example -
<html> <body> <form action="welcome.jsp"> <label>Enter Roll :</label> <input type="text" name="sroll"><br /> <label>Enter Name :</label> <input type="text" name="sname"> <input type="submit" value="Submit"> </form> </body> </html>
above index.html file will be used for accepting the user data and submit button will send the details to JSP page for further processing.
<html> <body> <% String roll = request.getParameter("sroll"); String name = request.getParameter("sname"); out.print("Student Roll : " + roll); out.print("<br />"); out.print("Student Name : " + name); %> </body> </html>
file welcome.jsp will do all type of processing,
String roll = request.getParameter("sroll"); String name = request.getParameter("sname");
getParameter method of request object will access the parameters passed by form.
Output :
After clicking on submit button -