开发者

How to submit a form from a html file to a java file? [duplicate]

开发者 https://www.devze.com 2023-03-03 17:21 出处:网络
This question already has answers here: How to transfer data from JSP to servlet when submitting HTML form
This question already has answers here: How to transfer data from JSP to servlet when submitting HTML form (4 answers) Closed 6 years ago.

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.


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!


Create a servlet class and map it on a certain URL pattern, e.g. /servleturl. Let the HTML <form> action point to that URL.

<form action="servleturl" method="post">
    <input type="text" name="foo" />
    <input type="text" name="bar" />
    <input type="submit">
</form>

In the servlet class, override the doPost() method and gather the submitted values by request.getParameter().

String foo = request.getParameter("foo");
String bar = request.getParameter("bar");
// ...

Then, in the same servlet method just import, instantiate and/or invoke that Java program according to its documentation. For example,

YourProgram program = new YourProgram();
program.process(foo, bar);
// ...


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

0

精彩评论

暂无评论...
验证码 换一张
取 消