標籤:boolean redirect div odi return語句 驅動 public long sof
java.lang.IllegalStateException
org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:423)
經過分析、查看jdk文檔終於找到解決的辦法,在response.sendRedirect()方法後加return語句即可,如下:
response.sendRedirect(login.jsp);
return null;
原因是:在程式中兩次調用了response.sendRedirect()方法.
jdk5.0文檔中很清楚地介紹了出現IllegalStateException異常的可能情況:
1)同一個頁面中再次調用response.sendRedirect()方法.
2)提交的URL錯誤,即不是個有效URL.
package com.servlet;import java.io.*;import java.sql.*;import javax.servlet.http.*;import javax.servlet.*;import javax.swing.JOptionPane;import com.bean.DataByPage;import com.sun.rowset.CachedRowSetImpl;public class Handlemodify extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @SuppressWarnings("restriction") CachedRowSetImpl rowSet = null; @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (Exception e) { System.out.println("沒有載入到驅動"); } } @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setCharacterEncoding("gb2312"); HttpSession session = req.getSession(true); /* * true: 如果session存在,則返回該session,否則建立一個新的session; false: * 如果session存在,則返回該session,否則返回null. */ String Id = req.getParameter("identity"); String password = req.getParameter("password").trim(); String again_password = req.getParameter("again_password").trim(); // boolean b = password.length() > 0 && again_password.length() > 0; Connection con = null; String cdn = ""; DataByPage dataBean = null; try { dataBean = (DataByPage) session.getAttribute("dataBean"); if (dataBean == null) { dataBean = new DataByPage();// 建立對象 session.setAttribute("dataBean", dataBean); } } catch (Exception exp) { dataBean = new DataByPage();// 建立對象 session.setAttribute("dataBean", dataBean); } if (password == null) password = ""; if (!password.equals(again_password)) { // userBean.setBackNews("兩次密碼不同,註冊失敗"); JOptionPane.showMessageDialog(null, "你輸入的密碼不同,請重新輸入", "error", JOptionPane.ERROR_MESSAGE); resp.sendRedirect("/demo9/student/modify.jsp"); return;//這裡得加個return 不然會報java.lang.IllegalStateException } try { con = DriverManager.getConnection( "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=manage", "wyc", "123456"); Statement sql = con .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); cdn = "update users set password=‘" + password + "‘" + "where Idnumber=‘" + Id + "‘"; ResultSet rs = sql.executeQuery(cdn); rowSet = new CachedRowSetImpl(); rowSet.populate(rs);// 將rs記錄轉載到CachedRowSetImpl對象中,然後關閉資料庫連接,釋放資源 dataBean.setRowSet(rowSet); con.close(); } catch (SQLException ex) { System.out.println(ex); } resp.sendRedirect("/demo9/tiaozhuan.jsp"); }}
java.lang.IllegalStateException