酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Java实现登录验证:编写LoginServlet并返回JSON结果

```java import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置响应内容类型为JSON response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); // 获取用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 假设正确的用户名和密码是admin和123456 String correctUsername = "admin"; String correctPassword = "123456"; // 创建输出流 PrintWriter out = response.getWriter(); // 判断用户名和密码是否正确 if (correctUsername.equals(username) && correctPassword.equals(password)) { // 登录成功,返回成功的JSON数据 out.print("{code:1, msg:'success'}"); } else { // 登录失败,返回失败的JSON数据 out.print("{code:0, msg:'failure'}"); } // 关闭输出流 out.flush(); out.close(); } } ``` ######[AI写代码神器 | 329点数解答 | 2024-11-05 16:36:31]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]