java如何使用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>
登录检查。爪哇
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>
# 1 楼答案
我无法解决这个具体问题,但我找到了一个解决方法,实现了我想要做的事情。如果您想了解我是如何修复它的,请查看此链接:https://jelastic.com/blog/restrict-access-tomcat-web-application-hosting/
# 2 楼答案
您使用了一个servlet,但可以使用一个简单的java类,并在jsp页面中声明您的逻辑,但这只是我的想法
我认为问题在于
response.sendRedirect("LoginWorked.jsp");
,我一直使用对象RequestDispatcher将请求重定向到另一个servlet。 jsp页面是servlet,在您的案例中,您有一个三个servlet尝试使用对象RequestDispatcher,因为您的配置似乎正确
# 3 楼答案
我不想陷入这样一种刻板印象,即贬低你的技术选择,但是JSP已经非常过时了。如果您有选择的话,我推荐一些较新的技术,比如RESTAPI和javascript前端优于jsp。查看jackson+jersey中的java API和Angular/React/Vue。前端框架的js。您还可以使用SpringSecurityforAuth,而不必与Web服务器并行运行servlet
我说的都不是假的JSP has been deprecated by Oracle多年来,我们不应该通过创建使用这项技术的新网站来让它永久化。另外,除非OP需要它来完成特定的工作,否则他们最好花时间学习更常用的技术