js0823
js0823

Reputation: 1873

Jetty/Tomcat needed to build a webpage in Java?

I am trying to learn web programming using Java, and I ran into bunch of frameworks which supports servlet to create webpages, such as Jetty and Apache Tomcat.

If I wanted to create a web page that has one button(not text), is Jetty/Tomcat required? From what I read, it seems all tutorials uses Jetty/Tomcat and servlet to create even a simple text based webpage.

If all I wanted my server to do was to accept incoming connection and display a button on the webpage(for example, user would just type hhtp://1.1.1.1:8080 and a webpage with a button will show up), is Jetty or Tomcat still required? Can I do it without them?

If so, is there an example for it?

Upvotes: 1

Views: 1232

Answers (5)

Jacek Cz
Jacek Cz

Reputation: 1906

Tomcat or Jetty IS NOR FRAMEWORK in sense like we use. These are strict speaking "servlet containers" or "server" speaking in popular way.

Java world has plenty of frameworks do web app. Simple work You can made without fw, but bigger projekt that is other history.

Both in terms "framework" (environment for programmers develeop his work) have JSP or pure Java coding. Older require much XML too, servlet 3 standard can use more annotations.

Upvotes: 0

Alexis Dufrenoy
Alexis Dufrenoy

Reputation: 11946

Java servlet, and also ASP or PHP, are used to achieve server-side interactivity, which means you don't want just to serve plain HTML to the client (the browser), but add a level of smart by being able to generate an ad-hoc web page, for example by inserting data from a database in it.

So if you look at the code of an servlet, it's doing exactly that: generating HTML (or other formats, but that's not the point here).

The servlet it self only contains the logic to let you generate this HTML, but all the boring stuff, like network connection, HTTP requests and responses management, cookies handling and so on is obviously not managed by your very servlet. Because some other software is already managing it, and offers these services to your application. And that's what a server like Tomcat or Jetty does. So the entry point of the process is the server, which calls your servlet to generate dynamically the HTML you want to transfer to the client, grabs the result and manages all the rest by itself without you having to care about it.

Upvotes: 2

vandershraaf
vandershraaf

Reputation: 895

As iNan points out, Apache HTTP is enough for plain HTML.

However, I think you are referencing Java website in its simplest form. If that so, try writing Java Servlet. A servlet is simply an entry point for HTTP response and request. Servlet in its simplest form looks something like this:

(copied from http://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/)

package com.mkyong;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo1 extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<body>");
    out.println("<h1>Hello Servlet Get</h1>");
    out.println("</body>");
    out.println("</html>"); 
     }
 }

Obviously there are several other stuff needed to be setup in the process (refer to the link above), but this is the simplest one. If you want to make a more complex Java-based website, you can use JSP (Java Server Page).

Hope this will help.

Upvotes: 3

mikera
mikera

Reputation: 106351

If you want to serve web pages then you will always need some form of web server application or library that listens on port 80 or 8080 and handles incoming http requests.

Jetty or Tomcat are just two (popular, mature, well-tested) options that enable you to do this. There are plenty of other options - see for example http://java-source.net/open-source/web-servers

If you really wanted, you could write a web server component yourself using Java NIO. But this would be a lot of work, and definitely not recommended for a beginner......

Upvotes: 2

Sabya
Sabya

Reputation: 1429

To serve static HTML content and respond to a HTTP Request, a HTTP Server is enough . You don't need a webserver like Tomcat .

Upvotes: 1

Related Questions