8.write A JSP Program To Demonstrate The Declaration, Scriplets and Expressions. 8.jsp

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

8.

Write a JSP program to demonstrate the declaration, scriplets and


expressions.
8.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Example</title>
</head>
<body>
<!-- Declaration -->
<%!
String message = "Hello, World!";
%>

<!-- Scriptlet -->


<%
int x = 5;
int y = 10;
int sum = x + y;
%>

<!-- Expression -->


<h1><%= message %></h1>
<p>Sum of <%= x %> and <%= y %> is <%= sum %>.</p>
</body>
</html>
Output:

9.Write a JSP program to demonstrate the setAttribute and getAttribute


method.
9.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Attribute Example</title>
</head>
<body>
<%
// Set an attribute
String message = "Hello, World!";
request.setAttribute("message", message);

// Get the attribute


String retrievedMessage = (String) request.getAttribute("message");
%>

<h1>Message set using setAttribute:</h1>


<p><%= retrievedMessage %></p>
</body>
</html>
Output:

10.Write a JSP program to demonstrate the use of JSP Page directives


10.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Page Directive Example</title>
</head>
<body>
<h1>JSP Page Directive Example</h1>
<p>This is an example of using JSP page directives.</p>

<%-- This is a comment in JSP --%>

<%-- Including another JSP page --%>


<%@ include file="included.jsp" %>

<%-- Setting a session attribute --%>


<% session.setAttribute("username", "JohnDoe"); %>

<%-- Forwarding the request to another resource --%>


<%@ include file="forwarded.jsp" %>
</body>
</html>
Included.jsp:
<p>This is the included JSP file.</p>
Forwarded.jsp:
<p>This is the forwarded JSP file.</p>
<p>The username attribute set in the main JSP is: <%=
session.getAttribute("username") %></p>

Output:

11.Write a JSP program to pass a parameter from one jsp file to


another and display the values.
Index.jsp:
<!DOCTYPE html>
<html>
<head>
<title>Pass Parameter Example</title>
</head>
<body>
<form action="second.jsp" method="get">
<label for="param">Enter Parameter:</label>
<input type="text" id="param" name="param">
<input type="submit" value="Submit">
</form>
</body>
</html>

Second.jsp:
<!DOCTYPE html>
<html>
<head>
<title>Second JSP</title>
</head>
<body>
<h1>Received Parameter</h1>
<p>Parameter value: <%= request.getParameter("param") %></p>
</body>
</html>

Output:

12.Create an HTML form to accept the login name and password,


check the authorized login name and password through JSP at the
server side and display appropriate message.

Login.html:
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<h2>Login Form</h2>
<form action="login.jsp" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>

<label for="password">Password:</label>
<input type="password" id="password" name="password"
required><br><br>

<input type="submit" value="Login">


</form>
</body>
</html>

Login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Status</title>
</head>
<body>
<h2>Login Status</h2>
<%-- Define authorized username and password --%>
<%
String authorizedUsername = "admin";
String authorizedPassword = "password";

// Retrieve username and password from the request


String username = request.getParameter("username");
String password = request.getParameter("password");

// Check if the credentials are correct


if(authorizedUsername.equals(username) &&
authorizedPassword.equals(password)) {
out.println("<p>Login successful!</p>");
} else {
out.println("<p>Invalid username or password. Please try
again.</p>");
}
%>
</body>
</html>
Output:

Only admin , password ivandi inka emi ichina fail avtadhi code lo by
default declare chesam username, password

You might also like