如何解决带有servlet的java中的字符串的double方法

时间:2015-07-14 05:21:08

标签: java jsp servlets

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

1 个答案:

答案 0 :(得分:0)

getParameter() HttpServletRequest方法继承自ServletRequest。从API docs您可以看到此方法始终返回string。您需要从此字符串中解析boolean或double,例如

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