我试图让站点上的用户使用正确的凭据登录,然后才能访问我的网页,所以我创建了一个简单的servlet登录来实现这一点。在Eclipse中我的web项目中,它工作得很好,但是当我尝试在我的Amazon实例上运行它并输入凭据时,我得到了这个错误: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>发布于 2019-06-15 05:25:36
您已经使用了servlet,但是您可以使用一个简单的java类,并在jsp页面中声明您的逻辑,但这只是我的想法。
我认为问题出在response.sendRedirect("LoginWorked.jsp");上,我一直使用对象RequestDispatcher将请求重定向到另一个servlet。页面jsp是servlet对于您来说,您有一个三个servlet
请尝试使用对象RequestDispatcher,因为您的配置似乎是正确的
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/LoginFailed.jsp");
try {
requestDispatcher.forward(request, response);
} catch (ServletException ex) {
ex.printStackTrace();
}发布于 2019-06-18 01:22:27
我不能解决这个特定的问题,但我找到了一种解决方法,它实现了我想要做的事情。如果你想知道我是如何解决这个问题的,可以看看这个链接:https://jelastic.com/blog/restrict-access-tomcat-web-application-hosting/
发布于 2019-06-15 04:38:31
我不想陷入这样的老套,认为你的技术选择是垃圾,但是JSP已经非常过时了。如果您有选择,我会推荐一些较新的技术,比如在jsp上使用javascript前端的REST API。查看java的jackson+jersey和前端框架的Angular/React/Vue.js。您还可以使用Spring安全进行身份验证,而不必运行与all服务器并行的servlet!
我说的都是假的。JSP has been deprecated by Oracle已经有很多年了,我们不应该通过创建使用这项技术的新网站来延续它。此外,除非OP在特定工作中需要它,否则他们可以更好地将时间花在学习更常用的技术上。
https://stackoverflow.com/questions/56604806
复制相似问题