繁体   English   中英

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

[英]How to set up Tomcat server with JSP Servlet Login

我试图使用户能够使用正确的凭据登录我的网站,然后才能访问我的网页,因此我创建了一个简单的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登录名之前,我的网站可以正常工作。 有什么建议么?

index.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>

LoginCheck.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");
        }

    }

}

LoginWorked.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>

LoginFailed.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>

web.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,在您有3个servlet的情况下

  1. LoginCheck.java
  2. LoginFailed_jsp.java
  3. LoginWorked_jsp.java

尝试使用对象RequestDispatcher,因为您的配置似乎正确

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/

我讨厌陷入浪费您的技术选择的SO刻板印象,但是JSP太过时了。 如果您可以选择的话,我建议您使用一些较新的技术,例如在jsp上具有javascript前端的REST API。 查看用于Java API的jackson + jersey和用于前端框架的Angular / React / Vue.js。 您还可以使用Spring安全性进行身份验证,而不必与Web服务器并行运行servlet!

我说的没错。 甲骨文已经弃用JSP多年了,我们不应该通过创建使用该技术的新站点来使其永存。 另外,除非OP需要将其用于特定工作,否则他们可以更好地花时间学习更常用的技术。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM