gpt4 book ai didi

用于获取响应并根据 servlet 的响应进行操作的 JavaScript 代码

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:47 24 4
gpt4 key购买 nike

我制作了一个登录表单,它会比较从文本文件输入的用户名和密码,然后显示用户名/密码是否不正确或是否允许访问。

    <form role="form" action="/traveositelogin" method="post">
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input type="text" placeholder="Username..." class="form-username form-control" id="username">
<p id="UsernameIncorrect" hidden>Incorrect Username. Try Again</p>
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input type="password" placeholder="Password..." class="form-password form-control" id="password">
<P id="PasswordIncorrect" hidden>Incorrect Password. Try Again.</p>
</div>
<button type="submit" class="btn" id="Login">Login</button>
<p><a href="contact.html"> Forgot Password?</a></p>

<div class="form-group">
</div>

这是我的 servlet 代码:

package com.traveosoft;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class TraveoWebsiteLoginServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, this is a testing servlet. \n\n");
Properties p = System.getProperties();
p.list(resp.getWriter());

}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String filePath = "{login.txt}";
String username = null;
String password = null;

//resp.setContentType("text/plain");
//resp.getWriter().println("Hello, this is a testing servlet. Post method \n\n");
//Properties p = System.getProperties();
//p.list(resp.getWriter());

if (req.getParameter("Login")!= null )
{
username = req.getParameter("username");
password = req.getParameter("password");
//resp.sendRedirect("index.html");
}
try
{
FileInputStream fstream = new FileInputStream("login.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
int count = 0;

while((strLine = br.readLine())!= null)
{
//Enter userName
String[] creds = strLine.split(",");
System.out.println("creds[0], creds[1]:" + creds[0] + "," +creds[1] );
//System.out.println("User name from the file is:"+ creds[0]);
if (creds[0].equals(username))
{
//user name matches. So we need to check for password now
count++;
if (creds[1].equals (password))
{
//Great... password also matches. Allow the user access to our repository
System.out.println("Allow access");
}
else
{
// Password didn't match. Ask the user to reenter the password
System.out.println("Wrong password. Try Again.");
}
break;
}

//strLine = br.readLine();
//count++;
//Enter Password
//System.out.println("Password from the file is:"+ creds[1]);
}
if (count == 0 )
{
// No user name matched with the user name entered by the user. So user name itself is wrong
System.out.println("Your user name is wrong. Try Again.");
}
fstream.close();
br.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}

}

我怎样才能编写一个javascript代码,当用户名错误时,打印“用户名错误”;密码错误,则打印“密码错误”;如果两者都正确,则允许我将超链接引用放置到保存资源的页面。

我希望使用 javascript 示例代码(而不是 jsp 文件)。我仍在学习这一切,任何帮助将不胜感激。提前致谢。

最佳答案

如果我理解正确的话,你想要一个响应对象(或者说消息)从服务器端直接发送到客户端到 JS,而不是通过 JSP 发送。
为了做到这一点,你需要付出更多的努力......
您可以按照以下步骤操作:
1. 创建响应对象。
2. 将该对象转换为 JSON 格式。
3. 将响应内容类型设置为text/json并设置响应。


//要发送到 JavaScript 的响应对象

            public class ResponseMessage {
private boolean isLoginSuccess;
private String errorMessage;

// Getters and setters ...
}

// Servlet code

ResponseMessage message = new ResponseMessage();
message.setError(true);
message.setErrorMessage("Your error message");
Gson gson = new Gson(); // GSON: library to convert Java object to JSON
String content = gson.toJson(message);
response.setContentType("text/json");
response.getWriter().print(content);

JS代码

function processRequest() {
var result = jQuery.ajax ({
url: "your.url",
method: "post",
async: false,
data: ""
});

// Create JS object from JSON
var message = jQuery.parseJSON(result.responseText);

// Now you can show your message
if(message.isLoginSuccess) {
alert("Welcome");
}else{
alert("Login Failed");
}
}

关于用于获取响应并根据 servlet 的响应进行操作的 JavaScript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44820416/

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