gpt4 book ai didi

Java servlet doPost 方法不起作用

转载 作者:行者123 更新时间:2023-11-28 00:45:35 25 4
gpt4 key购买 nike

编辑:

我将发布请求指向/vault/Login 但 servlet 在/vault/index 和 vault/login当将它指向索引时它起作用了。

我正在尝试制作一个登录系统。我有一个 html 文件,从中请求了一个 post 方法,但是在请求时永远不会触发 doPost 方法。单击提交按钮时,url 随其中的参数发生变化,但没有任何反应。每次执行 doPost() 时,第一条语句都会向控制台写入一些内容,但两者都没有发生。

public class Login extends HttpServlet {



protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html;charset=UTF-8");
System.out.println("fired");
HttpSession session = request.getSession();
session.setAttribute("loginFailed", false);


RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
rd.forward(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("this is another test");


HttpSession session = request.getSession();

String username = (String)session.getAttribute("username");
String password = (String)session.getAttribute("password");
System.out.println(username + " " + password);
boolean succes = false;

if (!"".equals(username) && !"".equals(password)){
try {
succes = Authentication.checkCredentials(username, password);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}

switch ((String)session.getAttribute("platform")){
case "browser":
RequestDispatcher rd = request.getRequestDispatcher("Lobby.jsp");
rd.forward(request, response);
break;
case "desktop":

int id = Tracker.getIdByUsername(username);
List vaults = (List)VaultManagement.getVaultsByUserId(id);

int[] vaultIds = new int[vaults.size()];

if (vaults.isEmpty()){
vaultIds[0] = -1;
}else{
int x = 0;
for (Object vault : vaults){
Vault v = (Vault)vault;
vaultIds[x] = v.getId();

x++;
}
}
DAL.Entities.Account account = AccountManagement.getAccountByUsername(username);
LoginPackage pack = new LoginPackage(username, password, account.getEmail(), vaultIds);
String json = JSON.dataPackageToJson(pack);
PrintWriter writer = response.getWriter();
writer.print(json);


break;
}

}

@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

发布

<div class="jumbotron" width="10%">
<form id="form" action="/vault/Login.jsp">
<h6>Username:</h6>
<input type="text" class="form-control" id="username" name="username">
<h6>Password:</h6>
<input type="password" class="form-control" id="password" name="password"><br>
<button type="button" onclick="location.href='vault/Register.jsp'" class="btn btn-secondary">Register</button>
<input type="hidden" name="platform" value="browser"/>
<input type="submit" value="Login"/>
</form>



</div>

最佳答案

您忘记将 method 属性指定为 post:

<form id="form"  action="/vault/Login.jsp" method="post">
<!-- Here ---------------------------------^ -->

关于Java servlet doPost 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50774723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com