onkar patil. onkar patil. - 3 years ago 277
Java Question

description The requested resource is not available

I am receiving error while trying to run the code.
Not able to find where is the problem.
I may missing some minor details, it will be great if you could correct it.


import java.io.IOException;
import javax.servlet.*;
@WebServlet(name="CalculateServlet", urlPatterns={"/CalculateServlet"})
public class CalculateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

* Default constructor.
public CalculateServlet() {
// TODO Auto-generated constructor stub

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//assuming request as the object of the HttpServletRequest Class.
//retrieving & storing the values from the textboxes into the String Variables.
String n1 = request.getParameter("fnum");
int num1 = Integer.parseInt(n1); //Converting String into Integer Variable
String n2 = request.getParameter("snum");
int num2 = Integer.parseInt(n2); int ans=0;

//performing calculation according to the selection made from the Radio Buttons named "calc".
ans = num1+num2;
ans = num1-num2;
ans = num1/num2;
ans = num1*num2;

//assuming response as the object of the HttpServletResponse Class.
//displaying output to the user

response.getWriter().append("Served at: ").append(request.getContextPath());

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<form action="CalculateServlet">
<input type="text" name="fnum"/>
<input type="text" name="snum"/>
Select Operation
<input type="radio" name="calc" value="Add"/>Add
<input type="radio" name="calc" value="Sub"/>Subtract
<input type="radio" name="calc" value="Div"/>Divide
<input type="radio" name="calc" value="Multi"/>Multiply
<input type="submit" value="Calculate" name="submit"/>


Directory Structure

Answer Source

The URL Pattern in your web.xml is


But your jsp submits it to "calculate"

<form action="calculate">

Change it to,

<form action="CalculateServlet">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download