从 servlet 重定向到 html 时出现 404 错误
Posted
技术标签:
【中文标题】从 servlet 重定向到 html 时出现 404 错误【英文标题】:404 error while redirecting to html from a servlet 【发布时间】:2019-04-23 23:38:21 【问题描述】:我有一个学校项目的问题,它是一个小的网络动态项目。 在登录页面中,如果我提交数据库中存在的用户的参数,一切顺利并且我有我的“success.html”页面,但是如果我提交不在我的数据库中的用户的参数,我有一个 404页面,而我期待我的“failure.html”。 你能帮我找出错误吗?
我正在使用:
Eclipse Java 霓虹灯 Tomcat 9.0.12这是我的登录表单:
<form id="login" class="form-horizontal" action="/IROPLAS/Controller/LoginServlet" method="GET">
<div class="form-group">
<label class="control-label col-xs-8 col-sm-4 col-md-5" for="mail">E-mail:</label>
<div class="col-xs-10 col-sm-8 col-md-4">
<input type="text" class="form-control" id="mail" name="mail" placeholder="E-mail">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-4 col-md-5" for="pw">Password:</label>
<div class="col-xs-10 col-sm-8 col-md-4">
<input type="password" class="form-control" id="pw" name="pw" placeholder="Password">
</div>
</div>
<br>
<button type="submit" class="btn btn-default"id="entra">Entra</button>
</form>
这是我的 Servlet:
package Controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
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 LoginServlet
*/
@WebServlet("/IROPLAS/Controller/LoginServlet")
public class LoginServlet extends HttpServlet
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public LoginServlet()
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
RequestDispatcher requestDispatcher;
String m=request.getParameter("mail");
String p=request.getParameter("pw");
if(Model.BUtente.validate(m, p))
Model.BUtente u=Model.BUtente.getUtente(m, p);
request.setAttribute("utente", u);
requestDispatcher=request.getRequestDispatcher("/Success.html");
requestDispatcher.forward(request, response);
//pagina profilo utente
else
PrintWriter pw=response.getWriter();
response.sendRedirect(request.getContextPath()+"/Failure.html");
pw.close();
//errore
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
doGet(request, response);
Model.BUtente.validate(mail,pw) 如果用户(使用电子邮件=mail 和密码=pw)是我的数据库的记录,则返回 true,否则返回 false,I'已经测试过了,效果很好。
这些是我的 Success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Success
</body>
</html>
还有我的 Failure.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Fail
</body>
</html>
【问题讨论】:
【参考方案1】:我做到了! 如果你有同样的问题,我在这里发布我的解决方案:
我的登录表单:
<form id="login" class="form-horizontal" action="LoginServlet" method="GET">
<div class="form-group">
<label class="control-label col-xs-8 col-sm-4 col-md-5" for="mail">E-mail:</label>
<div class="col-xs-10 col-sm-8 col-md-4">
<input type="text" class="form-control" id="mail" name="mail" placeholder="E-mail">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-4 col-md-5" for="pw">Password:</label>
<div class="col-xs-10 col-sm-8 col-md-4">
<input type="password" class="form-control" id="pw" name="pw" placeholder="Password">
</div>
</div>
<br>
<button type="submit" class="btn btn-default"id="entra">Entra</button>
</form>
我的 Servlet:
package Controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
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 LoginServlet
*/
//@WebServlet("/IROPLAS/Controller/LoginServlet")
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public LoginServlet()
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
RequestDispatcher requestDispatcher;
String m=request.getParameter("mail");
String p=request.getParameter("pw");
if(Model.BUtente.validate(m, p))
Model.BUtente u=Model.BUtente.getUtente(m, p);
request.setAttribute("utente", u);
requestDispatcher=request.getRequestDispatcher("/Success.html");
requestDispatcher.forward(request, response);
//pagina profilo utente
else //404 not found
PrintWriter pw=response.getWriter();
response.sendRedirect("Failure.html");
pw.close();
//errore
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
doGet(request, response);
【讨论】:
以上是关于从 servlet 重定向到 html 时出现 404 错误的主要内容,如果未能解决你的问题,请参考以下文章
访问本地托管的 Wordpress 站点时出现问题(重定向+其他废话)