31.
Write a java servlet which accepts two numbers from an HTML form and displays their sum
and diffference on another page.
Html
<form action="CalcServlet" method="get">
Enter First Number: <input type="text" name="num1"><br>
Enter Second Number: <input type="text" name="num2"><br>
<input type="submit" value="Calculate">
</form>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class CalcServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
{
int a = [Link]([Link]("num1"));
int b = [Link]([Link]("num2"));
int sum = a + b;
int diff = a - b;
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("Sum: " + sum + "<br>Difference: " + diff);
32. Write a servlet displaying the current date and time.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class DateTimeServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
{
Date date = new Date();
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("Current Date and Time: " + [Link]());
33. Write a program to add two numbers using JSP.
Html
<form action="[Link]" method="post">
Number 1: <input type="text" name="num1"><br>
Number 2: <input type="text" name="num2"><br>
<input type="submit" value="Add">
</form>
[Link]
<%
int a = [Link]([Link]("num1"));
int b = [Link]([Link]("num2"));
int sum = a + b;
%>
<h2>Sum = <%= sum %></h2>
34. Create a JSP page to accept a number from an user and display it in words: Example:
123 – One Two Three.
Html
<form action="[Link]" method="post">
Enter Number: <input type="text" name="num">
<input type="submit" value="Show in Words">
</form>
[Link]
<%
String num = [Link]("num");
String[] words = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
%>
<h2>Number in Words:</h2>
<%
for(char ch : [Link]()) {
if([Link](ch)) {
[Link](words[[Link](ch)] + " ");
} else {
[Link]("? ");
%>
35. Write a JSP program to perform Arithmetic operations such as Addition, Subtraction,
Multiplication and Division. Design a HTML to accept two numbers in text box and radio
buttons to display operations. On submit display result as per the selected operation on next
page using JSP.
Html
<form action="[Link]" method="post">
Number 1: <input type="text" name="num1"><br>
Number 2: <input type="text" name="num2"><br>
Operation:<br>
<input type="radio" name="op" value="add">Add<br>
<input type="radio" name="op" value="sub">Subtract<br>
<input type="radio" name="op" value="mul">Multiply<br>
<input type="radio" name="op" value="div">Divide<br>
<input type="submit" value="Calculate">
</form>
[Link]
<%
int a = [Link]([Link]("num1"));
int b = [Link]([Link]("num2"));
String op = [Link]("op");
int result = 0;
switch(op) {
case "add": result = a + b; break;
case "sub": result = a - b; break;
case "mul": result = a * b; break;
case "div": result = b != 0 ? a / b : 0; break;
%>
<h2>Result: <%= result %></h2>
36. Create a JSP page, which accepts user name in a text box and greets the user
according to the time on server side.
Example: If user name is Admin Output: If it is morning then display message in red color as,
Good morning Admin Today’s date: dd/mm/yyyy format Current time: hh:mm:ss format If it is
afternoon then display message in green color as, Good afternoon Admin Today’s date :
dd/mm/yyyy format Current time : hh:mm:ss format If it is evening then display message in
blue color as, Good evening Admin Today’s date: dd/mm/yyyy format Current time:
hh:mm:ss format (Hint: To display date and time use GregorianCalendar and Calendar class)
Html
<form action="[Link]" method="post">
Enter your name: <input type="text" name="username">
<input type="submit" value="Greet Me">
</form>
[Link]
<%@ page import="[Link].*, [Link].*" %>
<%
String name = [Link]("username");
Calendar cal = new GregorianCalendar();
int hour = [Link](Calendar.HOUR_OF_DAY);
String greeting = "";
String color = "";
if (hour < 12) {
greeting = "Good Morning";
color = "red";
} else if (hour < 17) {
greeting = "Good Afternoon";
color = "green";
} else {
greeting = "Good Evening";
color = "blue";
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
%>
<h2 style="color:<%= color %>;">
<%= greeting %> <%= name %><br>
Today’s date: <%= [Link]([Link]()) %><br>
Current time: <%= [Link]([Link]()) %>
</h2
37. Write a JSP program to display number of times user has visited the page. (Use cookies)
<%
int count = 1;
Cookie[] cookies = [Link]();
if (cookies != null) {
for (Cookie c : cookies) {
if ([Link]().equals("visitCount")) {
count = [Link]([Link]()) + 1;
Cookie visitCookie = new Cookie("visitCount", [Link](count));
[Link](60 * 60 * 24 * 365); // 1 year
[Link](visitCookie);
%>
<h2>You have visited this page <%= count %> times.</h2>
38. Create a simple Java Servlet that returns a "Hello, World!" message when accesses by a
web browser.
import [Link].*;
import [Link].*;
import [Link].*;
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>Hello, World!</h1>");
}
}
39. Write a JSP page that uses an expression to display the sum of two numbers entered by
the user through an HTML form
Html
<form action="[Link]" method="post">
Number 1: <input type="text" name="a"><br>
Number 2: <input type="text" name="b"><br>
<input type="submit" value="Get Sum">
</form>
[Link]
<%
int a = [Link]([Link]("a"));
int b = [Link]([Link]("b"));
%>
<h2>Sum: <%= a + b %></h2>
40. Write a JSP page that connects to a MySQL database and retrieves a list of records from
a table (e.g., "Students").
Sql
CREATE TABLE Students (
id INT PRIMARY KEY,
name VARCHAR(100),
percentage FLOAT
);
[Link]
<%@ page import="[Link].*" %>
<%
String url = "jdbc:mysql://localhost:3306/your_db";
String user = "root";
String pass = "your_password";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
[Link]("[Link]");
conn = [Link](url, user, pass);
stmt = [Link]();
rs = [Link]("SELECT * FROM Students");
%>
<table border="1">
<tr><th>ID</th><th>Name</th><th>Percentage</th></tr>
<%
while([Link]()) {
%>
<tr>
<td><%= [Link]("id") %></td>
<td><%= [Link]("name") %></td>
<td><%= [Link]("percentage") %></td>
</tr>
<%
%>
</table>
<%
} catch(Exception e) {
[Link]("Database error: " + [Link]());
} finally {
if(rs != null) [Link]();
if(stmt != null) [Link]();
if(conn != null) [Link]();
%>
41. Write a Spring MVC program for subtraction of two numbers.
42. Write a Spring MVC program for multiplication of two numbers.
Both in one program
SpringMVC_Calc/
├── src/
│ └── [Link]/
│ └── [Link]
├── WebContent/
│ ├── WEB-INF/
│ │ ├── views/
│ │ │ ├── [Link]
│ │ │ └── [Link]
│ │ └── [Link]
│ └── [Link]
├── [Link]
[Link]
<web-app>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>[Link]</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/[Link]</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
[Link]
<beans xmlns="[Link]
xmlns:mvc="[Link]
xmlns:context="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
<context:component-scan base-package="[Link]"/>
<mvc:annotation-driven/>
<bean class="[Link]">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
[Link]
<jsp:forward page="[Link]" />
[Link]
<h2>Spring MVC Calculator</h2>
<form action="calculate" method="post">
Enter First Number: <input type="text" name="num1"><br>
Enter Second Number: <input type="text" name="num2"><br><br>
Select Operation:
<select name="operation">
<option value="sub">Subtraction</option>
<option value="mul">Multiplication</option>
</select><br><br>
<input type="submit" value="Calculate">
</form>
[Link]
<h2>Result:</h2>
<p><strong><%= [Link]("result") %></strong></p>
<a href="[Link]">Try Again</a>
[Link]
package [Link];
import [Link];
import [Link].*;
import [Link];
@Controller
public class CalcController {
@RequestMapping(value = "/calculate", method = [Link])
public String calculate(HttpServletRequest request) {
int num1 = [Link]([Link]("num1"));
int num2 = [Link]([Link]("num2"));
String op = [Link]("operation");
int result = 0;
String output = "";
if ([Link]("sub")) {
result = num1 - num2;
output = "Subtraction: " + result;
} else if ([Link]("mul")) {
result = num1 * num2;
output = "Multiplication: " + result;
}
[Link]("result", output);
return "result";
}
}