//Create a Dynamic Web Project...
//Right click on the Project and choose New -> Servlet
// ALTERNATE: New -> Other -> Web -> Servlet
OR Right click on your Dynamic Web Project -> New Class (this will create/use the default package)
Source Folder: hellobutton/src
Project Name: hellobutton
Superclass: java.lang.Object
Choose the Java package (for beginners it's ok to use Default)
Class Name: YOURAPP
Superclass: javax.servlet.http.HttpServlet (Next allows you to modify URL mappings)
To run your Servlet you can use the Run button from the menu (which will publish it
to your local server), but after publishing you can use your browser
(notice the PROJECTNAME / CLASSNAME can both have the same name = a weird URL)
http://localhost:8080/HelloServlet/HelloServlet
You may have to use services.msc in Windows to stop VMWARE services to run Eclipse/tomcat
(it can take up to 5 minutes for these services to relinquish control of port 8005)
verify using netstat -an
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//This is an example of a self submitting Java Servlet (yeah, JSP was designed to
//be the "presentation" layer so that SERVLETS can do the processing but...)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType( "text/html" ); // MIME type
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>");
out.println("<html>");
out.println("<head><title>Servlet</title></head>");
out.println("<body>");
out.println("<form action=\"Test\"; method=\"post\">");
out.println("<input type=\"submit\" value=\"Register\" style=\"color:#FF3300; font-size:120%;\">");
out.println("</form>");
//final Process process = Runtime.getRuntime().exec(command);
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
} //end class
MANUAL METHOD OF INSTALLING A SERVLET CLASS IN TOMCAT
/webapps/ROOT/WEB-INF/classes
web.xml file located in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/
<servlet>
<servlet-name>HelloForm</servlet-name>
<servlet-class>HelloForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloForm</servlet-name>
<url-pattern>/HelloForm</url-pattern>
</servlet-mapping>
*/
if the web.xml is not created you can also ...
Project Explorer -> right click -> Java EE Tools -> "Generate deployment descriptor stub" and web.xml will be generated under WEB-INF.
<?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" version="3.0">
<display-name>Report</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
ALSO, with Eclipse Indigo and Tomcat7, goofy error:
ERROR: Tomcat server does not start and says "internal error occur launching … org/eclipse/jdt/debug/core/JDIDebugModel
sudo apt-get --reinstall install tzdata-java
Then exit and relaunch Eclipse