how do i solve the double method while string in java with servlet

package com.dev.web.Controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dev.web.Dao.UserDao;
import com.dev.web.Model.User;

public class UserControll extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static String INSERT_OR_EDIT = "/user.jsp";
    private static String LIST_USER = "/listEmp.jsp";
    private UserDao dao;

    public UserControll() {
        super();
      UserDao dao = new UserDao();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String forward="";
        String action = request.getParameter("action");

        if (action.equalsIgnoreCase("delete")){
            int Employee_Id= Integer.parseInt(request.getParameter("Employee_Id"));
            dao.deleteUser(Employee_Id);
            forward = LIST_USER;
            request.setAttribute("users", dao.getAllUsers());    
        } else if (action.equalsIgnoreCase("edit")){
            forward = INSERT_OR_EDIT;
            int Employee_Id = Integer.parseInt(request.getParameter("empId"));
            User user = dao.getUserById(Employee_Id);
            request.setAttribute("user", user);
        } else if (action.equalsIgnoreCase("listUser")){
            forward = LIST_USER;
            request.setAttribute("Employee", dao.getAllUsers());
        } else {
            forward = INSERT_OR_EDIT;
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        user.setEmployee_Name(request.getParameter("empname"));
        user.setSalary(request.getParameter("empsalary"));// here is my error..i declared salary is double
        try {
            Date dob = new SimpleDateFormat("MM/dd/yyyy").parse(request.getParameter("empdob"));
            user.setDob(dob);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setActive(request.getParameter("empactive"));// here is my error.. i declared active is boolean
        String empid = request.getParameter("empid");
        if(empid == null || empid.isEmpty())
        {
            dao.addUser(user);
        }
        else
        {
            user.setEmployee_ID(Integer.par`enter code here`seInt(empid));
            dao.updateUser(user);
        }
        RequestDispatcher view = request.getRequestDispatcher(LIST_USER);
        request.setAttribute("users", dao.getAllUsers());
        view.forward(request, response);
    }
}

Answers


The getParameter() method of HttpServletRequest is inherited from ServletRequest. From the API docs you can see this method always returns a string. You need to parse your boolean or double from this string, for example

Double.parseDouble(request.getParameter("empsalary"));

Need Your Help

Creating has_many relationships in ActiveRecord without :primary_key

ruby activerecord

I'm working with an older version of Rails and ActiveRecord that does not have the :primary_key option in has_many relationships of more recent ActiveRecord versions and unfortunately I don't get to

popup box should not close if user having error in their signup form

javascript php jquery ajax forms

This php coding is showing the error message correctly. But, my problem is that popup box does not hold on if the registration form having error. For example, if you click a link one popup box will...