Simple Calculator using Servlet Part 2

 

Go back to Simple Calculator using Servlet Part 1

 

Hope you have understand the basic concept of the simple calculator using servlet. In Calculator part 1 I have explained the layout of the calculator which you need to prepare, Here is the code for the same. This part of simple calculator using servlet explains the way to do it completely.

 

 File: CalculateServlet.java

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

public class CalculateServlet extends HttpServlet
{

public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
display();
PrintWriter out = response.getWriter();
//out.println(“hello”);
String n1 = request.getParameter(“no1”);
String n2 = request.getParameter(“no2”);
String opt = request.getParameter(“opt”);
if(opt.equals(“p”))
out.println(Integer.parseInt(n1) + Integer.parseInt(n2));
else if(opt.equals(“m”))
out.println(Integer.parseInt(n1) – Integer.parseInt(n2));
}

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
doPost(request,response);
}

}

 File : web.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!–
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the “License”); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
–>

<web-app xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”
version=”3.0″
metadata-complete=”true”>

<display-name>Tomcat Manager Application</display-name>
<description>
A scriptable management web application for the Tomcat Web Server;
Manager lets you view, load/unload/etc particular web applications.
</description>

<servlet>
<servlet-name>CalculateServlet</servlet-name>
<servlet-class>CalculateServlet</servlet-class>
</servlet>

<!– Define the Manager Servlet Mapping –>
<servlet-mapping>
<servlet-name>CalculateServlet</servlet-name>
<url-pattern>/CalculateServlet</url-pattern>
</servlet-mapping>
</web-app>

 

Few more Servlet Tutorial for you

Few More Tutorial on JavaScript

Tutorial on PHP

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative