JDBC データベースへの追加または更新を処理するサーブレットを作成しようとしています。データは POST を介して渡され、新しいデータを作成または編集しようとすると、java.lang.StackOverflowError が発生します。
コードは次のとおりです。
package servlet;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.netbeans.rest.application.config.JerseyTerrenos;
import org.netbeans.rest.application.config.Terrenos;
public class Sucessoterreno extends HttpServlet {
Terrenos terrenos = new Terrenos();
JerseyTerrenos jterrenos = new JerseyTerrenos();
Integer thebiggest = 0;
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
String nome = request.getParameter("nome");
String coords = request.getParameter("coords");
if (request.getParameter("id") != null) {
terrenos = jterrenos.find_XML(Terrenos.class, request.getParameter("id"));
terrenos.setTerrenosNome(nome);
terrenos.setTerrenosCords(coords);
jterrenos.edit_XML(terrenos);
} else {
ClientResponse clientResponse = jterrenos.findAll_XML(ClientResponse.class);
GenericType<List<Terrenos>> gType = new GenericType<List<Terrenos>>() {
};
List<Terrenos> listterrenos;
listterrenos = (clientResponse.getEntity(gType));
for (int i = 0; i <= listterrenos.size(); i++) {
if (thebiggest < listterrenos.iterator().next().getTerrenosId()) {
thebiggest = listterrenos.get(i).getTerrenosId();
}
}
terrenos.setTerrenosId(thebiggest);
terrenos.setTerrenosNome(nome);
terrenos.setTerrenosCords(coords);
jterrenos.create_XML(terrenos);
}
PrintWriter out = response.getWriter();
try {
out.print("<!DOCTYPE html>\n"
+ "<html>\n"
+ " <head>\n"
+ " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
+ " <link rel=\"stylesheet\" type=\"text/css\" href=\"resources/style.css\">\n"
+ " <link rel=\"stylesheet\" type=\"text/css\" href=\"resources/reset.css\"/>\n"
+ " <title>Florest Mapping - Terrenos</title>\n"
+ " </head>\n"
+ " <body>\n"
+ " <div id=\"header\">\n"
+ " <h1>Florest Mapping</h1><br>\n"
+ " </div>\n"
+ " <div id=\"content\">\n"
+ " <ul id=\"coolMenu\">\n"
+ " <li>\n"
+ " <a href=\"index.jsp\">Inicio</a>\n"
+ " </li>\n"
+ " <li>\n"
+ " <a href=\"#\">Equipas</a>\n"
+ " <ul class=\"noJS\">\n"
+ " <li><a href=\"equipa\">Criar Equipa</a></li>\n"
+ " <li><a href=\"listarterrenos\">Listar Equipas</a></li>\n"
+ " </ul>\n"
+ " </li>\n"
+ " <li>\n"
+ " <a href=\"#\">Áreas</a>\n"
+ " <ul class=\"noJS\">\n"
+ " <li><a href=\"area\">Criar área</a></li>\n"
+ " <li><a href=\"listarareas\">Listar áreas</a></li>\n"
+ " </ul>\n"
+ " </li>\n"
+ " <li>\n"
+ " <a href=\"#\">Terrenos</a>\n"
+ " <ul class=\"noJS\">\n"
+ " <li><a href=\"terreno\">Criar Terreno</a></li>\n"
+ " <li><a href=\"listarterrenos\">Listar Terrenos</a></li>\n"
+ " </ul>\n"
+ " </li>\n"
+ " </ul>\n" + "<br><br><div id=\"main\">"
+ " <h2>Criar Terreno</h2>\n");
out.println("Terreno criado (ou editado) com Sucesso</div></div>\n");
out.println(" <div id=\"footer\">\n"
+ " 2013 - João Correia\n"
+ " </div>\n"
+ " </body>\n"
+ "</html>");
} finally {
out.close();
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}