0

Currently I'm using netbeans 7.2 using glassfish server. I want to put two numbers using HTML+ Servelet. I'm just a beginner I have no idea. Would somebody help me? I also don't know why I got error: Incompatible types required int found string although I used parseInt to convert String into Integer. Here's the code:

INDEX.JSP:

<%-- 
    Document   : index
    Created on : Oct 3, 2012, 4:06:09 PM
    Author     : len
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form="add/addpackage">
            Name:<input type="text" name="name" value="" />
            Num1:<input type="text" name="num1" value="" />
            Num2:<input type="text" name="num2" value="" />
            <br>
            <input type="submit" value="button" />
        </form>
    </body>
</html>

Servelet:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author len
 */
@WebServlet(name = "HelloServelet", urlPatterns = {"/HelloServelet"})
public class HelloServelet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

            String name = request.getParameter("name");
            int a = request.getParameter("num1");
            int b = request.getParameter("num2");
            int sum = 0;

            int x,y;

            x = Integer.parseInt(a);
            y = Integer.parseInt(b);

            sum = x + y;

            out.println("Hi!" + name + "The sum is" + sum);     
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
3
Contributors
2
Replies
8
Views
5 Years
Discussion Span
Last Post by jalpesh_007
1

int a = request.getParameter("num1");
int b = request.getParameter("num2");

here a and b are integer variable,
request.getparameter() method will return integer value,
change those variable type as string

Integer.parseInt

this method reuqires string as argument

0

request.getParameter() method returns the value in string.

So you have to take String object to store the value of that text field instead of int a write String a and String b.

Then convert into integer using INTEGER.parseInt(a) and then add both.

Thank you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.