Java 以JSON格式JSPServlet将表单详细信息保存在文件中

Java 以JSON格式JSPServlet将表单详细信息保存在文件中,java,json,jsp,servlets,Java,Json,Jsp,Servlets,我在jsp中创建了一个注册表单,然后通过使用servlet检索这些值 在servlet中,我编写了创建文件的代码,并尝试将数据以json格式保存在该文件中 但我无法保存数据:-( 我可以使用请求调度器将这些值转发到下一页吗? 完全处于混乱中 File f=new File("C:\\Users\\marslab4\\workspace\\Registration\\WebContent\\sample2.json"); f.createNewFile();

我在jsp中创建了一个注册表单,然后通过使用servlet检索这些值

在servlet中,我编写了创建文件的代码,并尝试将数据以json格式保存在该文件中

但我无法保存数据:-(

我可以使用请求调度器将这些值转发到下一页吗? 完全处于混乱中

        File f=new File("C:\\Users\\marslab4\\workspace\\Registration\\WebContent\\sample2.json");
        f.createNewFile();
        //out.println("json");
        /*FileWriter filewriter = new FileWriter(f, true);
        filewriter.write("first name:" + fname);
        filewriter.write("last name:" + lname);
        filewriter.write("user name:" + uname);
        filewriter.write("email:" + email);
        filewriter.write("password:" + pswd);
        filewriter.write("confirm password:" + repswd);
        filewriter.write("phonenumber:" + phonenumber);
        filewriter.write("security question:" + SecurityQuestion);
        filewriter.write("answer" + tarea);
        filewriter.flush();
        filewriter.close(); */

    //saving in JSON form

    JSONObject json=new JSONObject();
    JSONArray jarray=new JSONArray();
    JSONObject details;
   try
    {
       for(int i=0;i<10;i++)
       {
               details = new JSONObject();
               details.put("firstname:",fname);
               details.put("lastname:",lname);
               details.put("username:",uname);
               details.put("email:",email);
               details.put("password:",pswd);
               details.put("confirm password:",repswd);
               details.put("phonenumber:",phonenumber);
               details.put("security question:",SecurityQuestion);
               details.put("answer:",tarea);
               jarray.put(details);
       }
               json.put("details",jarray);
    }
    catch(JSONException e)
    {
        out.print("unable to save in JSON File");
        e.printStackTrace();
    } 
   response.setContentType("application/json");
   response.getWriter().write(json.toString());

您可以尝试以下代码:

File f = new File("C:\\Users\\marslab4\\workspace\\Registration\\WebContent\\sample2.json");        
FileWriter filewriter = new FileWriter(f, true);
filewriter.write(json.toString()); // Pass json object.
filewriter.flush();
filewriter.close();
您不应该单独传递每个请求参数,而应该传递创建的json对象,如上所示


希望这有帮助。

您需要将请求保存到文件中吗?或者您需要将一些信息发送回客户端吗?您想先解决哪个问题?hi@alayor首先我需要保存到文件中。稍后我需要解析JSON对象以显示给客户端。为什么您无法保存到文件中?您能够从cli获取数据吗输入servlet?在txt文件中,我可以保存。但是,我需要以JSON格式保存..“名字:ganeshlast name:pasupuletiuser name:ganeshpemail:ganesh@gmail.compassword:jhansi123确认密码:jhansi123电话号码:999999999安全问题:foanswerbchboifhbxhifhg“我想您可以使用相同的代码保存在txt文件中,但调用writer方法的方式是
filewriter.write(json.toString());
File f = new File("C:\\Users\\marslab4\\workspace\\Registration\\WebContent\\sample2.json");        
FileWriter filewriter = new FileWriter(f, true);
filewriter.write(json.toString()); // Pass json object.
filewriter.flush();
filewriter.close();