0

I am reading 2 numbers as input and am trying to print them in the output dynamically using Servlet, req.getParameter() returns null:

HTTP Status 500 – Internal Server Error Type Exception Report

Message null

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NumberFormatException: null java.lang.Integer.parseInt(Integer.java:542) java.lang.Integer.parseInt(Integer.java:615) com.telusko.AddServlet.service(AddServlet.java:11) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.26

Index.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
      </head>
      <body>
         <form action="add">
            Enter 1st number: <input type="text' name="num1"><br>
            Enter 2nd number: <input type="text' name="num2"><br>
            <input type="submit">
         </form>
      </body>
    </html>

AddServlet.java

  package com.telusko;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;

  public class AddServlet extends HttpServlet
 {
public void service(HttpServletRequest req, HttpServletResponse res)
{
    int i =Integer.parseInt(req.getParameter("num1"));
    int j =Integer.parseInt(req.getParameter("num2"));
    int k = i + j;
    System.out.println("result is"+k);
}
  }

Web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 
       version="3.1">
 <servlet>
     <servlet-name>abc</servlet-name>
    <servlet-class>com.telusko.AddServlet</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>abc</servlet-name>
     <url-pattern>/add</url-pattern>
</servlet-mapping>

I know that my result will get printed in my eclipse console.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jvoo
  • 19
  • 1
  • 1
  • 2
  • 3
    There is a typo in this: ``. See it? And the next line? That probably means that your form is not sending the sending the `num1` and `num2` parameters properly. – Stephen C Sep 28 '19 at 15:53
  • @Stephen C Couldn't notice your comment before answering. I think solution should be in answer, instead of in comments. As most of users will not notice comments as i and other two answerer did. :) – PraveenKumar Lalasangi Sep 30 '19 at 00:53
  • @PraveenKumarLalasangi - Actually, if my comment is correct, then this Question should be closed as "Off-topic - not reproducible or caused by a simple typo". That's why I didn't post an Answer! – Stephen C Sep 30 '19 at 01:13
  • FWIW: I commented after the other two people posted their answers. I remember. So you are the only person for whom there is evidence that they didn't notice the comment :-) – Stephen C Sep 30 '19 at 01:17

1 Answers1

0

You are trying to parse null hence NumberFormatException or 500 error.
Your request does not have parameters num1 and num2. Because your form is not sending those param's.
Your form is not sending those param's beacause of typo error. You mixed double quote and single quote for a attribute value. ensure both are double quote "text" or single quote 'text'.

it is always recommended to look for errors in browser console(In case of javascript) and view page source in case of html.

Outcome of this typo error.
enter image description here

In this error scenario. If you try to get type and name
enter image description here

PraveenKumar Lalasangi
  • 3,255
  • 1
  • 23
  • 47