Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Jsp :

Scriptlet :(<% …. %>)

Preserving form data while submitting the jsp form to same page :
- Jsp also work with Expression language
- Expression language is denoted by ${}
- The same symbol is also used in javascript
- That’s why when we work in jsp page then ${} (Expression language ) by default work
as serverside code for jsp , so we cannot use it for javascript.
- We can use the expression for read the from’s data / or request parameter
(request.getParameter => ${param.variableName}
- Make sure this expression must be written inside the HTML section of the page not
inside scriptlet (<%. .. %>)
- If the value is missing or not found or form is submitted then it will return empty (in
place of null)

<%--
Document : second
Created on : 9 May, 2023, 8:01:41 AM
Author : user
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post">
<pre>
Enter first no <input type="text" name="n1" value="${param.n1}"/>

Enter second no <input type="text" name="n2" value="${param.n2}"/>

<input type="submit" value="addition" name="submit"/>


</pre>
</form>
<%
if(request.getParameter("submit")!=null){
int a = Integer.parseInt(request.getParameter("n1"));
int b = Integer.parseInt(request.getParameter("n2"));
int c = a+b;
out.println("sum = " + c);
}
%>
</body>
</html>
Jsp expression Tag
<%= expression %>
Expression tag is used to show the value of any variable, object, or expression similar to the
expression language (without using out.println() )
This tag is also used directly inside the HTML section (outside of scriptlet )

Ex
<%
int a=30, b=40;
// out.println("<h2>…..</h2>");
%>
<h2>example of addition of number using jsp</h2>
First number : <%=a%> <!-- <% out.println(a); %> -->
<br/>
Second no : <%=b%> <!-- <%out.println(b);%> -->
<br/>
Sum = <%= a+b%>

Note: if we want the value of text fields after submission of the form then we can use
- ${param.variableName}
- <%=request.getParameter(variableName)%>

<%--
Document : second
Created on : 9 May, 2023, 8:01:41 AM
Author : user
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int a = 10, b=20;
%>

<form method="post">
<pre>
Enter first no <input type="text" name="n1" value="<%=a%>"/>

Enter second no <input type="text" name="n2" value="<%=b%>"/>

<input type="submit" value="addition" name="submit"/>


</pre>
</form>

<h2>After form submission : </h2>


first number is <%=request.getParameter("n1")%> <br/>
Second number is <%=request.getParameter("n2")%> <br/>

<h2>Using Expression language : </h2>


first number is ${param.n1} <br/>
Second number is ${param.n2 }<br/>
</body>
</html>

Jsp Declaration tag :


<%!
Variable declaration
%>
Declaration tag is used for declaring the global variables which are accessible inside whole
page.
Example of using : declaration, scriptlet, expression:

<%--
Document : second
Created on : 9 May, 2023, 8:01:41 AM
Author : user
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
int a = 10;
int b = 20;
%>

<%
if(request.getParameter("submit")!=null) {
a = Integer.parseInt(request.getParameter("n1"));
b = Integer.parseInt(request.getParameter("n2"));
}
%>

<form method="post">
<pre>
Enter first no <input type="text" name="n1" value="<%=a%>"/>

Enter second no <input type="text" name="n2" value="<%=b%>"/>

<input type="submit" value="addition" name="submit"/>


</pre>
</form>

<h2>
<%=a%> + <%=b%> = <%=a+b%>
</h2>

</body>
</html>

Some more examples :


1. Input any number and show the multiplication table of the number in Jsp –
<%--
Document : second
Created on : 9 May, 2023, 8:01:41 AM
Author : user
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
int a = 0;
%>

<%
if (request.getParameter("submit") != null) {
a = Integer.parseInt(request.getParameter("n1"));
}
%>

<form method="post">
<pre>
Enter Number <input type="text" name="n1" value="<%=a%>"/>

<input type="submit" value="show multiplication table" name="submit"/>


</pre>
</form>

<%
if(request.getParameter("submit")!=null){
for(int i=1;i<=10;i++)
out.println(a*i + "<br/>");
}
%>

</body>
</html>

Code -2
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
int a = 0;
%>

<%
if (request.getParameter("submit") != null) {
a = Integer.parseInt(request.getParameter("n1"));
}
%>

<form method="post">
<pre>
Enter Number <input type="text" name="n1" value="<%=a%>"/>

<input type="submit" value="show multiplication table" name="submit"/>


</pre>
</form>

<table border='2' width='400' cellspacing='0'>


<%
if (request.getParameter("submit") != null) {
for (int i = 1; i <= 10; i++) {%>
<tr> <td><%=a%> * <%=i%> </td> <td><%=a*i%></td></tr>
<%}
}
%>
</table>
</body>
</html>

Ex-2:

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
String items[] = {"Computer","mobile","Laptop","Printer","Scanner","Keyboard",
"Mouse"};
%>
<select name="item">
<% for(String s : items){ %>
<option value="<%=s%>"> <%=s%> </option>
<%}%>
</select>
</body>
</html>
- We can use validation, redirection , collaboration etc inside the Jsp page
Login.jsp

<%--
Document : login
Created on : 9 May, 2023, 9:01:48 AM
Author : user
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title> <link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-
awesome.min.css">

<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script
src="https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></scri
pt>
</head>
<body>
<div class="container">
<div class="row">
<div class="col col-md-6">
<form method="post"class="form form-group">
<br/>
<span style="color:red">${param.msg}</span><br/>
<br/>
<input type="text" name="name" placeholder="Enter full Name"
value="${param.name}" class="form-control" required/><br>
<input type="text" name="userid" placeholder="Enter Userid"
value="${param.userid}" class="form-control" required/>
<br/>
<input type="password" name="password" placeholder="Enter Password"
class="form-control" required/>
<br/>

<input type="submit" value="submit" name="submit" class="btn btn-primary"/>


</form>

<%
if(request.getParameter("submit")!=null){
String userid=request.getParameter("userid");
String password=request.getParameter("password");
String name = request.getParameter("name");
if(userid.equals("admin")&& password.equals("admin123"))
response.sendRedirect("welcome.jsp?name="+name);
else
//out.println("<font color='red'> Sorry ! Invalud userid or password
</font>");
response.sendRedirect("login.jsp?msg=Sorry! Invalid userid or
password");
}
%>
</div>
</div>
</div>
</body>
</html>
Welcome.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Welcome ${param.name} !</h1>
</body>
</html>

You might also like