servlet将我重定向到家但是空白页面

时间:2017-04-08 03:44:34

标签: web-services jsp servlets web-applications jstl

为什么doPost会将我重定向到正确的页面,但它会显示一个空白页?

我必须使用不同的jsp文件,这两个文件都有doPost动作,这就是为什么我在主控制器上有if语句的原因。我试过了他们两个,但他们都重定向到url / home,但它显示为空白。

这是控制器的代码

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String mobile = req.getParameter("mobile");

        if(req.getParameter("db")!= null){
        Map<String, Object> record = new HashMap<String, Object>();
        ds = new DataService();

        record.put("name", name);
        record.put("email", email);
        record.put("mobile", mobile);

        if (id == null) {
            ds.updateRecord(DataService.INSERT_RECORD, record);
        } else {
            record.put("_id", Integer.parseInt(id));
            ds.updateRecord(DataService.UPDATE_RECORD, record);
        }
        resp.sendRedirect("home");
        }


        else if(req.getParameter("t2s") != null){
        TextToSpeechService service = new TextToSpeechService();
        String text = req.getParameter("name");
        service.getAudio(text, resp);

这是home.jsp文件

<body>
<h3>Address Book App (Using MySQL)</h3>
<a href="home?action=new">* New Contact</a>
<hr>
<table>
    <thead>
        <th>Name</th>
        <th>Email</th>
        <th>Mobile</th>
        <th></th>
    </thead>
    <tbody>
        <c:forEach items="${contacts}" var="contact">
            <tr>
                <td><c:out value="${contact.name}" /></td>
                <td><c:out value="${contact.email}" /></td>
                <td><c:out value="${contact.mobile}" /></td>
                <td>
                    <a href="home?action=edit&id=<c:out value="${contact._id}" />">Edit</a>
                    <a href="home?action=delete&id=<c:out value="${contact._id}" />">Delete</a>
                </td>
                <td>
                <form action="home" name="t2s" method="POST">
                        <input type="submit" value="Button"/>"></input>
                </form>
            </tr>
        </c:forEach>
    </tbody>
</table>

这是基于@hewittvs

答案的更新的xml文件
  <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <display-name>Watson - Text to Speech</display-name>
    <welcome-file-list>
        <welcome-file>/home</welcome-file>
    </welcome-file-list>
    <servlet>
    <servlet-name>home</servlet-name>
    <jsp-file>/home.jsp</jsp-file>
    </servlet>
</web-app>

1 个答案:

答案 0 :(得分:0)

使用JSP和Servlet时,我遇到了这个空白屏幕问题,而且大多数时候我犯了一些导致运行时异常的错误。请参阅服务器日志以检查是否发生了任何运行时异常。

也许在

resp.sendRedirect("home");

您指定主页而不是 home.jsp 。确保将该URL模式上相应的JSP servlet映射写入webapp的web.xml。

<servlet>
<servlet-name>home</servlet-name>
<jsp-file>/home.jsp</jsp-file>
</servlet>
相关问题