从servlet中检索参数数组

时间:2017-05-01 13:49:38

标签: java jsp servlets

我想检索在输入的文本类型中输入的参数。问题是它们的名称取决于forEach,我找不到从servlet中检索的方法。

<!DOCTYPE html>
  <html>
       <head>
          <meta charset="utf-8" />
          <title>Editer les sous-titres</title>
      </head>
      <body>
          <form method="post">    
          <input type="submit" value="Envoyer"  style="position:fixed; top: 10px; right: 10px;" />
          <table>
            <c:forEach items="${ subtitles }" var="line" varStatus="status">
                <tr>
                    <td><c:out value="line${ status.index }"></c:out></td>
                    <td style="text-align:right;"><c:out value="${ line }" />
                                                                          </td>
                    <td><input type="text" name="line${ status.index }" id="line${ status.index }" size="35" value="<c:out value='${ traductions }'></c:out>" /></td>
                </tr>
            </c:forEach>
        </table>
    </form>
</body>
</html>

这是我的servlet的代码:

package com.subtitlor.servlets;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.subtitlor.beans.Traduire;
import com.subtitlor.dao.DaoFactory;
import com.subtitlor.dao.SubtitlorDao;
import com.subtitlor.utilities.SubtitlesHandler;

@WebServlet("/EditSubtitle")
public class EditSubtitle extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String FILE_NAME = "/WEB-INF/password_presentation.srt";
    private SubtitlorDao subtitlorDao;

    public void init() throws ServletException {
        DaoFactory daoFactory = DaoFactory.getInstance();
        this.subtitlorDao = daoFactory.getUtilisateurDao();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        System.out.println(context.getRealPath(FILE_NAME));
        SubtitlesHandler subtitles = new SubtitlesHandler(context.getRealPath(FILE_NAME));
        request.setAttribute("subtitles", subtitles.getSubtitles());

        for(int i=0;i<688;i++)
        {
            Cookie[] cookies = request.getCookies();
            if(cookies != null)
            {
                for(Cookie cookie : cookies)
                {
                    if(cookie.getName().equals("traduction[i]"))
                    {
                        request.setAttribute("traductions", cookie.getValue());
                    }
                }
            }
        }

        this.getServletContext().getRequestDispatcher("/WEB-INF/edit_subtitle.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Traduire trad = new Traduire();

        String[] line= new String[688];

        for(int i=0;i<688;i++)
        {   
            line[i]=request.getParameter("line"+i);
            trad.setTraduction(line[i]);
            subtitlorDao.ajouter(trad);

            Cookie cookie = new Cookie("traduction[i]", line[i]);
            cookie.setMaxAge(60 * 60 * 24 * 30);
            response.addCookie(cookie);

            Cookie[] cookies = request.getCookies();
            if(cookies != null)
            {
                for(Cookie cookiee : cookies)
                {
                    if(cookiee.getName().equals("traduction[i]"))
                    {
                        request.setAttribute("traductions", cookiee.getValue());
                    }
                }
            }
        }   

        this.getServletContext().getRequestDispatcher("/WEB-INF/edit_subtitle.jsp").forward(request, response);
    }

}

0 个答案:

没有答案