Java: How to invoke code running on a server from a browser?

前端 未结 6 602
花落未央
花落未央 2021-01-23 17:04

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 box
相关标签:
6条回答
  • 2021-01-23 17:07

    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).

    0 讨论(0)
  • 2021-01-23 17:20

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

    You can display result with javascript on your page.

    0 讨论(0)
  • 2021-01-23 17:21

    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.

    0 讨论(0)
  • 2021-01-23 17:24

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

    0 讨论(0)
  • 2021-01-23 17:25

    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">
      
      
      <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.

    0 讨论(0)
  • 2021-01-23 17:28

    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.

    0 讨论(0)
提交回复
热议问题