Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample of Java servlet
/*
* Java servlet
*
* Edit "/var/lib/[tomcat]/webapps/ROOT/WEB-INF/web.xml"
*
* <?xml version="1.0" encoding="UTF-8"?>
* <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
* http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
* version="3.1">
* <servlet>
* <servlet-name>Servlet</servlet-name>
* <servlet-class>Servlet</servlet-class>
* </servlet>
*
* <servlet-mapping>
* <servlet-name>Servlet</servlet-name>
* <url-pattern>/Servlet</url-pattern>
* </servlet-mapping>
* </web-app>
*
* Compile:
* javac Servlet.java -classpath "/usr/share/[tomcat]/lib/servlet-api.jar"
*
* Deploy:
* cp Servlet.class /var/lib/[tomcat]/webapps/ROOT/WEB-INF/classes/Servlet.class
* service [tomcat] restart
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
response.getWriter().println("Hello world!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment