1

I am trying to create a program in which I can accept input and get the square root of a number with a servlet. I am a beginner, so I don't know much. The problem is when I try my code, it doesn't work. Here is the code:

MyServletDemo.java

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

public class MyServletDemo extends HttpServlet {

     public void init() throws ServletException {
    }
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   }
   public void doGet(HttpServletRequest request, 
      HttpServletResponse response)
      throws ServletException, IOException 
   {


      response.setContentType("text/html");
      String num2 = request.getParameter("num");
      int num3 = Integer.parseInt(num2); //This is where the error
      int numSqrt = Math.sqrt(num3);
      PrintWriter out = response.getWriter();
      out.println("<p> The sqrt is "+numSqrt+"</p>");
   }

}

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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Get sqrt of num</title>
</head>
<body>


    <p>Num to find sqrt of: </p> <input type="text" name="num"/>


    <a href="welcome">Click to call Servlet</a>


</body>
</html>

I don't know much about xml, I found the code in a tutorial: 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">
  <display-name>Find-the-sqrt-of-num</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>MyHttpServletDemo</servlet-name>
<servlet-class>MyServletDemo</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyHttpServletDemo</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

</web-app>

The results are: First this, then when you hit the link, you get :

This result

3 Answers3

2

What you need to do is make a POST request to your servlet instead of GET. The idea is if you need to pass information to your servlet, you are POSTING information to your server.

If you don't specify the method type in your jsp, it defaults to GET and GET is used for just moving to different pages of your website without passing/submitting information to your server(servlet)

Take time to know the basic differences of GET and POST requests for a servlet.

In your code, after the tag, you need to wrap all tags inside with a <form> tag and set the attribute of "method" to POST type. In your servlet, instead of doGet() use the doPost() method.

You can find many samples from google of a simple POST request using jsp and servlet.

1

You are getting null error because request.getParameter("num") is null .In your jsp page you never send any value to your servlet i.e : by writing <a href="welcome">Click to call Servlet</a> this will take you to the servlet but will not send any parameter with it .Instead do like below:

 <form method="get" action="Yourservletpage">
   <p>Num to find sqrt of: </p> <input type="text" name="num"/>
   <input type="submit" name="submit" value="submit" />
 </form>
Swati
  • 28,069
  • 4
  • 21
  • 41
1

Or try this:

<a href="welcome?num=123">Click to call Servlet</a>
ErShakirAnsari
  • 653
  • 7
  • 19