Java Servlet Radians to Degrees

Java Servlet Radians to Degrees


Posted in : Java Posted on : October 13, 2011 at 4:32 PM Comments : [ 0 ]

In this section you will learn how to change the radians into degrees using java servlet.

Java Servlet Radians to Degrees

In this section you will learn how to change the radians into degrees using java servlet.

Here I am giving a simple java servlet example which is concern to change the radians value into degrees. In mathematics radian is used for measuring angle. It can be defined as the ratio of length and radius of a circular arc.

Now, since I have to use this function in java servlet program, therefore I have created a java servlet class named RadianToDegreeServlet which extends the HttpServlet class. In the body of class I overridden the method doGet() and created an objects of HttpServletRequest and HttpServletResponse into its parameter. Inside the doGet() method first I set the mime type that in which format the browser will show the output. In the next line I used the getWriter() method of ServletResponse interface with the object of HttpServletResponse. HttpServletResponse interface extends this method from the ServletResponse. In Next step I am taking input using the getParameter() method of ServletRequest with the object of HttpServletRequest. And create a  method for calculating to change the radian into degree.

Example :

radianTodegree.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>RadianToDegree</title>
</head>
<body>
<form method="get" action="RadianToDegreeServlet">
<table>
<tr><td>Enter a value in radian </td><td><input type="text" name="text1"/></td></tr>
<tr><td></td><td><input type="submit" value="ok"/></td></tr>
</table>
</form>
</body>
</html>

RadianToDegreeServlet.java

package simpleServletExample;
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 RadianToDegreeServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
double rad = Double.parseDouble(request.getParameter("text1"));

out.println(degree(rad));
}
public int degree(double rad)
{
double deg = (rad*(180/Math.PI));
int d = (int)deg;
return d;
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>servletBasicExample</display-name>
<servlet>
<servlet-name>RadianToDegreeServlet</servlet-name>
<servlet-class>simpleServletExample.RadianToDegreeServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>RadianToDegreeServlet</servlet-name>
<url-pattern>/RadianToDegreeServlet</url-pattern>
</servlet-mapping> 

</web-app>

Output :

When you will execute the above example you will get the output as :

When you will enter the value

You will get the output like as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics