我在一个学校项目上有问题,这是一个小型的网络动态项目。 在“登录”页面中,如果我提交数据库中存在的用户参数,一切顺利,并且我有“ success.html”页面,但是,如果我提交不在数据库中的用户参数,我将得到404页,而我希望输入“ failure.html”。 您能帮我找到错误吗?
我正在使用:
这是我的登录表格:
<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)如果用户(使用电子邮件=邮件和密码= pw)是我的数据库的记录,则返回true,否则返回false,进行了测试,效果很好。
这些是我的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>
答案 0 :(得分:0)
我做到了! 如果您遇到相同的问题,我将在此处发布我的解决方案:
我的登录表单:
<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);
}
}