简体   繁体   English

JSP:调用java代码

[英]JSP : Evoking a java code

I was just introduced to JSP 3 days ago so I'm still not very familiar with it.我 3 天前才刚接触 JSP,所以我对它还不是很熟悉。

So I have this code here :所以我在这里有这个代码:

 <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>

Note : This code was borrowed from the internet.注:此代码是从网上借来的。 I'll be changing it once I'm done with connecting this to mySQL.完成将它连接到 mySQL 后,我将对其进行更改。 Credits to Lahaul Seth.感谢 Lahaul Seth。

So I was wondering how I can evoke the code below.所以我想知道如何唤起下面的代码。

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");
}

I think I have to do something with the login button in the html part, but I have no idea how.我想我必须对 html 部分中的登录按钮做一些事情,但我不知道如何做。 Hope you guys can help me.希望你们能帮帮我。 Thank you!谢谢!

Please add a action attribute on form tag as below :请在表单标签上添加一个 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>

create one servlet mention like this which will read the username and password based on name attribute of input tag as mentioned below创建一个这样的 servlet 提及,它将根据输入标签的 name 属性读取用户名和密码,如下所述

@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);
    }

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM