在servlet(JavaEE)中读取电子邮件和密码



我正在开发一个简单的登录servlet,用户在其中输入存储在文本文件中的电子邮件和密码(我不能使用数据库),如果凭据正确,则用户将被重定向到weclome jsp页面。如果不正确,则重定向到失败登录jsp页面,我面临的问题是,我总是被重定向到登录失败页面,即使我在检查电子邮件和密码是否在文本文件中并且是正确的

我正在使用Tomcat 10

所以文本文件看起来像这样:

  • example1@gmail.com,令人敬畏100
  • example2@hotmail.hotmail.com,冷却009

电子邮件和密码由","分隔

这是代码:

package com.example.try8;
import java.io.*;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -5498866193863633001L;
/**
* HashMap to store all users credentials
*/
private final Map<String, String> credentialsPairs = new HashMap<> 
();
@Override
public void init(ServletConfig config) throws ServletException {
String delimiter = ",";
String line = "";
/**
* Credentials file will be there in WEB-INF directory as it 
provide secured
* access only.
*/
String credentialFile = "/WEB-INF/accounts.txt";
/**
* Read the file and prepare Map with username as key and 
password as value We
* have put this code in init method as it is called once only 
that will avoid
* overhead of iterating values from file for each request
*/
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
ServletContext context = config.getServletContext();
try {
/**
* Open stream of file
*/
is = context.getResourceAsStream(credentialFile);
if (is != null) {
/**
* Read the file line by line and store email as a key 
and password as value
*/
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
String[] credentials = line.split(delimiter);
// credentials[0] is email and credentials[1] is 
password
credentialsPairs.put(credentials[0], 
credentials[1]);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void doGet(HttpServletRequest request, HttpServletResponse 
response) throws IOException {
/**
* Get user entered credentials
*/
String userEmail = request.getParameter("email");
String userPassword = request.getParameter("password");
PrintWriter out = response.getWriter();
boolean isValidUser = false;
/**
* Get value from Map for user entered email address.
*/
String password = credentialsPairs.get(userEmail);
/**
* If User with entered email address found then we will get  
password for that
* user
*/
if (password != null) {
/**
* Compare password entered by user with one that is 
retrieved from file
*/
if (password.equals(userPassword)) {
isValidUser = true;
} else {
// Please enter correct password
out.println("Please enter correct password");
}
} else {
// User does not exists
out.println("User does not exist");
}
if (isValidUser) {
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("fail.jsp");
}
}
public void destroy() {
/**
* Free up the map
*/
credentialsPairs.clear();
}
}

index.jsp:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF- 
8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<div align="center">
<h1>
Sign-In
</h1>
<form action="login" method="get">
<table>
<tr> <td>E-mail : <input type="email" name="email"></td> 
</tr>
<tr> <td>Password : <input type="password" name="password"> 
</td></tr>
<tr> <td><input type="submit" value="Login"></td></tr>
</table>
</form>
</div>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee 
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<servlet>
<servlet-name>lgn</servlet-name>
<servlet-class>com.example.try8.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>lgn</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>

你好@Mohammed Hamdoon

您可以在下面找到代码。这符合您的目标,并消除了每次迭代文件以匹配电子邮件和密码的开销。在这里,我可以使用accounts.txt文件中提供的凭据成功登录。

请阅读我在servlet文件中的评论以获得进一步的解释

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
/**
* 
*/
private static final long serialVersionUID = -5498866193863633001L;
/**
* HashMap to store all users credentials
*/
private final Map<String, String> credentialsPairs = new HashMap<>();
@Override
public void init(ServletConfig config) throws ServletException {
String delimiter = ",";
String line = "";
/**
* Credentials file will be there in WEB-INF directory as it provide secured
* access only.
*/
String credentialFile = "/WEB-INF/accounts.txt";
/**
* Read the file and prepare Map with username as key and password as value We
* have put this code in init method as it is called once only that will avoid
* overhead of iterating values from file for each request
*/
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
ServletContext context = config.getServletContext();
try {
/**
* Open stream of file
*/
is = context.getResourceAsStream(credentialFile);
if (is != null) {
/**
* Read the file line by line and store email as a key and password as value
*/
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
String[] credentials = line.split(delimiter);
// credentials[0] is email and credentials[1] is password
credentialsPairs.put(credentials[0], credentials[1]);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
/**
* Get user entered credentials
*/
String userEmail = request.getParameter("email");
String userPassword = request.getParameter("password");
boolean isValidUser = false;
/**
* Get value from Map for user entered email address.
*/
String password = credentialsPairs.get(userEmail);
/**
* If User with entered email address found then we will get password for that
* user
*/
if (password != null) {
/**
* Compare password entered by user with one that is retrieved from file
*/
if (password.equals(userPassword)) {
isValidUser = true;
} else {
// Please enter correct password
}
} else {
// User does not exists
}
if (isValidUser) {
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("fail.jsp");
}
}
public void destroy() {
/**
* Free up the map
*/
credentialsPairs.clear();
}
}

如果您对以上内容有任何疑问,请分享您的意见。

相关内容

  • 没有找到相关文章

最新更新