【问题标题】:servlet redirects me to home but blank pageservlet 将我重定向到主页但空白页面
【发布时间】:2017-09-03 12:51:27
【问题描述】:

为什么 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>

【问题讨论】:

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


    【解决方案1】:

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

    也许在

    resp.sendRedirect("home");
    

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

    <servlet>
    <servlet-name>home</servlet-name>
    <jsp-file>/home.jsp</jsp-file>
    </servlet>
    

    【讨论】:

    • 我添加了您提供的 url 模式,但仍然无法正常工作。我尝试查找运行时错误,但它不会引发任何错误。我目前正在使用 bluemix 的管道来创建网站。
    猜你喜欢
    • 2020-02-10
    • 2016-03-25
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多