Sunteți pe pagina 1din 2

How-to Create Servlets in IntelliJ IDEA

This How-to briefly describes how to create a servlet in IntelliJ IDEA, basing on a servlet
sample.

Creating a Basic Servlet Sample


How-to Make It Run

Related documents:
IDEA Help: Web Application Options, Web Application Debugging Options, JSP and
Servlets Support.
How-to Series: How-to Set up Web Application Support in IntelliJ IDEA, JSP How-to

in IntelliJ IDEA.

Creating a Basic Servlet Sample


1. Tomcat 4.0.x is used as a web-server.
2. In any default package of your project, create a package named "myServletPackage".
Let's consider, that that its full path is "C:\web\src\myServletPackage".
3. Create a new class in that package as follows:

package myServletPackage;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import java.io.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response

PrintWriter out = response.getWriter();


out.println("Hello, this is a servlet!!!");
}
}

To speed up development process you can create a specific File Template for servlets
using Options | File Templates dialog.

How-to Make It Run


1. Set up the Web Application Support as described in the How-to Set up Web
Application Support in IntelliJ IDEA. Some settings need to be adjusted further to
conform with the present How-to .
2. Prior to going further, please, make sure that your web application folder (in our case "C:/web/src/") has the following structure of subfolders:

3. Make sure that "C:\web\src\" is added to your project's sourcepath, and "C:\web\WEBINF\classes\" is specified as an output path.
4. If there is no web.xml in the WEB-INF folder, create it as shown in the next step.
5. Create a file named web.xml in the WEB-INF folder.
Sample web.xml (your web application deployment descriptor).
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2
<web-app>
<servlet>
<servlet-name>MyServletName</servlet-name>
<servlet-class>myServletPackage.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServletName</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>

The servlet-class tag should contain the fully qualified name of your servlet class.
If you have already created web.xml, replace it with the sample one. More details on
web.xml you can find in the How-to Set up Web Application Support in IntelliJ IDEA,
section Creating minimal web.xml for your servlet and in the Tomcat documentation.
6. Add the following context to server.xml (the Tomcat configuration file) within the Host
tag.
<Context path="/testAppl" docBase="C:\web"/>
More details on contexts you can find in the How-to Set up Web Application Support in
IntelliJ IDEA, section Tomcat configuration details and in the Tomcat documentation.
7. Set up run/debug configuration for your web application in IDEA (if you haven't done it
before) as described in How-to Set up Web Application Support in IntelliJ IDEA.
8. Compile MyServlet.
9. Run the web application. To see how it works, type "http://localhost:8080/testAppl/
myservlet/" in the browser address field.

S-ar putea să vă placă și