doGet()无法从模型中获取任何数据

时间:2016-09-23 05:33:13

标签: java servlets

我有一个简单的servlet应用程序。在servlet类中,doPost()doGet()基本上从模型类中获取相同的数据,但是doGet()无法获取任何数据。

public class ClickerServlet extends HttpServlet {

    ClickerModel clickerModel = new ClickerModel();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();

        if (servletPath.equals("/submit")) {
            doPost(request, response);
        } else if (servletPath.equals("/getResults")) {
            doGet(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String answer = request.getParameter("answer");
        clickerModel.addAnswer(answer);

        request.setAttribute("recentAnswer", clickerModel.getRecentAnswer());
        request.getRequestDispatcher("submit.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("ok" + clickerModel.getRecentAnswer());
        request.setAttribute("clickerModel", clickerModel);
        request.getRequestDispatcher("results.jsp").forward(request, response);
    }
}

我的ClickerModel课程:

public class ClickerModel {

    private String recentAnswer;
    private HashMap<String, Integer> answers;

    public ClickerModel() {
        recentAnswer = "";
        answers = new HashMap<>();
    }

    public void addAnswer(String answer) {
        recentAnswer = answer;

        if (answers.containsKey(answer)) {
            answers.put(answer, answers.get(answer) + 1);
        } else {
            answers.put(answer, 1);
        }
    }

    public void clearAnswers() {
        answers = new HashMap<>();
    }

    public HashMap<String, Integer> getAnswers() {
        return answers;
    }

    public String getRecentAnswer() {
        return recentAnswer;
    }
}

任何人都知道为什么我无法使用doGet()从模型中获取任何数据?谢谢!

了解更多信息:

从表单提交中调用

doPost()

<form action="submit" method="post">
    <input type="radio" name="answer" value="A"> A<br>
    <input type="radio" name="answer" value="B"> B<br>
    <input type="radio" name="answer" value="C"> C<br>
    <input type="radio" name="answer" value="D"> D<br>
    <br><input type="submit" value="submit">
</form>
当我直接粘贴并点击浏览器中的网址(doGet())时,会调用

localhost:something/getResults

我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Results</servlet-name>
        <servlet-class>Clicker.ClickerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Results</servlet-name>
        <url-pattern>/getResults</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Submit</servlet-name>
        <servlet-class>Clicker.ClickerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Submit</servlet-name>
        <url-pattern>/submit</url-pattern>
    </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:0)

我的理解是,你将doGet()方法放在Results servlet中,与提交一个不同。

提交Servlet:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String answer = request.getParameter("answer");
        clickerModel.addAnswer(answer);

        request.setAttribute("recentAnswer", clickerModel.getRecentAnswer());
        //request.getRequestDispatcher("submit.jsp").forward(request, response);  
    response.sendRedirect("getResults"); //   <---After submit will go to this page. 
    }

结果Servlet:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("ok" + clickerModel.getRecentAnswer());
    request.setAttribute("clickerModel", clickerModel);
    request.getRequestDispatcher("results.jsp").forward(request, response);
}