我的servlet有什么问题 - 得到java.lang.IllegalStateException

时间:2013-06-30 00:16:39

标签: java java-ee servlets

我是java servlets的新手,并尝试打开我的登录页面。这是servlet的服务方法:

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}

这是我的login.jsp文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>


<form action="servletexample" method="post">
    <table border="0">
        <tr>
            <td>User Name:</td><td><input type="text" name="firstname" /></td>
        </tr>
        <tr>
            <td>Password:</td><td><input type="password" name="lastname" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
        </tr>
    </table>
    </form>

</body>
</html>

当我运行项目时,我收到以下错误:

HTTP Status 500 - Cannot forward after response has been committed

--------------------------------------------------------------------------------

type Exception report

message Cannot forward after response has been committed

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
com.exaple.tutorial.ServletExample.service(ServletExample.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.37 logs.

以下是WebContent文件夹的屏幕截图:

enter image description here

最后是web.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ServletsJSPExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ServletExample</display-name>
    <servlet-name>ServletExample</servlet-name>
   <servlet-class>com.exaple.tutorial.ServletExample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletExample</servlet-name>
    <url-pattern>/servletexample</url-pattern>
  </servlet-mapping>
</web-app>

有人可以帮忙吗?感谢

1 个答案:

答案 0 :(得分:1)

不确定为什么要在服务方法中初始化PrintWriter。这可能是导致500内部错误的原因,因为一旦退出服务方法,编写器将被刷新/关闭。当你尝试在转发请求后呈现jsp时,它将抛出响应已经提交的服务器500错误。因此,请注释掉您的printwriter实例,然后尝试将请求转发给login.jsp。

这构成javadocs

  应该在响应提交之前调用

forward   客户端(在刷新响应正文输出之前)。如果回应   已经提交,这个方法抛出了   IllegalStateException异常。 响应缓冲区中未提交的输出是   在前锋之前自动清除。

另一件重要的事情

你没有在forward()方法之后返回,因为该方法不会神奇地终止代码执行并跳出方法。您必须从方法返回并自行停止执行代码的残余。