javascript 从 JSP 到 Servlet,然后回到同一个 JSP,字段为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12469038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:16:21  来源:igfitidea点击:

Go from JSP to Servlet, then come back to same JSP and the fields are empty

javascriptjspservlets

提问by Simpson

I need to go from my JSP to my Servlet and then come back to the same JSP. The problem is that when i come back all my text fields are empty.

我需要从我的 JSP 转到我的 Servlet,然后再回到同一个 JSP。问题是当我回来时,我所有的文本字段都是空的。

How do i solve this? Notes:

我该如何解决这个问题?笔记:

  1. The form has method='post' enctype='multipart/form-data'.
  2. I'm using buttons to submit. They are normal buttons that execute a javascript function onclick, this function validates the text fields and if everything is ok then it calls to the submit()function and submits the form.
  3. The come back from the servlet is being done with this code:

    RequestDispatcher rd = request.getRequestDispatcher("/altaPerfil.jsp");
    rd.forward(request, response);
    
  1. 表格有method='post' enctype='multipart/form-data'.
  2. 我正在使用按钮提交。它们是执行 javascript 函数 onclick 的普通按钮,该函数验证文本字段,如果一切正常,则调用该submit()函数并提交表单。
  3. 从 servlet 返回正在使用以下代码完成:

    RequestDispatcher rd = request.getRequestDispatcher("/altaPerfil.jsp");
    rd.forward(request, response);
    


Additional info:


附加信息:

My JSP page is similar to this one: https://www.taringa.net/registro.
It has one form, inside the form there are 7 text fields, 3 select fields for the date(exactly like in that page), an upload field to upload an image, a radioButton with two fields and 3 buttons.
One of the buttons is used to validate the nick and email fields(if the nick or mail are already taken it returns false, otherwise returns true, this is done in the servlet because we try to keep the logic separated from the presentation).
The second button is used to upload the image.
The third button is used to send all the form(all the text fields and the date).
The first two buttons must go to the servlet and come back to the JSP...
Example code:
(JSP)

我的 JSP 页面与此类似:https: //www.taringa.net/registro
它有一个表单,表单内有 7 个文本字段、3 个日期选择字段(与该页面完全一样)、一个用于上传图像的上传字段、一个具有两个字段和 3 个按钮的单选按钮。
其中一个按钮用于验证昵称和电子邮件字段(如果昵称或邮件已被获取,则返回 false,否则返回 true,这是在 servlet 中完成的,因为我们试图将逻辑与演示文稿分开)。
第二个按钮用于上传图像。
第三个按钮用于发送所有表单(所有文本字段和日期)。
前两个按钮必须去servlet然后回到JSP...
示例代码:
(JSP)

<form action="altaPerfilServlet" 
      name = "frmValidar" 
      method='post' enctype='multipart/form-data' >

    Nick:
    <br />
    <input type="text" name="nick" 
           id="nick" onkeypress="deshabilita()" 
           value="${requestScope.nick}" />

<input type=button name="botonValidar" 
           id="botonValidar" value="Validar datos" 
           onclick="validarNick()" />
<!--Note: the javascript validarNick() verifies the nick field and if it is -->
<!--not empty then it calls frmValidar.submit()-->



(Servlet)

(小服务程序)

  FileItemFactory factory = new DiskFileItemFactory();  
  ServletFileUpload upload = new ServletFileUpload(factory);

  List<FileItem> fields = upload.parseRequest(request);
  Iterator<FileItem> it = fields.iterator();

  <!--here i process all the FileItems and obtain their values-->
  <!--then i talk to a java app (which acts as a server, providing -->
  <!--the data and a series of classes) and obtain a boolean value(esValido)-->
  <!--that tells me if the nick is available(true) or taken(false)-->

  <!--finally: -->

  request.setAttribute("esValido", esValido);<!-- saving the boolean -->

  String nick=request.getParameter("nick");
  request.setAttribute("nick", nick);
  RequestDispatcher rd = request.getRequestDispatcher("/altaPerfil.jsp");
  rd.forward(request, response);

回答by adatapost

Read the <input>data from requestin doPostmethod and push/bind <input>data into request scope.

读取<input>从数据requestdoPost的方法和推/绑定<input>数据到请求范围。

sample.jsp

示例.jsp



<form method="post" action="servlet_url">
  No   : <input type="text" name="no" value="${requestScope.no}"/>
  Name : <input type="text" name="name" value="${requestScope.name}"/>
  <input type="submit"/>
</form>

and code in doPost,

和代码doPost

String no=request.getParameter("no");
String name=request.getParameter("name");

//other statements

request.setAttribute("no",no);
request.setAttribute("name",name);

RequestDispatcher rd = request.getRequestDispatcher("/sample.jsp");
rd.forward(request, response);