2013-05-28 59 views
-1

以下是我的兩個servlet,我想通過我的會議。會議傳球失敗 - Servlet的

問題是,當其導航到SuccessPage servlet的,但不是它要失敗Servlet時傳球完成了會議。

登錄的servlet doGet()方法:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  
ServletException, IOException{ 
    PrintWriter out=response.getWriter(); 
    response.setContentType("text/html"); 
    String userName=request.getParameter("userName"); 
    String userPass=request.getParameter("userPassword"); 
    String userRePass=request.getParameter("userRePassword"); 
    try{ 
     String query="Select VendorName from vendorinfo where VendorPass=?"; 
     connection1=Connection_Class.getInstance().getConnection(); 
     ptmt=connection1.prepareStatement(query); 
     ptmt.setString(1,userPass); 
     rs=ptmt.executeQuery(); 
     if(rs.next()&& userName.equalsIgnoreCase(rs.getString("VendorName"))){ 
     HttpSession session=request.getSession(true); 
     session.setAttribute("loggedVendor",rs.getString(1)); 
     //this is working fine...im able to get the userName in the next servlet 
     ServletContext context = getServletContext(); 
     RequestDispatcher dispatcher=context.getRequestDispatcher("/SuccessPage"); 
     dispatcher.forward(request,response); 
     } 
     else{ //this is not working .....whats the problem here ? 
      request.setAttribute("wrongUser",userName); 
      ServletContext context=getServletContext(); 
      RequestDispatcher dispatcher=context.getRequestDispatcher("/Failure"); 
      dispatcher.forward(request,response); 
     } 
    } 
    catch(SQLException e){ 
     e.printStackTrace(); 
    } 
} 

失敗的servlet doGet()方法:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException { 
/*all i want to do here is that I want to get the userName from the previous servlet but its 
not displaying that and its displaying null */ 
    response.setContentType("text/html"); 
    PrintWriter out=response.getWriter(); 
    out.println("<body bgcolor=#F3EEF0><h1>"); 
    out.println("<center>"); 
    HttpSession session=request.getSession(false); 
    out.println("This is a failure page"); 
    out.println(session.getAttribute("wrongUser")); 
    out.println("</center></h1></body>"); 
} 

有什麼不對的代碼?

回答

1

您在申請使用鍵「wrongUser」將數據放置,而不是會議中的第一個servlet:

request.setAttribute("wrongUser",userName); 

,並在失敗的Servlet從會話中檢索它:

session.getAttribute("wrongUser"); 

使用無論是在兩個地方的「會話」還是在兩個地方的「請求」。所以如果你使用request.setAttribute()使用request.getAttribute()。如果使用session.setAttribute(),請使用session.getAttribute()。

建議:使用要求,這樣你就不能啓動了很多不必要的任意加載會話。您不需要超出此請求/響應週期範圍的此值。