2011-06-24 61 views
-1
<%@ page import="java.util.*" %> 
<html> 
<head> 
</head> 
<body> 
<form name="loginform" method="post" action="WelcomeServlet"> 
<br><br> 
    <table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table> 
    <table width="300px" align="center" style="border:1px solid #000000;backgroundcolor:#efefef;"> 
     <tr><td colspan=2></td></tr> 
     <tr><td colspan=2>&nbsp;</td></tr> 
    <tr> 
    <td><b>Login Name</b></td> 
    <td><input type="text" name="username" ></td> 
    </tr> 
    <tr> 
    <td><b>Password</b></td> 
    <td><input type="password" name="password"></td> 
    </tr> 

    <tr> 
    <td></td> 
    <td><input type="submit" name="Submit" value="Submit"></td> 
    </tr> 
    <tr><td colspan=2>&nbsp;</td></tr> 
    </table><br><br></br></br> 
    <p> 
    <% 
    String mess=""; 
    mess=(String)request.getAttribute("Message"); 
    out.println("="+mess); 
    %> 
    </p> 
    </form> 
</body> 
</html> 

然後這個servlet空響應由小服務程序發送到jsp的

import java.io.*; 
import java.util.*; 
    //import java.io.PrintWriter; 
    import javax.servlet.*; 
    //import javax.servlet.ServletConfig; 
//import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class WelcomeServlet extends HttpServlet { 
/* 
@Override 
public void init(ServletConfig config) throws ServletException { 
    super.init(config); 
} 
*/ 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
/* 
    * Get the value of form parameter 
*/ 
try 
{ 
    response.setContentType("text/html"); 

    PrintWriter out = response.getWriter(); 
    //out.println("I am on welcome servlet..."); 
    String username = request.getParameter("username"); 
    String password =request.getParameter("password"); 
    System.out.println("username="+username); 
    System.out.println("password="+password); 
    System.out.println("response="+password); 
    String welcomeMessage=""; 
    out.println("<html>"); 
    out.println("<head>"); 
    out.println("<title> A very simple servlet example</title>"); 
    out.println("</head>"); 
     out.println("<body>"); 
    if(username.equals("")&& username.equals("null")&& password.equals("")&& password.equals("null")) 
     { 
     welcomeMessage="username or password can not be blanked...."; 
     out.println("<h1>"+welcomeMessage+"<h1>"); 
     }else{  
      if((username.equals("kiran"))&&(password.equals("kiran"))) 
      { 

      welcomeMessage = "Welcome "+username+" thanks for login..."; 
      out.println("<h1>"+welcomeMessage+"</h1>"); 

      }else 
       { 

      welcomeMessage="You are not the valid user..."; 
      out.println("<h1>"+welcomeMessage+"</h1>"); 


       } 

      } 
     out.println("</body>"); 
     out.println("</html>"); 
     RequestDispatcher rd = request.getRequestDispatcher("/login.jsp"); 
     rd.forward(request, response); 
     request.setAttribute("Message",welcomeMessage); 
     //out.close(); 

} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
    //throw e; 
} 
    } 

    public void destroy() { 
    } 
} 
在這個MVC IM MVC ..please執行後獲得空

給出的解決方案儘早

+1

'請儘快給出解決方案......好吧,那不好,是嗎? – Jacob

+0

請爲制定您的問題付出一些努力。格式化,剝離不相關的位。 – Bozho

+0

難道你不知道在你之前的問題中,「servlet不應該用於輸出HTML」嗎? http://stackoverflow.com/questions/6452537/servlet-send-response-to-jsp你目前的做法只會導致完全無效的HTML或可能只是在服務器日誌中的IllegalStateException和客戶端的空白頁面。 – BalusC

回答

2

前設置你的屬性轉發

request.setAttribute("Message",welcomeMessage); 
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp"); 
rd.forward(request, response); 
+0

如果你喜歡答案,那麼你可以接受它,通過點擊正確的標誌或你可以upvote它 – mahesh

相關問題