java.lang.IllegalStateException

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");
    }
}

 

转载于:https://www.cnblogs.com/yi-mi-yangguang/p/6241085.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值