2016-09-19 62 views
-1

我剛剛介紹給JSP 3天前,所以我還不是很熟悉它。JSP:喚起java代碼

所以我有這樣的代碼在這裏:

<body> 
 
     <form method="post"> 
 
      <center> 
 
      <table border="1" cellpadding="5" cellspacing="2"> 
 
       <thead> 
 
        <tr> 
 
         <th colspan="2">Login Here</th> 
 
        </tr> 
 
       </thead> 
 
       <tbody> 
 
        <tr> 
 
         <td>Username</td> 
 
         <td><input type="text" name="username" required/></td> 
 
        </tr> 
 
        <tr> 
 
         <td>Password</td> 
 
         <td><input type="password" name="password" required/></td> 
 
        </tr> 
 
        <tr> 
 
         <td colspan="2" align="center"><input type="submit" value="Login" /> 
 
          &nbsp;&nbsp; 
 
          <input type="reset" value="Reset" /> 
 
         </td>       
 
        </tr>      
 
       </tbody> 
 
      </table> 
 
      </center> 
 
     </form>

注:此代碼是從互聯網上借來的。一旦完成將它連接到mySQL,我會改變它。致拉哈爾賽斯。

所以我想知道如何喚起下面的代碼。

String username = request.getParameter("username"); 
String password = request.getParameter("password"); 

if(Login.LoginA(username, password).equals("Admin")){ 
     out.println("Admin"); 
}else if(Login.LoginA(username, password).equals("Employee")){ 
     out.println("Employee"); 
}else{ 
    out.println("Unidentified User"); 
} 

我想我必須做一些事情,在html部分登錄按鈕,但我不知道如何。希望你們能幫助我。 謝謝!

+0

投入一些時間閱讀[教程](http://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html)。 –

+0

我試過了,但我仍然無法很好地理解它。我認爲如果我對它的實際工作有了一個想法,我會更好地理解這個教程。所以我問。 – InanimateObject

回答

0

請加上窗體標籤的action屬性如下:

<form method="post" action="LoginServlet"> 
      <center> 
      <table border="1" cellpadding="5" cellspacing="2"> 
       <thead> 
        <tr> 
         <th colspan="2">Login Here</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
         <td>Username</td> 
         <td><input type="text" name="username" required/></td> 
        </tr> 
        <tr> 
         <td>Password</td> 
         <td><input type="password" name="password" required/></td> 
        </tr> 
        <tr> 
         <td colspan="2" align="center"><input type="submit" value="Login" /> 
          &nbsp;&nbsp; 
          <input type="reset" value="Reset" /> 
         </td>       
        </tr>      
       </tbody> 
      </table> 
      </center> 
     </form> 

創建一個servlet的提到像下面這樣

@WebServlet("/LoginServlet") 
public class LoginServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public LoginServlet() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 

    String user_name =request.getParameter("username"); 
    String password =request.getParameter("password"); 
    System.out.println("Username -> "+user_name +" Password --> "+password); 
    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     doGet(request, response); 
    } 

} 
提到它會讀取基於輸入標籤的name屬性的用戶名和密碼
+0

如果你想解釋他如何使用Servlet,你應該添加更多的信息。 – AxelH

+1

謝謝先生!我會盡我所能瞭解這一點。 – InanimateObject