2012-09-22 74 views
2

我正在轉發到一個jsp,該jsp顯示了由jstl sql標記填充的註釋的表和表單。從servlet轉發到jsp導致表單提交錯誤

的問題是形式,如果我用response.sendRedirect("comments.jsp");

工作正常,但因爲我需要保留頁面之間的會話信息,我想用request.getRequestDispatcher("comments.jsp").forward(request, response); 觸發後的形式和重定向隨後的職位,以servlet的URL。

LoginServlet

public class LoginServlet extends HttpServlet { 
    Connection connection = null; 
    PreparedStatement ptmt = null; 
    ResultSet resultSet = null; 
    User user = null; 
    boolean fail = true; 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     String email = request.getParameter("email"); 
     String password = request.getParameter("password"); 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     if (null != email && null != password) { 
      try { 
       connection = ConnectionFactory.getInstance().getConnection(); 
       user = new User(); 
       String queryString = "SELECT * FROM USERINFO WHERE EMAIL=?"; 
       ptmt = connection.prepareStatement(queryString); 
       ptmt.setString(1, email); 
       resultSet = ptmt.executeQuery(); 
       resultSet.next(); 
       user.setEmail(resultSet.getString("EMAIL")); 
       ...      
       user.setSecret3(resultSet.getString("SECRET3")); 
      } catch (SQLException e) { 
       System.out.println(e.getMessage()); 
      } finally { 
      } 
     } 
     if (null != user.getPassword() && user.getPassword().equals(password)) { 
      request.setAttribute("user",user); 
      request.getRequestDispatcher("comments.jsp").forward(request, response); 
//   response.sendRedirect("comments.jsp"); 

     } 

comments.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Comments</title> 
    </head> 
    <body> 
     <sql:setDataSource var="dataSource" driver="org.apache.derby.jdbc.ClientDriver" 
          url="jdbc:derby://localhost:1527/mp1" 
          user="app" password="app"/> 

     <sql:setDataSource var="dataSource2" driver="org.apache.derby.jdbc.ClientDriver" 
          url="jdbc:derby://localhost:1527/mp1" 
          user="app" password="app"/> 

     <H2>Comments</H2> 

     <sql:query dataSource="${dataSource}" sql="SELECT * FROM COMMENTS" var="comments" /> 
     <table border=1> 
      <c:forEach var="row" items="${comments.rows}"> 
       <tr> 
        <c:forEach var="col" items="${row}"> 
         <td><c:out value="${col.value}" /></td> 
        </c:forEach> 
       </tr> 
      </c:forEach> 
     </table> 

     <form method="post"> 
      <table> 
       <tr> 
        <td>Enter Email</td> 
        <td><input type="text" name="EMAIL"></td> 
       </tr> 
       <tr> 
        <td>Enter Comment</td> 
        <td><input type="text" name="COMMENT"></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><input type="submit" value="submit"></td> 
       </tr> 
      </table> 
     </form> 
     <c:if test="${pageContext.request.method=='POST'}"> 
      <c:catch var="exception"> 
       <sql:update dataSource="${dataSource2}" var="updatedTable"> 
        INSERT INTO 
        COMMENTS (EMAIL,COMMENT) 
        VALUES (?, ?) 
        <sql:param value="${param.EMAIL}" /> 
        <sql:param value="${param.COMMENT}" /> 
       </sql:update> 
       <c:if test="${updatedTable>=1}"> 
        <c:redirect url="/comments.jsp"/> 
       </c:if> 
      </c:catch> 
      <c:if test="${exception!=null}"> 
       <c:out value="Unable to add comment." /> 
      </c:if> 
     </c:if> 
    </body> 
</html> 

順便說一句,這是一個家庭作業是老師要我們學習它是如何做舊的方式。因此,使用安全性和更好的技術並不是真正的問題。

PS。我已經通過分離評論和添加評論到單獨的頁面來解決這個問題。也許更好的解決方案是使用會話而不是對象傳輸請求。

+1

這不是答案的一部分,而是一個建議:不要在servlet中定義屬性,它們不會是線程安全的。詳細瞭解servlet如何在這裏工作:[servlet如何工作?實例化,會話變量和多線程](http://stackoverflow.com/a/3106909/1065197) –

+0

你的servlet中最後一條if語句發生了什麼?代碼被切斷。 – Sean

+0

@肖恩,邏輯,指向不同的頁面。因爲它不會被執行,所以不會影響結果。 – jeremyjjbrown

回答

1
  1. 重定向和轉發都應該保留會話,因爲session支持cookie(大多數情況下)。

  2. 您的表單沒有action參數,因此其行爲取決於瀏覽器的當前URL(表單獲取當前URL而不是定義在動作中),當前URL在重定向和轉發時不同。

+0

「重定向和轉發應保留會話」,當然,但我把數據放入請求中,而不是通過重定向保留。 – jeremyjjbrown