gpt4 book ai didi

java - IllegalStateException 错误

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

我正在创建一个登录页面,工程师可以通过以“engg”开头的用户名登录。问题出在登录页面,当我使用正确的密码提供正确的输入时,它会给出“非法状态异常”。在错误的输入中,它工作正常。就像当我在我的 oracle 表中给出“engg140”时,它会给出异常.代码是:

     import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.java2d.pipe.GeneralCompositePipe;

import accessdb.Dao;
import accessdb.Getset;

public class AdminLogIn extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @return
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
String username=request.getParameter("username");
String password=request.getParameter("password");
Getset g=new Getset();
Dao dao=new Dao();

if(username.equals("") || password.equals(""))

{
response.sendRedirect("login.jsp");

}
else{
String newuname=username.substring(0,4);
// System.out.println(""+username);
if(newuname.equals("engg"))
{
ResultSet rs=dao.EnggLogInUseridCheck(g);

System.out.println(""+newuname);

while(rs.next())
{
String uname=rs.getString("uname");
System.out.println(""+uname);
if(uname.equals(username))
{
g.setuname(username);
System.out.println(""+username);
System.out.println(""+uname);
ResultSet rs1=dao.EnggLogInPasswordCheck(g);
while(rs1.next())
{
String password1=rs1.getString("password");
if(password1.equals(password))
{ System.out.println(""+password1);
System.out.println(""+password);
response.sendRedirect("engghome.jsp");
break;

}
else
{
response.sendRedirect("login.jsp");
return;
}
}

}

}
response.sendRedirect("login.jsp");
}
else
{
response.sendRedirect("login.jsp");
return;
}



}


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

右输入的控制台输出如下:

                     03:31:26,265 INFO  [STDOUT] engg
03:31:26,265 INFO [STDOUT] engg172
03:31:26,265 INFO [STDOUT] engg140
03:31:26,265 INFO [STDOUT] engg140
03:31:26,265 INFO [STDOUT] engg140
03:31:26,281 INFO [STDOUT] 365125
03:31:26,281 INFO [STDOUT] 365125
03:31:26,281 INFO [STDOUT] engg141
03:31:26,281 ERROR [[AdminLogIn]] Servlet.service() for
servlet
AdminLogIn threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
at AdminLogIn.doGet(AdminLogIn.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

最佳答案

您没有包含堆栈跟踪的“引起”部分,这是最重要的部分(实际上,整个堆栈跟踪很重要)。

sendRedirect() 上的 IllegalStateException 与 HTTP 协议(protocol)有关,并且在已提交对客户端的响应时发生。此时,无法再发送重定向,因为 HTTP header 已发送(或至少已排队等待发送)并且无法更改。在代码中的某个位置,在未显示的方法中,您已将某些内容写入输出流并导致提交响应。

这与数据库无关。

关于java - IllegalStateException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387061/

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