且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java Servlet登录页面程序

更新时间:2023-12-03 22:57:52

服务器不应该验证名称&密码?我想是的。



所以让服务器回答一个合适的返回值:

如果是真/通过你应该授予访问,如果是假/没通过,你应该出示另一页。





编辑:



所以你不要做服务器请求。那么你也不应该使用HTTPServlet。



Shouldn''t the server validate the name & password? I guess so.

So let the server answer with a suitable return value:
In case of true / "pass" you should grant access, in case of false / "no pass" you should present another page.




So you do NOT do Server-Requests. Then you should also not use a HTTPServlet.

public class SimpleServlet extends GenericServlet {

 public void service(ServletRequest request, ServletResponse response)
        throws ServletException, IOException {

    // figure values from client's request
    String strParam = request.getParameter("param");
    String strNick  = request.getParamter("nick");
    String strPW  = request.getParamter("pw");

    if("login".equals(strParam) ){ // ask first, cause strNick & strPW could be NULL
        // present login
    }
    else if("dolly".equals(strNick) && "dolly".equals(strPW) ){ // login values fine
        response.getWriter().write("<html><body>Login granted</body></html>");
    }
    else { // login not granted
        response.getWriter().write("<html><body>OOOPS!</body></html>");
    }
  }
}





您只需提供登录(就像您现在在代码中一样) )点击登录调用内部函数检查静态缺口/密码。

之后,你出现



You simply present login (as you do in your code now) and call an internal function on click "login" to check the static nick/pw.
After that, you present