2013-06-30 14 views
0

我是java servlets的新手,並試圖打開我的登錄頁面。這裏是servlet的服務方法:我的servlet有什麼問題 - 獲取java.lang.IllegalStateException

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

不確定爲什麼要在服務方法中初始化PrintWriter。這可能是500內部錯誤的原因,因爲一旦你退出你的服務方法,作家將被刷新/關閉。當你在轉發請求之後嘗試呈現jsp時,它將拋出響應已經提交的server 500錯誤。因此,請註釋您的printwriter實例,然後嘗試將請求轉發到login.jsp。

這種形式的javadocs

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

另一個重要的事情

爲方法不會奇蹟般地終止代碼執行和跳出你不是forward()方法返回後,的方法。您必須從方法返回並自行停止執行代碼的剩餘部分。

+0

稱爲index.jsp的其他jsp頁面在我的項目中正常工作。我嘗試了你所說的,但仍然有同樣的錯誤。我編輯的問題,並把web.xml文件,你可以看看它可以在XML文件中有問題嗎? – yrazlik

+0

@bigO更新了答案,您需要在轉發方法調用後添加一個返回。 –

+0

工作,謝謝 – yrazlik