【问题标题】:My html form is not being redirected after filling form to HelloWorld.java?将表单填写到 HelloWorld.java 后,我的 html 表单没有被重定向?
【发布时间】:2014-06-19 23:18:23
【问题描述】:

我是 JavaEE 的初学者,我正在使用 intellij idea 作为 IDE,并且我已经将表单制作成这样的 html:

这是我的Hello.html

<!DOCTYPE html>
<html>
<body>
<form action="HelloWorld" method="POST">
    First name: <input type="text" name="first_name">
    last name: <input type="text" name="last_name">
    <input type="submit"  value="Submit">
</form>
</body>
</html>

现在我已经制作了一个 servlet 文件,上面写着 HelloWorld.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        response.setContentType("text/html");

        PrintWriter out=response.getWriter();
        String docType="<!DOCTYPE html>";

        out.println(docType+
        "<html>" +
                "<head><title>This is just a title man.. ;)</title></head>" +
                "<body>" +
                "<h1>My name is Rajendra Arora</h1>" +
                "<ul>" +
                "<li><b>First name: </b></li>" +
                request.getParameter("first_name")+"<br>"+
                "<li><b>Last name: </b></li>" +
                request.getParameter("last_name")+
                "</ul>" +
                "</body>" +
        "</html>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        doGet(request, response);
    }
}

这是我的web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="3.1">

    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>

</web-app>

现在当我运行我的HelloWorld.java 时它运行成功,但是当它与表单一起运行时它没有运行,我的意思是当我运行Hello.html 时它以正确的方式显示表单但是当我在单击提交后填写名称时它显示错误消息请参阅我的pic。 我以正确的方式开始我的工作并且它正在工作,并且当我使用http://localhost:8888/HelloWorld?first_name=Raj&amp;last_name=Arora输入url时只运行HelloWorld.java它成功显示名称但默认使用Hello.html它显示错误消息......请帮助

请帮忙!!

【问题讨论】:

  • 在浏览器上发布表单的html生成代码..
  • 正如你所说,我做了所有但不是工作的人:(
  • 我猜你正在通过http://localhost:8888/HelloWorld访问Hello.html
  • 最后我解决了我的问题,所以这就是为什么我正在考虑给出我自己的答案,最后我的程序正在运行谢谢大家这里是解决方案请查看pic
  • 请给出你自己的答案

标签: java html forms servlets intellij-idea


【解决方案1】:

您没有指定 helloworld 文件的扩展名。它应该是 helloworld.java。同时删除分号“;”从标题标签。您收到错误“filenotfound”只是因为您没有为 helloworld 提供扩展名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多