package servlet;import java.io.IOException;import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import util.DBConnection;public class LoginServlet extends HttpServlet { /**
 * 
 */
private static final long serialVersionUID = 8161250346523392984L; /**
 * Constructor of the object.
 */
public LoginServlet() {
super();
} /**
 * Destruction of the servlet. <br>
 */
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
 * The doDelete method of the servlet. <br>
 * 
 * This method is called when a HTTP delete request is received.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException { // Put your code here
} /**
 * The doGet method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response); } /**
 * The doPost method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to
 * post.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { // 设置提交字符编码
request.setCharacterEncoding("gb2312");
// 取得session
HttpSession session = request.getSession(true); // 取得提交参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String role = request.getParameter("role");
        

if (!this.checkUser(username, password)) {
//当用户名或密码不对时,就跳转到登录页面重新登录

session.setAttribute("errMsg", "");
session.setAttribute("errMsg", "用户名或密码不对,请重新输入!");
RequestDispatcher requestDispathcer = request
.getRequestDispatcher("/login.jsp");
requestDispathcer.forward(request, response);
} //用户名和密码正确,判断用户类型

if ("0".equals(role)) {

RequestDispatcher requestDispathcer = request
.getRequestDispatcher("/student.jsp");
requestDispathcer.forward(request, response);    //这是119行


if ("1".equals(role)) {

RequestDispatcher requestDispathcer = request
.getRequestDispatcher("/teacher.jsp");
requestDispathcer.forward(request, response);  //这是125行
} } // 验证用户名和密码
public boolean checkUser(String username, String password) {
ResultSet rs = null;
String sql = "select * from  user where username = '" + username + "'";
String dbPassword = "";
DBConnection dbc = new DBConnection();
rs = dbc.getResultSet(sql); try {

if (rs != null && rs.next()) {
dbPassword = rs.getString("password");
}
} catch (SQLException e) { e.printStackTrace();
} if (password.equals(dbPassword)) {
return true;
} return false; } public void init() throws ServletException {

}}
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.IllegalStateException: Cannot forward after response has been committed
servlet.LoginServlet.doPost(LoginServlet.java:125)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.
--------------------------------------------------------------------------------Apache Tomcat/5.5.25
我代码里没有对response进行了发送或者flush之类的操作,会有这种异常!

解决方案 »

  1.   

    你重复执行了requestDispathcer.forward(request, response);这句,,
    你应该用if else
    而不应该用if if...
      

  2.   

    if ("0".equals(role)) {
      ...
    }else if ("1".equals(role)) {
      ...
    }
    都是对role判断,该用else if吧
      

  3.   

    我想问下role总不会同时等于1和0吧
    也就是说两个if里面的语句最多只有一个会执行或者都不执行
    也就是只有一个转向
    为生活一定要用else if难道判断if时候就等于请求转向了?