0

我是 java servlet 的新手,并试图打开我的登录页面。这是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 文件夹的屏幕截图:

在此处输入图像描述

最后是 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>

任何人都可以帮忙吗?谢谢

4

1 回答 1

1

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

这形成了javadocs

forward 应该在响应提交给客户端之前调用(在刷新响应主体输出之前)。如果已提交响应,则此方法将引发 IllegalStateException。响应缓冲区中未提交的输出在转发之前会自动清除。

另一个重要的事情

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

于 2013-06-30T00:21:06.760 回答