gpt4 book ai didi

java - 错误500响应未提交

转载 作者:行者123 更新时间:2023-12-01 11:16:30 26 4
gpt4 key购买 nike

我试图向我的 jsp 页面(即制造商详细信息)发送请求并通过 session 获取属性,但它抛出 500 错误。

    package com.osahub.disaster.controller;
import java.io.IOException;

import static com.osahub.disaster.controller.Ofymethodadmin.ofy;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.osahub.disaster.controller.SendMail;

@SuppressWarnings("serial")
public class admin extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
resp.setContentType("text/plain");
resp.getWriter().println("Data Saved!");

PrintWriter out = resp.getWriter();

String name= req.getParameter("name");
String address = req.getParameter("address");
String email = req.getParameter("email");
String website = req.getParameter("website");
String topex = req.getParameter("topex");
String topdes = req.getParameter("topdes");
String topmob = req.getParameter("topmob");
String year = req.getParameter("year");
String brand = req.getParameter("brand");
String factory = req.getParameter("factory");
String sector = req.getParameter("sector");
String contactpername = req.getParameter("contactpername");
String contactperdes = req.getParameter("contactperdes");
String contactpermob = req.getParameter("contactpermob");

HttpSession currentSession=req.getSession();
currentSession.setAttribute("name", name);
req.getRequestDispatcher("ManufacturerDetails.jsp").forward(req, resp);


SendMail mail = new SendMail();
mail.send(email, "send Test mail from gae" , "this is the mail body");



ManufacturerDetails ad = new ManufacturerDetails(name,address,email,website,topex,topdes,topmob,year,brand,factory,sector,contactpername,contactperdes,contactpermob);
ofy().save().entity(ad);
ofy().clear();

List<ManufacturerDetails> li = ofy().load().type(ManufacturerDetails.class).list();
Iterator<ManufacturerDetails> iter = li.iterator();

while(iter.hasNext())
{
ManufacturerDetails ad1 = iter.next();
System.out.println(ad1.getName());
System.out.println(ad1.getAddress());
System.out.println(ad1.getEmail());
System.out.println(ad1.getWebsite());
System.out.println(ad1.getTopex());
System.out.println(ad1.getTopdes());
System.out.println(ad1.getTopmob());
System.out.println(ad1.getYear());
System.out.println(ad1.getBrand());
System.out.println(ad1.getFactory());
System.out.println(ad1.getSector());
System.out.println(ad1.getContactpername());
System.out.println(ad1.getContactpermob());
System.out.println(ad1.getContactperdes());

}
resp.sendRedirect("about.jsp");


}
}

最佳答案

任何异常都可能导致 HTTP 状态代码 500 的响应。

但是,在您的情况下,似乎抛出了异常,因为您在 req.getRequestDispatcher("ManufacturerDetails.jsp") 之后调用 resp.sendRedirect("about.jsp"); .forward(req, resp);

您不能这样做,因为一旦对 forward() 的调用完成,响应就会被提交。但是,这并不意味着 doPost() 方法返回控制权,它会继续执行。因此,resp.sendRedirect("about.jsp"); 会导致抛出 java.lang.IllegalStateException 异常,因为响应已经关闭。

你必须检查你的方法,并引入一个条件语句:

if (condition) {
// do something
req.getRequestDispatcher("ManufacturerDetails.jsp").forward(req, resp);
} else {
// do something
resp.sendRedirect("about.jsp");
}

另一种选择是在 req.getRequestDispatcher("ManufacturerDetails.jsp").forward(req, resp); 之后引入显式 return

关于java - 错误500响应未提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770760/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com