如何使用 JSP Servlet 登录设置 Tomcat 服务器



我试图让站点上的用户在能够访问我的网页之前使用正确的凭据登录,因此我创建了一个简单的 servlet 登录来完成此操作。在 Eclipse 中的 Web 项目中,它运行良好,但是当我尝试在我的 Amazon ec2 实例上运行它并输入凭证时,我收到此错误: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.我认为这与我的 LoginCheck Java 类和我的 web.xml 文件的配置有关,因为在我添加 servlet 登录之前,我的站点可以正常工作。有什么建议吗?

索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>AWS Ads Updater</title>
    </head> 
    <body>
         <form method="post" action="LoginCheck">
             <table>
                 <tr><td>Username</td><td><input type="text" name="uname"></td></tr>
                 <tr><td>Password</td><td><input type="password" name="password"></td></tr>
                 <tr><td></td><td><input type="submit" name="login"></td></tr>
            </table>
        </form>
    </body>
</html>

登录检查.java

    package LoginCheck.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class LoginCheck
 */
@WebServlet("/LoginCheck")
public class LoginCheck extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginCheck() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname = request.getParameter("uname");
        String pass = request.getParameter("password");
        if(uname.equals("user") && pass.equals("pass")) {
            response.sendRedirect("LoginWorked.jsp");
        } else {
            response.sendRedirect("LoginFailed.jsp");
        }
    }
}

登录工作.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
       <meta charset="ISO-8859-1">
       <title>AWS Ads Updater</title>
    </head> 
    <body>
         <p>Login worked</p>
     </body>
</html>

登录失败.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>AWS Ads Updater</title>
    </head> 
    <body>
        <p>Login failed, please try again</p>
    </body>
</html>

网络.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>LoginCheck</display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

你用了一个servlet,但你可以使用一个简单的Java类,你在jsp页中声明你的逻辑,但这只是我的想法。

我认为问题出在response.sendRedirect("LoginWorked.jsp");,我一直使用对象 RequestDispatcher 将请求重定向到另一个 servlet。页面 jsp 是 servlet 进入你的情况,你有一个三个 servlet

  1. 登录检查.java
  2. LoginFailed_jsp.java
  3. LoginWorked_jsp.java

尝试使用对象请求调度程序,因为您的配置似乎正确

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/LoginFailed.jsp");
try {
   requestDispatcher.forward(request, response);
} catch (ServletException ex) {
  ex.printStackTrace();
}

我无法弄清楚这个特定问题,但我找到了一种解决方法,可以完成我想做的事情。如果您想了解我如何修复它,请查看此链接:https://jelastic.com/blog/restrict-access-tomcat-web-application-hosting/

我讨厌陷入破坏你的技术选择的刻板印象,但 JSP 已经过时了。如果你有选择,我会推荐一些更新的技术,比如在jsp上带有javascript前端的REST API。查看 jackson+jersey 的 java API 和 Angular/React/Vue.js 的前端框架。你也可以使用 Spring 安全性进行身份验证,而根本不需要与你的 Web 服务器并行运行 servlet!

我说的一切都不是假的。JSP已经被Oracle弃用多年,我们不应该通过创建使用该技术的新站点来延续它。另外,除非OP需要它来完成特定的工作,否则他们可以更好地花时间学习更常用的技术。

最新更新