Show List
JSP Coding Questions
- Create a JSP page that displays "Hello, World!" in an
<h1>
heading.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
- Create a JSP page that prompts the user for their name and displays a greeting.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Greeting</title>
</head>
<body>
<form action="greeting.jsp" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<input type="submit" value="Submit">
</form>
<%
String name = request.getParameter("name");
if (name != null) {
out.println("<p>Hello, " + name + "!</p>");
}
%>
</body>
</html>
- Create a JSP page that displays the current date and time.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Current Date and Time</title>
</head>
<body>
<p>The current date and time is: <%= new java.util.Date() %></p>
</body>
</html>
- Create a JSP page that displays a random number between 1 and 10.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Random Number</title>
</head>
<body>
<p>Your random number is: <%= (int) (Math.random() * 10) + 1 %></p>
</body>
</html>
- Create a JSP page that displays a table of the multiplication table for a number entered by the user.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Multiplication Table</title>
</head>
<body>
<form action="multiplication.jsp" method="post">
<label for="number">Number:</label>
<input type="number" id="number" name="number" required>
<input type="submit" value="Submit">
</form>
<%
int number = Integer.parseInt(request.getParameter("number"));
%>
<table>
<% for (int i = 1; i <= 10; i++) { %>
<tr>
<td><%= i %></td>
<td>x</td>
<td><%= number %></td>
<td>=</td>
<td><%= i * number %></td>
</tr>
<% } %>
</table>
</body>
</html>
- Create a JSP page that displays a list of links to other JSP pages.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Links</title>
</head>
<body>
<ul>
<li><a href="page1.jsp">Page 1</a></li>
<li><a href="page2.jsp">Page 2</a></li>
<li><a href="page3.jsp">Page 3</a></li>
</ul>
</body>
</html>
- Create a JSP page that displays a random quote.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Random Quote</title>
</head>
<body>
<p>"<%= getRandomQuote() %>"</p>
</body>
</html>
<%
String[] quotes = { "Be the change you wish to see in the world.", "The only way to do great work is to love what you do.", "Believe you can and you're halfway there." };
String getRandomQuote() {
return quotes[(int) (Math.random() * quotes.length)];
}
%>
- Create a JSP page that displays a list of links to images.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Images</title>
</head>
<body>
<ul>
<li><a href="image1.jsp">Image 1</a></li>
<li><a href="image2.jsp">Image 2</a></li>
<li><a href="image3.jsp">Image 3</a></li>
</ul>
</body>
</html>
- Create a JSP page that displays a random image from a list of images.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Random Image</title>
</head>
<body>
<img src="<%= getRandomImage() %>" alt="Random Image">
</body>
</html>
<%
String[] images = { "image1.jpg", "image2.jpg", "image3.jpg" };
String getRandomImage() {
return images[(int) (Math.random() * images.length)];
}
%>
- Create a JSP page that displays a form for adding a new user to a database.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Add User</title>
</head>
<body>
<form action="addUser.jsp" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<input type="submit" value="Add User">
</form>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username != null && password != null) {
// Code to add user to database
}
%>
</body>
</html>
- Create a JSP page that displays a list of users from a database.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Users</title>
</head>
<body>
<table>
<tr>
<th>Username</th>
<th>Password</th>
</tr>
<%
// Code to get list of users from database
while (resultSet.next()) {
String username = resultSet.getString("username");
String password = resultSet.getString("password");
%>
<tr>
<td><%= username %></td>
<td><%= password %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
- Create a JSP page that displays a form for searching a database for a specific item.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="search.jsp" method="get">
<label for="query">Search:</label>
<input type="text" id="query" name="query" required>
<input type="submit" value="Search">
</form>
<%
String query = request.getParameter("query");
if (query != null) {
// Code to search database for item
}
%>
</body>
</html>
- Create a JSP page that displays a form for uploading a file to the server.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
</head>
<body>
<form action="upload.jsp" method="post" enctype="multipart/form-data">
<label for="file">Select file to upload:</label>
<input type="file" id="file" name="file" required>
<input type="submit" value="Upload">
</form>
<%
if (request.getMethod().equalsIgnoreCase("post")) {
String fileName = request.getPart("file").getSubmittedFileName();
// Code to save file to server
}
%>
</body>
</html>
- Create a JSP page that displays a form for sending an email.
jspCopy code
<!DOCTYPE html>
<html>
<head>
<title>Send Email</title>
</head>
<body>
<form action="sendEmail.jsp" method="post">
<label for="to">To:</label>
<input type="email" id="to" name="to" required>
<br>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" required>
<br>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<br>
<input type="submit" value="Send">
</form>
<%
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
if (to != null && subject != null && message != null) {
// Code to send email
}
%>
</body>
</html>
Leave a Comment