Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Servlet Help

Featured Replies

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??


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.

Yes, I have a php calculator script it you would like that instead if the java. I will post it only if youll like it

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.