我对编程很新,我遇到了这个问题,我有搜索互联网,无法找到我想要的答案。希望你能在这里帮助我。谢谢!
我正在尝试传递2个变量,即名称和custid。 (我使用mysql workbench作为我的数据库)。
我猜我的错误与此有关。
String email=request.getParameter("email");
String password=request.getParameter("password");
这是我的代码:
@WebServlet("/login")
public class loginServlet extends HttpServlet{
@Resource(name="jdbc/eg3752")
private DataSource usercatalogue;
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
String email=request.getParameter("email");
String password=request.getParameter("password");
Connection connection=null;
Statement statement=null;
ResultSet resultset=null;
String login="SELECT * FROM customer where email='"+email+"'";
try
{
connection=usercatalogue.getConnection();
statement=connection.createStatement();
resultset=statement.executeQuery(login);
while(resultset.next())
{
// check if password is same in the database
if(password.equals(resultset.getString("password")))
{
HttpSession session =request.getSession();
session.setAttribute("name", resultset.getString("fullname"));
session.setAttribute("custid", resultset.getString("customerId"));
response.sendRedirect(this.getServletContext().getContextPath()+"/search.html");
}
else
{
response.sendRedirect(this.getServletContext().getContextPath() + "/login.html");
}
}
}
catch(Exception e)
{
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
这是GlassFish Server 4上的错误
Severe: java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:518)
at com.aleksi.loginServlet.doPost(loginServlet.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:1)
你没有包含"由"堆栈跟踪的各个部分,这是重要的部分。
sendRedirect()上的IllegalStateException与HTTP协议有关,并且在已经提交了对客户端的响应时发生。这通常发生在响应缓冲区达到最大大小或者有人显式调用flush()或者向输出流写入内容并导致响应被提交时。
致电sendRedirect()
后,添加return;
,然后尝试。