1

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.

CalculateServlet

    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".
    if(request.getParameter("calc").equals("Add"))
    ans = num1+num2;
    if(request.getParameter("calc").equals("Sub"))
    ans = num1-num2;
    if(request.getParameter("calc").equals("Div"))
    ans = num1/num2;
    if(request.getParameter("calc").equals("Multi"))
    ans = num1*num2;

    //assuming response as the object of the HttpServletResponse Class.
    //displaying output to the user 
    response.getWriter().println(ans); 

    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);
}}

Index.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <form action="CalculateServlet">
   <input type="text" name="fnum"/>
   <br/>
   <input type="text" name="snum"/>
   <br/>
   Select Operation
   <br/>
   <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
   <br/>
   <input type="submit" value="Calculate" name="submit"/>
</form>
    </body>
</html>

Output1

Directory Structure

2 Answers2

2

The URL Pattern in your web.xml is

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

But your jsp submits it to "calculate"

<form action="calculate">

Change it to,

<form action="CalculateServlet">
Jerin Joseph
  • 1,087
  • 9
  • 17
  • I tried the changes you suggested but still with no success – Onkar Patil Oct 21 '16 at 18:19
  • @onkarpatil. what is the URL you are seeing in the browser after you click submit? – Jerin Joseph Oct 21 '16 at 18:22
  • @onkarpatil. Can you also provide the error you are seeing. – Jerin Joseph Oct 21 '16 at 18:23
  • @onkarpatil. This does'nt seem right, Even after changing the form action to CalculateServlet, the URL still directs to /Calc/calculation. Can you rebuild and re-deploy? – Jerin Joseph Oct 21 '16 at 18:29
  • Sure i will try – Onkar Patil Oct 21 '16 at 18:31
  • Still with the same Error http://localhost:8080/Calc/calculation?fnum=1&snum=2&calc=Add&submit=Calculate HTTP Status 404 - /Calc/calculation type Status report message /Calc/calculation description The requested resource is not available. -------------------------------------------------------------------------------- Apache Tomcat/8.0.38 – Onkar Patil Oct 21 '16 at 18:38
  • Can you have a look at directory structure – Onkar Patil Oct 21 '16 at 18:38
  • @onkarpatil. the directory structure looks fine. This still does not seem right. Can you update the jsp file with the changes in the question. – Jerin Joseph Oct 21 '16 at 18:44
  • I closed the work space and reopened and now it works fine. Thanks Jerin Joseph for your time and immediate response. – Onkar Patil Oct 21 '16 at 18:51
0

looks like you are close.

Take a look at this thread:

<form action="/sampleServlet" giving me exception

I think your issue is the form action is set to calculation

Community
  • 1
  • 1
D. Wood
  • 56
  • 4