我有登录JSP,它将重定向到名为DisplayData的servlet,它必须检查用户凭据并仅为注册用户显示数据。但是它会抛出一个名为“在响应提交后无法转发”的错误。这是servlet代码
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session = request.getSession(true);
HttpSession usersession = request.getSession(true);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String query;
Connection conn;
Statement stmt;
ResultSet res;
DatabaseConnection dbconn;
String username="";
String hiddenname = request.getParameter("hiddenname");
if (hiddenname.equals("login"))
{
username = request.getParameter("username");
//System.out.println(username);
String password = request.getParameter("password");
// System.out.println(password);
usersession.setAttribute("uname", username);
usersession.setAttribute("upass", password);
Connection con = dbconnection.getCon();
System.out.println(con);
PreparedStatement statemt = null;
try {
statemt = con.prepareStatement("select User_name,Password from login_details where User_name = ? and Password = ?");
System.out.println(statemt);
statemt.setString(1, username); // set input parameter 1
statemt.setString(2, password); // set input parameter 2
ResultSet rs = statemt.executeQuery();
if (rs.next() == false)
{
out.write("Invalid user name or password. Please press back button to login again");
}
else
{
String url="/index.jsp";
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
con.close();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
List lst=new ArrayList();
String login_name,login_password;
try
{
login_name=(String) session.getAttribute("uname");
login_password=(String) session.getAttribute("upass");
request.setAttribute("UserName", login_name);
request.setAttribute("UserPassword", login_password);
dbconn=new DatabaseConnection();
conn=dbconn.setConnection();
stmt=conn.createStatement();
query="select * from mpi";
res=dbconn.getResultSet(query, conn);
while(res.next())
{
lst.add(res.getString("UniqueID"));
lst.add(res.getString("Phylum"));
lst.add(res.getString("Family"));
lst.add(res.getString("Genus"));
lst.add(res.getString("NCBI_Taxnomy_ID"));
lst.add(res.getString("16s_Sanger_seq"));
lst.add(res.getString("Genome_Sequencing_Batch"));
lst.add(res.getString("Stock_number"));
lst.add(res.getString("Stock_Location"));
lst.add(res.getString("Soil_batch"));
lst.add(res.getString("Host"));
lst.add(res.getString("Operator"));
lst.add(res.getString("GPS_coordinates"));
lst.add(res.getString("Greenhouse_or_Natural_sites"));
lst.add(res.getString("Isolation_procedure"));
lst.add(res.getString("Date_of_isolation"));
lst.add(res.getString("Previous_Ids"));
lst.add(res.getString("Compartment"));
lst.add(res.getString("Publication"));
lst.add(res.getString("Strain_Derivatives"));
lst.add(res.getString("Growth_conditions"));
lst.add(res.getString("Natural_antibiotic_resistance"));
lst.add(res.getString("Colony_morphology"));
}
res.close();
}
catch(Exception e)
{
RequestDispatcher rd=request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
e.printStackTrace();
}
finally
{
request.setAttribute("UserData", lst);
RequestDispatcher rd=request.getRequestDispatcher("/displayrecord.jsp");
rd.forward(request, response);
lst.clear();
out.close();
}
}
我不知道错误在哪里。请帮助我。
答案 0 :(得分:5)
您的代码中有几个转发。一个例子:
String url="/index.jsp";
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
在代码的下方,你有try/catch/finally
的阻止。具体来说,你有块
finally
{
request.setAttribute("UserData", lst);
RequestDispatcher rd=request.getRequestDispatcher("/displayrecord.jsp");
rd.forward(request, response);
lst.clear();
out.close();
}
转发请求时,它不会停止在您的方法中执行。一旦完成前进,它将继续执行您离开的位置,并且您最终使用finally
块保证前进。无论代码中先前发生了什么,都将执行finally
块。如果您是第一次在代码顶部执行其中一个转发,当您点击finally
块时它会失败,因为它会尝试再次转发请求,这将导致错误正在经历。
仔细查看完整的堆栈跟踪,因为它应该指示第二次尝试使用响应(在您的情况下为转发)的确切行号。您需要重新排列/重写代码,以便只执行一个前进(最多)。