views:

133

answers:

6

Is there somehow that I can invoke Java running on a server from a web browser? I would like:

  1. User navigates to URL in a browser
  2. User fills in input boxes (text)
  3. User presses submit button
  4. Input fields are sent as parameters to java that is executing on the server
  5. A new html page is displayed that was generated by the java running on the server.

What is the standard way to do this, or something similar to this.

I think with PHP this would be relatively simple. I think that you would just pass arguments after the URL like this: www.mysite.com/folder?arguments.

+5  A: 

Yes, this is possible (and is extremely common). Two of the most common ways are Java Servlets (where responses are generated purely via Java code) and Java Server Pages (where server logic is intermingled within HTML, similar to ASP or PHP).

Mark Peters
+2  A: 

As a followup to Mark Peters answer, you need a java web server like Tomcat or GlassFish in order to use servlets or jsps. There are a lot of great frameworks for Java that help you abstract away from the original servlet classes, but I'll let you look those up and decided if you even need them for something this simple.

Hiro2k
A: 

If you want to pass arguments in a URL, then easier approach is Axis

You can display result with javascript on your page.

tulskiy
A: 

If you want to pass arguments in a URL, then easier approach is Axis

tulskiy
+4  A: 

There are countless ways to serve HTML from Java but virtually all of them rely on java servlets and java server pages (JSPs) which are Java's specification for handling web requests.

The absolute bare minimum to get going:

  1. Install Java EE SDK ensuring to also install Netbeans and Glassfish.
  2. Launch Netbeans and create a "Java Web" / "Web Application" project
  3. Enter a project name, e.g. MyWebApp
  4. In the Server and Settings screen, you need to Add... your server so do so. Point to the file location of your Glassfish server and enter the admin name and password
  5. Ignore the framework stuff and Finish
  6. NetBeans will generate a sample app and you can click straightaway on Run Main Project. It will deploy your app to Glassfish and load http://localhost:8080/MyWebApp/ from your default browser

Important things to note:

  1. A file called web.xml tells the host server some basics about your web app. This file can contain a lot of other stuff but the default is some boiler plate. The most interesting part says <welcome-file>index.jsp</welcome-file> which means when you load http://localhost:8080/MyWebApp/ it will default to load index.jsp.

  2. The index.jsp is what gets loaded if you don't specify a page to the server. If you look at index.jsp it's just HTML with some JSP markup.

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd"&gt;
    
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
        </body>
    </html>
    
  3. Creating new JSPs is as simple as writing HTML. Netbeans has a wizard to create a simple JSP.

  4. You can embed chunks of Java into a .jsp easily and step in and out of Java / HTML with the <% %> notation such as

    <% for (int i = 0; i < 10; i++) { %> Hello <%=i%>
    <% } %>

  5. Glassfish is just one possible app server. As long as you write compliant code it should functional with only minimal or zero modifications on any other implementation of Java Servlet / JSP spec. e.g. Jetty, Tomcat, oc4j, JBoss, WebSphere etc.

  6. This is just the tip of the iceberg. You can make things as simple or complex as you like.

Once you know the basics then it's up to you how deep you go. More advanced topics would be:

  1. Taglibraries - these can remove a lot of java clutter and are considered more correct
  2. Expressions - using expressions inside JSP pages to reduce the need for messy <%= notation
  3. Custom servlets let you move model / business stuff into a Java class and leave the .jsp to just presentational
  4. MVC web frameworks like Struts, Spring etc.
  5. Security & filtering

It's a massive subject but it's fairly easy to do something quick and dirty.

locka
"Netbeans and Glassfish" are certainly not part of the "bare minimum". I've done just fine with Eclipse and Tomcat, thank you. But some good points, +1.
Mark Peters
I'm someone that likes to spend as much time as possible in Java and as little as possible in HTML. I've tried a lot of different methods of doing this and my favorite combo is Netbeans + Glassfish + JEE/JSF. Fans of HTML would probably prefer the JSP approach. Obviously as @Mark mentions, Eclipse and Tomcat are also usable too.
Brian Knoblauch
I'm talking bare minimum to get up and running from the POV of someone coming from PHP. My original post actually referred to Tomcat but then it got messy describing tomcat-users.xml so I thought easier to use what comes out of the box. Netbeans + Glassfish just happens to be the easiest to get going even though I use Eclipse, Maven, Jetty and oc4j in my daily life. If Maven were the default that everyone knew about I'd just say generate a war archetype and then run Tomcat from a plugin.
locka
@locka: Fair enough, I hadn't considered that the OP might not have developed in Java before. Just didn't want somebody to think they didn't have their choice of tools when using Java Servlets (particularly when you suggested Netbeans, which is truly not needed to make servlets).
Mark Peters
A: 

My school has an apache server that we are required to use. I was not allowed to install tomcat. I ended up invoking my server side Java using PHP. Not the most beautiful solution but it works.

sixtyfootersdude