4 ms·
Using Eclipse to bootstrap into Java represents a pretty reasonable and comfortable environment to wade into. Go to: http://www.eclipse.org/downloads/ http://w
by qwerty_asdf 13y ago
Using Eclipse to bootstrap into Java represents a pretty reasonable and comfortable environment to wade into.
Go to:
http://www.eclipse.org/downloads/ http://www.eclipse.org/downloads/
Download the Java EE version. Create a project with a class named Main.java in a package like "com.example.test".
In the class:
public static void main(String[] args) {
if (args != null && args.length > 0) {
for (int i = 0;i < args.length;i++) {
System.out.println("Hi there!");
}
} else System.out.println("Hello.");
}
Hey, presto! You're a Java programmer. Try compiling and running it.
To get your feet wet with web programming, here's some quick and dirty bare bones code:
TestServlet.java
/******************************************************************************/
package com.example.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
resp.getWriter().print("<html><body>Hello. <form action=\"\" method=\"POST\"><button>Hey!</button></form></body></html>");
} catch(Exception e) {
System.err.println(e.getClass().getName());
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
resp.getWriter().print("<html><body>Hi there!</body></html>");
} catch(Exception e) {
System.err.println(e.getClass().getName());
}
}
}
/******************************************************************************/
And then...
web.xml
<!-- ####################################################################### -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>com.example.test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<!-- ####################################################################### -->
This will compile and run on Apache Tomcat, if you compile and deploy as a ROOT.war file.