0% found this document useful (0 votes)
17 views5 pages

9,10 Output Fix

The document describes two experiments to create a servlet application that prints a message on a web browser and a JSP application for basic arithmetic calculations. The first experiment creates a HelloWorld servlet that prints a hardcoded message. The second experiment creates a calculator application with a JSP form to accept two numbers and an operator and performs the calculation on submit.

Uploaded by

samswag2898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

9,10 Output Fix

The document describes two experiments to create a servlet application that prints a message on a web browser and a JSP application for basic arithmetic calculations. The first experiment creates a HelloWorld servlet that prints a hardcoded message. The second experiment creates a calculator application with a JSP form to accept two numbers and an operator and performs the calculation on submit.

Uploaded by

samswag2898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 9

AIM:Create a Servlet/ application with a facility to print any message on web browser.

import java.io.*; import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException

// Do required initialization message = "Hello World";


}

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException

PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>");


}

public void destroy()

// do nothing.

<servlet>

<servlet-name>HelloWorld</servlet-name>

<servlet-class>HelloWorld</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>

</servlet-mapping>
Experiment 10
:
Aim Create JSP application for addition, multiplication and division.

Calculator.html
<html>
<head>
<title>Calculator</title>

</head>
<body>
<form action="calculator" method="get" name="frm">

Enter num1:
<input name="txt1" type="text" />

Enter num2:
<input name="txt2" type="text" />

Operator

<select name="op">

<option value="Addition">Addition</option>
<option value="Subtraction">Subtraction</option>
<option value="multiplication">multiplication</option>
<option value="division">division</option>
</select>

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

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

Calculator.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class calculator extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();


String n1 = request.getParameter("txt1");
String n2 = request.getParameter("txt2");
String op = request.getParameter("op");

if(op.equals("Addition")){
out.println((Integer.parseInt(n1) + Integer.parseInt(n2)));
}
else if(op.equals("Subtraction")){
out.println(Integer.parseInt(n1) - Integer.parseInt(n2));
}
else if(op.equals("multiplication")){
out.println(Integer.parseInt(n1) * Integer.parseInt(n2));
}
else{
out.println(Integer.parseInt(n1) / Integer.parseInt(n2));
}
}
}

OUTPUT:

You might also like