我正在实现一种网络搜索引擎(使用 Lucene 库),到目前为止我所拥有的是一个 html 文件,其中有一个小表单,其中包含一个用于输入关键字的输入文本和一个用于发送表单的提交按钮,重点是代码我得到的是一个 .java 文件(需要其他 .jar 文件),我是 .jsp 以及 html 和 java 如何互连的新手,我的问题很明确是:如何将表单从 html 文件提交到java文件以及java如何从html文件接收数据?
我记得在 php 中我们做了 $_GET['keyword'] ,但在 java 中不知道。
感谢您的支持,
问候。
I'm achieving kinda web search engine (using Lucene library), what I have so far is a html file with a little form in within an input text to inter keyword and a submit button to send the form, the point is that the code I got is a .java file (that needs to other .jar files), and I'm a newbie in .jsp and how html and java interconnected, My question is clear is: how to submit a form from a html file to a java file and how does java recieve data from the html file??
I remember that in php we do for example $_GET['keyword'] but no idea in java.
Thanks for your support,
Regards.
发布评论
评论(3)
小问题大答案!
需要了解的主要一点是,当前图片中缺少一大块:在 HTML 和 Java 之间,您将需要一个 Web 服务器。并且不仅仅是一个 Web 服务器,一个知道如何运行 Java 程序的 Web 服务器 - 这称为 servlet 引擎。令人高兴的是,其中有很多,而且很容易获得。您应该获取 Tomcat 或 码头;两者都非常好(如果您要使用 Jetty,我建议使用版本 6,而不是版本 7 - 长话短说)。
获得 Web 服务器后,您需要为其提供 HTML 和 Java,以便它可以向客户端提供 HTML 并运行 Java。您可以通过将它们打包到“Web 存档”或 WAR 中来实现此目的,该文件具有特定的布局和一个名为 web.xml 的配置文件。它可能会帮助您查看一个非常小的示例 WAR。
这里需要理解的一个关键问题是 servlet 引擎将如何调用您的 Java 代码。为此有一个标准框架,您可以在其中将代码编写为servlet。 Oracle 有一个关于这些的教程,但内容相当密集; Servlet 实际上比它所暗示的要简单得多。我上面提到的示例 WAR 有一个非常简单的示例,以及使其工作所需的配置。您将找到 的文档javax.servlet.http 包是不可或缺的 - 这是 servlet 可以使用的大多数有用 API 的地方(父级 javax.servlet 包)。
安装 servlet 引擎、编写 servlet 并将其打包为 WAR 后,您将需要弄清楚如何启动 servlet 引擎并向其提供 WAR。恐怕我要让您阅读文档了。我认为每个人的做法都略有不同!
不管怎样,这应该能让你继续前进。这确实是 Java Web 编程世界中最基本的部分。除了 servlet 之外,还有一种称为 JSP 的东西您应该了解。之后,出现了一个巨大且难以驾驭的领域,称为 Web 框架,其目的是使编写 Web 应用程序变得更简单。每个人都有自己最喜欢的东西 - 我的是 Stripes,它很简单,但确实可以完成任务。之后,您可能还想了解更复杂的框架,例如 JSF 或 Wicket,之后可能是像 Spring 或 Seam 或其他,更集中的服务器端技术,例如 Enterprise JavaBeans。或者您可能只想坚持使用 servlet!
A small question with a large answer!
The main thing to understand is that there is a big chunk missing from your current picture: in between the HTML and the Java, you will need a web server. And more than just a web server, a web server that knows how to run Java programs - this is called a servlet engine. Happily, there are many of these, easily available. You should get hold of either Tomcat or Jetty; both are very good (if you're going for Jetty, i would suggest version 6 rather than version 7 - long story).
Once you've got your web server, you will need to give it your HTML and your Java, so it can serve the HTML to clients, and run the Java. You do this by packaging them in something called a 'web archive', or WAR, which has a particular layout, and a configuration file called web.xml. It might help you to look at a very minimal example WAR.
A key thing to understand here is how the servlet engine will call your Java code. There is a standard framework for this, in which you write your code as something called a servlet. Oracle has a tutorial about these, but it's rather dense; servlets are really much more simple than it might suggest. The example WAR i mention above has a very simple example of one, with the configuration needed to make it work. You will find the documentation for the javax.servlet.http package indispensable - that's where most of the useful API that can be used by servlets is to be found (there's also important stuff in the parent javax.servlet package).
Having installed your servlet engine, written your servlet, and packaged it as a WAR, you will need to figure out how to start your servlet engine and feed it your WAR. There, i'm afraid i'm going to leave you to read the documentation. I think everyone does it slightly differently!
Anyway, that should get you going. This is really the most basic slice through the world of Java web programming. As well as servlets, there is something called JSP you should know about. After that, there is a huge and fractious space of things called web frameworks, which aim to make writing web applications simpler. Everyone has their favourite - mine is something called Stripes, which is very simple, but really gets things done. After that, you might also want to look at more sophisticated frameworks like JSF or Wicket, and after that, perhaps 'full-stack' frameworks like Spring or Seam, or other, more focused, server-side technologies such as Enterprise JavaBeans. Or you might well just want to stick with servlets!
创建一个 servlet 类并将其映射到特定的 URL 模式,例如
/servleturl
。让 HTML在 servlet 类中,重写
doPost()
方法并通过request.getParameter()
收集提交的值。然后,在同一个 servlet 方法中,只需根据 Java 程序的文档导入、实例化和/或调用该 Java 程序。例如,
Create a servlet class and map it on a certain URL pattern, e.g.
/servleturl
. Let the HTML<form>
action point to that URL.In the servlet class, override the
doPost()
method and gather the submitted values byrequest.getParameter()
.Then, in the same servlet method just import, instantiate and/or invoke that Java program according to its documentation. For example,
您需要一些东西才能运行 Java Servlet 和 Java Server Pages。
对于规范的开源实现,请检查 Tomcat,例如: http://tomcat.apache.org
然后您可以实现您自己的 servlet 并覆盖适当的方法。您可能想从这里开始获得一个想法:http://java. sun.com/developer/onlineTraining/Programming/BasicJava1/servlet.html
You need something to be able to run Java Servlets and Java Server Pages.
For an open source implementation of the specifications check Tomcat e.g.: http://tomcat.apache.org
You can then implement your own servlet and overwrite the appropriate methods. You might want to start here to get an idea: http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/servlet.html