如何从我的JSP类型单选输入中获得是或否的值?

时间:2019-02-10 01:03:27

标签: java jsp servlets input file-writing

我是JSP和servlet的新手。 如果用户选中“是”复选框,我想将用户信息写入文件中。,我的代码为我的if语句检查复选框是否选中提供了例外。

这是我的jsp:

<p>Would you like us to save your personal information? 
    <form  action="inputservlet" method="post">
        <label>yes</label>
        <input type="radio" name="personalInfo" value="yes">
        <label>no</label>
        <input type="radio" name="personalInfo" value="no"><br>
        <label>Email: </label>
        <input type="email" name="email" value="${user1.email}"><br>
        <label>First Name: </label>
        <input type="text" name="firstName" value="${user1.firstName}"><br>
         <label>Last Name: </label>
        <input type="text" name="lastName" value="${user1.lastName}"><br>

    </form>

这是我的servlet doPost方法:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // get parameters from the request (if box was checked) 

        String infoCheckbox = request.getParameter("personalInfo");

        String plan = request.getParameter("plan");
        String email = request.getParameter("email");
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");


        //get file to write personal info to from web.xml
        ServletContext context = getServletContext();
        String fileName = context.getInitParameter("filename");   

// Save  personal information to user object
         User user1 = new User(firstName, lastName, email);
         request.setAttribute("user1", user1); 
         UserFile userfile1= new UserFile();

// If checkbox was checked, write user to file. 

        if(infoCheckbox.equalsIgnoreCase("yes")){
            userfile1.addRecord(user1, fileName);

        }

我在此行得到一个空指针异常:

if(infoCheckbox.equalsIgnoreCase("yes")){
            userfile1.addRecord(user1, fileName);

        }

我在测试时选择了是。 根据选中的复选框,infoCheckbox是否应等于“ yes”或“ no”? 先感谢您。

0 个答案:

没有答案