0

In my jsp I have many input type number tags. When I submit them I go to my servlet who needs to read them to save the values in a database. I have tried request get attribute , get parameter, but they always return null values. I have also tried using Servlet File Upload but it returns a class cast exception saying that it can not cast MonitorRequestWrapper to RequestContext.

my jsp code:

<form method="GET" action="HomeServlet">

    <c:forEach var="kat" items="${requestScope.Category}">            
        <h2 style="background-color:blue; color: white; width:150px;">${kat.getName()}</h2>
        <c:if test='${kat.getName() == "Bike"}'>
            <table>
            <c:forEach var="pro" items="${requestScope.Bikes}">
                <tr>
                <td><label>${pro.getName()}</label></td>
                <td><label>${pro.getPrice()}</label>&nbsp;<span>kn</span></td>
                <td><label>Amount:</label></td>>
                <c:set var="pr" value="proz"></c:set>
                <c:set var="prp" value="${pro.getStringID()}"></c:set>
                <c:set var="id" value="${pr.concat(prp)}"></c:set>
                <td><input type="number" name="${id}"></td>
                </tr>
            </c:forEach>
                </table>
        </c:if>
        <c:if test='${kat.getName() == "Ball"}'>
            <table>
            <c:forEach var="pro" items="${requestScope.Balls}">
                <tr>
                <td><label>${pro.getName()}</label></td>
                <td><label>${pro.getPrice()}</label>&nbsp;<span>kn</span></td>
                <td><label>Amount:</label></td>>
                <c:set var="pr" value="proz"></c:set>
                <c:set var="prp" value="${pro.getStringID()}"></c:set>
                <c:set var="id" value="${pr.concat(prp)}"></c:set>
                <td><input type="number" name="${id}"></td>
                </tr>
            </c:forEach>
                </table>
        </c:if>          
    </c:forEach>
        <br>
        <input type="submit" value="Choose">
    </form>

my servlet code:

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession sesija =request.getSession();

    try {
        r = new Repository();
        bouth = r.getBouthProducts();
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletContext servletContext = this.getServletConfig().getServletContext();
        File rep= (File) servletContext.getAttribute("javax.servlet.context.tempdir");
        factory.setRepository(rep);
        List<FileItem> items = new ServletFileUpload(factory).parseRequest((RequestContext) request);
        for (FileItem item : items) {
            for (Product pro : r.GetProducts()) {
                String s = "proz";
                String concat = s.concat(pro.getStringID());
                String name = item.getFieldName();
                if(name.equals(concat)){
                    int i = Integer.parseInt(item.getString());
                    if(i > 0)
                    {
                        - doesn't matter
                    }
                }
            }
        }
    } catch (SQLException ex) {
        response.sendError(ex.getErrorCode());
    } catch (FileUploadException ex) {
        Logger.getLogger(HomeServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
   -- doesn't matter
}
Bacteria
  • 8,406
  • 10
  • 50
  • 67
Jure Fadiga
  • 195
  • 2
  • 19

0 Answers0