Jump to content

Recommended Posts

I'm tring to create a servlet that will calculate numbers input by the user but i'm a little stuck.

This is what i have done so far

<HTML><HEAD><TITLE>Call servlet to Calculate numbers</TITLE></HEAD>

<BODY><H1>Calculator</H1>

<P><FORM ACTION="/servlet/Add"
   METHOD=GET>

<P>First number : <INPUT TYPE=TEXT NAME=number1>

<P>Second number : <INPUT TYPE=TEXT NAME=number2>

<P>
<SELECT name=Action>
<OPTION value="" selected>- Select -</OPTION>
<OPTION value="+">Add</OPTION>
<OPTION value="-">Subtract</OPTION>
<OPTION value="/">Divide</OPTION>
<OPTION value="*">Times</OPTION>

</SELECT>

<P><INPUT TYPE=SUBMIT VALUE="Add the numbers">
</FORM>


</BODY>
</HTML>

// AdditionServlet.java

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

public class Add extends HttpServlet {

 public void doGet(HttpServletRequest req, HttpServletResponse res, HttpServletRequest req2)
   throws ServletException, IOException {
   res.setContentType("text/html");
   ServletOutputStream out = res.getOutputStream();

   out.println("<HTML><BODY>");
   out.println("We are pleased to inform you that the result " +
 "of your calculation is now ready:");
   
   String[] number1params = req.getParameterValues("number1");
   int number1 = Integer.parseInt(number1params[0]);
   
   String[] number2params = req.getParameterValues("number2");
   int number2 = Integer.parseInt(number2params[0]);
   
    String[] actionparams = req.getParameterValues("action");
   int action = Integer.parseInt(actionparams[0]);
   
    if (action == '+') {
   int sum = number1 + number2;
   out.println("<P>You kindly sent us the numbers " + number1 +
 " and " + number2);
   out.println("<P>The sum of the numbers is " + sum);
   out.println("</BODY></HTML>");
} else {
   out.println("<P> Wrong value");
   out.println("</BODY></HTML>");
}
   
   
   
 }

I'm getting the error "HTTP method GET is not supported by this URL"

Any ideas what i have done wrong??

Link to comment
Share on other sites


I've not done any programming for a while but this should point you towards the problem.

This error means that the servlet identified by the URL does not implement the 'doGet' method, as a result a HTTP GET request cannot be successful.

I hope this helps.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...