Home>

Excuse me for a long sentence.

I am creating a task management application (Index, Create, Show, Edit) in Java (Eclipse).

There is no error in the code, but when I check the operation on the browser, I get a 500 error (null).

Right now, Index and New are working.

The content of the error.
java.lang.NumberFormatException: null
java.lang.Integer.parseInt (Integer.java:542)
java.lang.Integer.parseInt (Integer.java:615)
controllers.editServlet.doGet (editServlet.java:37)
javax.servlet.http.HttpServlet.service (HttpServlet.java:622)
javax.servlet.http.HttpServlet.service (HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)
filters.EncodingFilter.doFilter (EncodingFilter.java:38)

Since this is a beginner, I would be grateful if you could tell me how to rewrite this part.
Thank you.
Uses Macbook Air. It uses Java 8 and Tomcat 8.

1st sheet (edit.jsp)
2nd (editServlet)
3rd (show.jsp)
4th (showServlet)
5th (index.jsp)
6th (indexServlet)
7th (new.jsp)
8th (newServlet)

edit.jsp

<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%><% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%><c: import url = "../ layout/app.jsp"><c: param name = "content">       id: ${task.id} edit page       <form method = "POST" action = "${pageContext.request.contextPath}/update">        <c: import url = "_ form.jsp" />    </form>    <p>           Return to list       </p>    <p>           Delete       </p>    <form method = "POST"
            action = "${pageContext.request.contextPath}/destroy">        <input type = "hidden" name = "_token" value = "${_token}" />    </form>    <script>          function confirmDestroy () {
                if (confirm ("Are you sure I want to delete?")) {
                    document.forms [1] .submit ();
                }
            }
      </script></c: param></c: import>


editServlet

package controllers;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.TaskData;
import utils.DBUtil;
/ **
 * Servlet implementation class EditServlet
 * /@WebServlet ("/ edit")
public class editServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
   /**
     * @see HttpServlet # HttpServlet ()
     * /
    public editServlet () {
        super ();
    }
   /**
     * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse
     * response)
     * /
    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = DBUtil.createEntityManager ();
        TaskData t = em.find (TaskData.class, Integer.parseInt (request.getParameter ("id")));
        em.close ();
        request.setAttribute ("task", t);
        request.setAttribute ("_token", request.getSession (). getId ());
        request.getSession (). setAttribute ("task_id", t.getId ());
        RequestDispatcher rd = request.getRequestDispatcher ("/WEB-INF/views/tasks/edit.jsp");
        rd.forward (request, response);
    }
}

show.jsp

<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%><% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%><% @ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt"%><c: import url = "../ layout/app.jsp"><c: param name = "content">       id: ${task.id} Details       <p>        <c: out value = "${task.content}" />    </p>    <p>          Creation date and time
          <fmt: formatDate value = "${task.created_at}"
                pattern = "yyyy-MM-dd HH: mm: ss" />    </p>    <p>          Update date and time
          <fmt: formatDate value = "${task.updated_at}"
                pattern = "yyyy-MM-dd HH: mm: ss" />    </p>    <p>           Return to list       </p>    <p>Edit</p></c: param></c: import>

showServlet

package controllers;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.TaskData;
import utils.DBUtil;
/ **
 * Servlet implementation class ShowServlet
 * /
@WebServlet ("/ show")
public class ShowServlet extends HttpServlet {private static final long serialVersionUID = 1L;
   /**
     * @see HttpServlet # HttpServlet ()
     * /
    public ShowServlet () {
        super ();
        // TODO Auto-generated constructor stub
    }
   /**
     * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse
     * response)
     * /
    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = DBUtil.createEntityManager ();
        TaskData t = em.find (TaskData.class, Integer.parseInt (request.getParameter ("id")));
        em.close ();
        request.setAttribute ("task", t);
        RequestDispatcher rd = request.getRequestDispatcher ("/WEB-INF/views/tasks/show.jsp");
        rd.forward (request, response);
    }
}

index.jsp

<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%><% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%><c: import url = "../ layout/app.jsp"><c: param name = "content">       Task list       <ul>        <c: forEach var = "task" items = "${tasks}">            <li>                                        <c: out value = "${task.id}" />                 <c: out value = "${task.content}" />            </li>        </c: forEach>    </ul>    <p>           New task       </p></c: param></c: import>

indexServlet

package controllers;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.TaskData;
import utils.DBUtil;
/ **
 * Servlet implementation class IndexServlet
 * /
@WebServlet ("/ index")
public class indexServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
   /**
     * @see HttpServlet # HttpServlet ()
     * /
    public indexServlet () {
        super ();
        // TODO Auto-generated constructor stub
    }/ **
     * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse
     * response)
     * /
    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = DBUtil.createEntityManager ();
        List<TaskData>tasks = em.createNamedQuery ("getAllTasks", TaskData.class) .getResultList ();
        response.getWriter (). append (Integer.valueOf (tasks.size ()). toString ());
        em.close ();
        request.setAttribute ("tasks", tasks);
        RequestDispatcher rd = request.getRequestDispatcher ("/WEB-INF/views/tasks/index.jsp");
        rd.forward (request, response);
    }
}

new.jsp

<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%><% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%><c: import url = "../ layout/app.jsp"><c: param name = "content">       Create new       <form method = "POST" action = "${pageContext.request.contextPath}/create">        <c: import url = "_ form.jsp" />    </form>    <p>           Return to list       </p></c: param></c: import>


newServlet

package controllers;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.TaskData;
/ **
 * Servlet implementation class NewServlet
 * /
@WebServlet ("/ new")
public class NewServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
   /**
     * @see HttpServlet # HttpServlet ()
     * /
    public NewServlet () {
        super ();
        // TODO Auto-generated constructor stub
    }
   /**
     * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse
     * response)
     * /
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // CSRF.΍.
         request.setAttribute ("_token", request.getSession (). getId ());
         // ���� 
         request.setAttribute ("task", new TaskData ());
         RequestDispatcher rd = request.getRequestDispatcher ("/WEB-INF/views/tasks/new.jsp");
         rd.forward (request, response);
    }
}