当我是doPost()末尾的ConfirmServlet时,它应该打开我的ResultServlet。但相反,链接保持/确认结束,我得到一个空白页面。任何人都可以帮我解决我的代码错误。
ConfirmServlet
package Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Bean.SurveyBean;
import DAO.DriverLoader;
import DAO.SurveyDAO;
import Service.SurveyService;
@WebServlet(value = "/Confirm", initParams = {
@WebInitParam(name = "Confirmpage", value = "/WEB-INF/pages/Confirm.jsp"),
@WebInitParam(name = "ResultURL", value = "Result") })
public class ConfirmServlet extends HttpServlet {
private String Confirmpage;
private String ResultURL;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
new DriverLoader();
Confirmpage = getInitParameter("Confirmpage");
ResultURL = getInitParameter(ResultURL);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
SurveyBean bean = new SurveyBean();
HttpSession sess = req.getSession();
bean = (SurveyBean) sess.getAttribute("bean");
req.setAttribute("bean", bean);
System.out.println(bean.getName() + " " + bean.getAnswer3() + " haha");
System.out.println("Testing");
req.getRequestDispatcher(Confirmpage).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Post Werkt");
HttpSession sess = req.getSession();
SurveyBean bean = new SurveyBean();
bean = (SurveyBean) sess.getAttribute("bean");
SurveyDAO Dao;
Dao = new SurveyDAO("jdbc:mysql://localhost/survey", "root", "");
Dao.postBean(bean);
resp.sendRedirect(ResultURL);
System.out.println("Hier moet ik ook komen!!!");
}
}
ResultServlet
package Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Bean.SurveyBean;
@WebServlet(value = "/Result", initParams = {
@WebInitParam(name = "ResultPage", value = "/WEB-INF/pages/Result.jsp"),
@WebInitParam(name = "IndexURL", value = "IndexURL") })
public class ResultServlet extends HttpServlet {
private String ResultPage;
private String IndexURL;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
System.out.println("Result");
super.init();
ResultPage = getInitParameter(ResultPage);
IndexURL = getInitParameter(IndexURL);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Doget Result");
req.getRequestDispatcher(ResultPage).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
resp.sendRedirect(IndexURL);
}
}
顺便说一句,我使用了相同的代码用于之前的路线,只有在这里工作我得到一个错误,我不知道哪一个。
答案 0 :(得分:0)
问题是在这里必须添加“在getInitParameter。在两个Servlet
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
new DriverLoader();
Confirmpage = getInitParameter("Confirmpage");
ResultURL = getInitParameter("ResultURL");
}