Making a Servlet in IntelliJ
By: Dario Longhitano

Requirements for this tutorial include:
	A computer capable of running - 	Java 1.6
					IntelliJ 7584 or Later
					Apache TomCat 6.0

Note: The assumptions made in this tutorial is that you already have Java, IntelliJ, and the Apache Server preinstalled on your machine.

Now let us begin...

Step One: Starting the WebServer Project

1. First, you must make sure that when you run IntelliJ you already have the Apache server stopped.

2. In IntelliJ you will create a new project. (File - New Project)

3. Create a project from Scratch and click next.

4. Give your project a name and file location. For the purpose of this tutorial we use the file name "WebServer" and "C:\Apache TomCat Server\" as the file location.

5. Leave the Create Source Directory the Same and click next.

6. Select the checkbox for a Web Application and click finish, After IntelliJ Finishing Loading proceed to the next step.

Step Two: Configuring IntelliJ to use TomCat 6.0

7. Now to configure IntelliJ you need to edit configurations. (Run - Edit Configurations)

8. Click on the Plus Sign to add a new TomCat Server and make it Local.

9. Once you are finished give the Server a name, in this case we will use: Web Server.

10. Now we must make sure the start up page is "http://localhost:8080/WebServer.jsp" where WebServer.jsp is the name of your server.

11. Now click on configure, select Tomcat 6.0 and click ok.

12. Now click on the checkbox for Deploy "Tomcat Manager" application and procees to the next tab.

13. Under deployment click on Web (in Application Server), and click the checkbox for deply Web Facet 'Web.' Then just make sure that there is no longer a red x on the tomcat icon on the left pane where it says your server name, in this case WebServer. If there still is start again from step 7 because you missed a setting.

14. Click Apply.

NOTE:  These next two steps are not neccesarry in a small project but will help one evade a Heap Size Error once your program expands in size.

15. Click on the Wrench and Gear icon in the toolbar to get to the Settings of IntelliJ.

16. Click on Compiler Heap Size, and make it it half of  the max amount of ram installed on your machine. (ex. If Installed = 1 Gig, Set it to 512 Mb)

Step Three: Creating your First Servlet

17. Now if you click on WebServer.jsp (The name of your project) you will be able to customize your webserver page.

18. Make a new class file in the src folder that looks like this, and call it Servlet:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<html>Hello World!!!</html>");
        out.flush();
        out.close();
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

19. Then double click on the Web.xml file, and edit it to look like this:

NOTE: You can use the xml editor in IntelliJ to automate this for you, but that is in another lesson. 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <description>ServletDescription</description>
    <display-name>TheServlet</display-name>
    <context-param>
        <param-name>MyServletName</param-name>
        <param-value>value</param-value>
    </context-param>
    <servlet>
        <description>TheHelloWorldDescription</description>
        <display-name>Servlet</display-name>
        <servlet-name>TheServlet</servlet-name>
        <servlet-class>Servlet</servlet-class>
        <init-param>
            <param-name>MyServletName</param-name>
            <param-value>value</param-value>
        </init-param>

        <load-on-startup>0</load-on-startup>

    </servlet>
    <servlet-mapping>
        <servlet-name>TheServlet</servlet-name>
        <url-pattern>/TheServlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>WebServer.jsp</welcome-file>
    </welcome-file-list>
</web-app>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

20. The  <load-on-startup>0</load-on-startup> can be added to make the servlet run automatically on startup.

21. Enjoy your new IntelliJ and Tomcat Experience.